Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Initializing API #39

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion community_portal/api/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.contrib import admin

# Register your models here.
from .models import Group

admin.site.register(Group)
30 changes: 30 additions & 0 deletions community_portal/api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.0.5 on 2022-07-02 12:13

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Group',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50)),
('description', models.TextField(null=True)),
('country', models.CharField(max_length=50)),
('image', models.ImageField(blank=True, null=True, upload_to='uploads/')),
('cover_image', models.ImageField(blank=True, null=True, upload_to='uploads/')),
('date_added', models.DateTimeField(auto_now_add=True)),
('slug', models.SlugField()),
],
options={
'ordering': ['-date_added'],
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.5 on 2022-07-02 13:04

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='group',
old_name='cover_image',
new_name='thumbnail',
),
]
21 changes: 21 additions & 0 deletions community_portal/api/migrations/0003_group_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.0.5 on 2022-07-04 14:24

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('api', '0002_rename_cover_image_group_thumbnail'),
]

operations = [
migrations.AddField(
model_name='group',
name='creator',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
53 changes: 52 additions & 1 deletion community_portal/api/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
from django.db import models
from io import BytesIO
from PIL import Image
from django.core.files import File
from django.contrib.auth.models import User

class Group(models.Model):
name = models.CharField(max_length=50,null=False)
creator = models.ForeignKey(User, on_delete=models.CASCADE)
description = models.TextField(blank=False, null=True)
country = models.CharField(max_length=50)
image = models.ImageField(upload_to='uploads/', blank=True, null=True)
thumbnail = models.ImageField(upload_to='uploads/', blank=True, null=True)
date_added = models.DateTimeField(auto_now_add=True)
slug = models.SlugField()

class Meta:
ordering = ['-date_added']

def __str__(self):
return self.name

def get_absolute_url(self):
return f'/{self.slug}/'

def get_image(self):
if self.image:
return 'http://127.0.0.1:8000' + self.image.url
return ''

def get_thumbnail(self):
if self.thumbnail:
return 'http://127.0.0.1:8000' + self.thumbnail.url
else:
if self.image:
self.thumbnail = self.make_thumbnail(self.image)
self.save()

return 'http://127.0.0.1:8000' + self.thumbnail.url
else:
return ''

def make_thumbnail(self, image, size=(300, 200)):
img = Image.open(image)
img.convert('RGB')
img.thumbnail(size)

thumb_io = BytesIO()
img.save(thumb_io, 'JPEG', quality=85)

thumbnail = File(thumb_io, name=image.name)

return thumbnail

# Create your models here.
17 changes: 17 additions & 0 deletions community_portal/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework import serializers

from .models import Group

class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = (
"id",
"name",
"creator",
"get_absolute_url",
"description",
"country",
"get_image",
"get_thumbnail"
)
12 changes: 12 additions & 0 deletions community_portal/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import path, include
from rest_framework.urlpatterns import format_suffix_patterns
from api import views

urlpatterns = [
path('group_list/', views.GroupsList.as_view()),
path('create_group/', views.CreateGroup.as_view()),
path('group_details/<int:pk>/', views.GroupDetail.as_view()),
path('update_group/<int:pk>/', views.UpdateGroupDetail.as_view()),

]
urlpatterns = format_suffix_patterns(urlpatterns)
58 changes: 56 additions & 2 deletions community_portal/api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
from django.shortcuts import render
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Group
from rest_framework import status
from rest_framework.generics import ListAPIView
from .serializers import GroupSerializer
from rest_framework.filters import SearchFilter

# Create your views here.
class GroupsList(ListAPIView):
queryset = Group.objects.all()
serializer_class = GroupSerializer
filter_backends = [SearchFilter]
search_fields = ['^name','^country','^description',]


class CreateGroup(APIView):
def post(self, request, format=None):
serializer = GroupSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class GroupDetail(APIView):
def get_object(self, pk):
try:
return Group.objects.get(pk=pk)
except Group.DoesNotExist:
raise Http404

def get(self, request, pk, format=None):
Groups = self.get_object(pk)
serializer = GroupSerializer(Groups, many=False)
return Response(serializer.data)


class UpdateGroupDetail(APIView):

def get_object(self, pk):
try:
return Group.objects.get(pk=pk)
except Group.DoesNotExist:
raise Http404

def put(self, request, pk, format=None):
Groups = self.get_object(pk)
serializer = GroupSerializer(Groups, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def delete(self, request, pk, format=None):
Groups = self.get_object(pk)
Groups.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
6 changes: 5 additions & 1 deletion community_portal/community_portal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api',
'rest_framework',
'django_filters',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -120,7 +123,8 @@
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

Expand Down
22 changes: 5 additions & 17 deletions community_portal/community_portal/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
"""community_portal URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
]
path('api/', include('api.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Binary file modified community_portal/requirements.txt
Binary file not shown.