diff --git a/db.sql b/db.sql new file mode 100644 index 0000000..e69de29 diff --git a/smugfolioapi/migrations/0002_rename_smug_user_id_images_smug_user_images_picture.py b/smugfolioapi/migrations/0002_rename_smug_user_id_images_smug_user_images_picture.py new file mode 100644 index 0000000..52298e7 --- /dev/null +++ b/smugfolioapi/migrations/0002_rename_smug_user_id_images_smug_user_images_picture.py @@ -0,0 +1,23 @@ +# Generated by Django 4.0.3 on 2022-03-17 18:43 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('smugfolioapi', '0001_initial'), + ] + + operations = [ + migrations.RenameField( + model_name='images', + old_name='smug_user_id', + new_name='smug_user', + ), + migrations.AddField( + model_name='images', + name='picture', + field=models.ImageField(null=True, upload_to='smugimages'), + ), + ] diff --git a/smugfolioapi/models/images.py b/smugfolioapi/models/images.py index 0aa21a4..1a5ff0f 100644 --- a/smugfolioapi/models/images.py +++ b/smugfolioapi/models/images.py @@ -1,11 +1,20 @@ from django.db import models class Images(models.Model): - file_name = models.CharField(max_length=1024) - file_path = models.CharField(max_length=1024) album_id = models.IntegerField(default=0) - smug_user_id = models.ForeignKey("Smug_Users", on_delete=models.CASCADE) + smug_user = models.ForeignKey("Smug_Users", on_delete=models.CASCADE) upload_date = models.DateTimeField(auto_now_add=True) + picture = models.ImageField( + upload_to='smugimages', height_field=None, + width_field=None, max_length=None, null=True + ) + + # @property + # def imagecount(self): + # return self.__imagecount + # @imagecount.setter + # def imagecount(self, value): + # self.__imagecount = value # I believe that auto_now_add is the correct syntax. If I'm understanding this correctly, it only creates a timestamp when the object is first created. # https://www.geeksforgeeks.org/datetimefield-django-models/ \ No newline at end of file diff --git a/smugfolioapi/views/__init__.py b/smugfolioapi/views/__init__.py index 0801135..a113543 100644 --- a/smugfolioapi/views/__init__.py +++ b/smugfolioapi/views/__init__.py @@ -1 +1,4 @@ -from .auth import login_user, register_user \ No newline at end of file +from .auth import login_user, register_user +from .images import ImageView +from .smug_users import SmugUserView +from .image_tags import Image_Tags \ No newline at end of file diff --git a/smugfolioapi/views/image_tags.py b/smugfolioapi/views/image_tags.py new file mode 100644 index 0000000..ce42c53 --- /dev/null +++ b/smugfolioapi/views/image_tags.py @@ -0,0 +1,93 @@ +from django.http import HttpResponseServerError +from rest_framework.viewsets import ViewSet +from rest_framework.response import Response +from rest_framework import serializers, status +from django.contrib.auth.models import User +from smugfolioapi.models import Image_Tags +from rest_framework.decorators import action + +class ImageTagsView(ViewSet): + """SmugFolio image_Tags s view""" + + def retrieve(self, request, pk): + """Handle GET requests for single image_Tags + + + Returns: + Response -- JSON serialized image_Tags + """ + + + + try: + image_Tags = Image_Tags.objects.get(pk=pk) + serializer = Image_TagsSerializer(image_Tags) + return Response(serializer.data) + except Image_Tags.DoesNotExist as ex: + return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND) + + + def list(self, request): + """Handle GET requests to get all image_Tags + + Returns: + Response -- JSON serialized list of image_Tags + """ + image_Tags_ = request.query_params.get('type', None) + image_Tags = Image_Tags.objects.all() + if image_Tags_ is not None: + image_Tags = image_Tags.filter(image_Tags_type_id=image_Tags_) + serializer = Image_TagsSerializer(image_Tags, many=True) + return Response(serializer.data) + + def create(self, request): + """Handle POST operations + + Returns + Response -- JSON serialized image_Tags instance + """ + + image_Tags = Image_Tags.objects.create( + image_tag_name = request.data["image_tag_name"] + + ) + serializer = Image_TagsSerializer(image_Tags) + return Response(serializer.data, status=201) + + def update(self, request, pk): + """Handle PUT requests for a image_Tags + + Returns: + Response -- Empty body with 204 status code + """ + + image_Tags = Image_Tags.objects.get(pk=pk) + image_Tags.image_tag_name = request.data["image_tag_name"] + image_Tags.save() + + return Response(None, status=status.HTTP_204_NO_CONTENT) + def destroy(self, request, pk): + """Handle DELETE requests for a image_Tags + + Args: + request (object): DELETE request object + pk (num): Accepts the argument of the primary key of an image_Tags + + Returns: + Status: If the method completes successfully, return a HTTP response 204 + """ + image_Tags = Image_Tags.objects.get(pk=pk) + image_Tags.delete() + return Response(None, status=status.HTTP_204_NO_CONTENT) + + +class Image_TagsSerializer(serializers.ModelSerializer): + """JSON serializer for image_Tags s + """ + class Meta: + model = Image_Tags + fields = ('id', 'image_tag_name') + depth = 2 + + + \ No newline at end of file diff --git a/smugfolioapi/views/images.py b/smugfolioapi/views/images.py new file mode 100644 index 0000000..930db10 --- /dev/null +++ b/smugfolioapi/views/images.py @@ -0,0 +1,107 @@ +from django.http import HttpResponseServerError +from rest_framework.viewsets import ViewSet +from rest_framework.response import Response +from rest_framework import serializers, status +from smugfolioapi.models import Images, Smug_Users +from PIL import Image +import uuid +import base64 +from django.core.files.base import ContentFile + +class ImageView(ViewSet): + """SmugFolio images s view""" + + def retrieve(self, request, pk): + """Handle GET requests for single images + + + Returns: + Response -- JSON serialized images + """ + + + + try: + images = Images.objects.get(pk=pk) + serializer = ImagesSerializer(images) + return Response(serializer.data) + except Images.DoesNotExist as ex: + return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND) + + + def list(self, request): + """Handle GET requests to get all images + + Returns: + Response -- JSON serialized list of images + """ + images_ = request.query_params.get('type', None) + images = Images.objects.all() + if images_ is not None: + images = images.filter(images_type_id=images_) + serializer = ImagesSerializer(images, many=True) + return Response(serializer.data) + + def create(self, request): + """Handle POST operations + + Returns + Response -- JSON serialized images instance + """ + # object must be retrived when using a foreign key + format, imgstr = request.data["picture"].split(';base64,') + ext = format.split('/')[-1] + data = ContentFile(base64.b64decode(imgstr), name=f'{request.data["picture"]}-{uuid.uuid4()}.{ext}') + smug_user = Smug_Users.objects.get(user=request.auth.user) + images = Images.objects.create( + file_name=request.data["file_name"], + file_path=request.data["file_path"], + album_id=request.data["album_id"], + picture= data, + smug_user= smug_user, + upload_date = request.data["upload_date"] + ) + serializer = ImagesSerializer(images) + return Response(serializer.data, status=201) + + def update(self, request, pk): + """Handle PUT requests for a images + + Returns: + Response -- Empty body with 204 status code + """ + # Refactor keys to match ERD + images = Images.objects.get(pk=pk) + images.file_name = request.data["file_name"] + images.file_path = request.data["file_path"] + images.album_id = request.data["album_id"] + images.smug_user_id = request.data["smug_user_id"] + images.upload_date = request.data["upload_date"] + images.save() + + return Response(None, status=status.HTTP_204_NO_CONTENT) + def destroy(self, request, pk): + """Handle DELETE requests for a images + + Args: + request (object): DELETE request object + pk (num): Accepts the argument of the primary key of a images + + Returns: + Status: If the method completes successfully, return a HTTP response 204 + """ + images = Images.objects.get(pk=pk) + images.delete() + return Response(None, status=status.HTTP_204_NO_CONTENT) + + +class ImagesSerializer(serializers.ModelSerializer): + """JSON serializer for images s + """ + class Meta: + model = Images + fields = ('id', 'file_name', 'file_path', 'album_id', 'smug_user_id', 'upload_date', 'picture', 'imagecount') + depth = 2 + + + \ No newline at end of file diff --git a/smugfolioapi/views/smug_users.py b/smugfolioapi/views/smug_users.py new file mode 100644 index 0000000..bb8c228 --- /dev/null +++ b/smugfolioapi/views/smug_users.py @@ -0,0 +1,80 @@ +from django.http import HttpResponseServerError +from rest_framework.viewsets import ViewSet +from rest_framework.response import Response +from rest_framework import serializers, status +from django.contrib.auth.models import User +from smugfolioapi.models import Smug_Users + +class SmugUserView(ViewSet): + """SmugFolio smug_users s view""" + + def retrieve(self, request, pk): + """Handle GET requests for single smug_users + + + Returns: + Response -- JSON serialized smug_users + """ + + + + try: + smug_users = Smug_Users.objects.get(pk=pk) + serializer = Smug_UsersSerializer(smug_users) + return Response(serializer.data) + except Smug_Users.DoesNotExist as ex: + return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND) + + + def list(self, request): + """Handle GET requests to get all smug_users + + Returns: + Response -- JSON serialized list of smug_users + """ + smug_users_ = request.query_params.get('type', None) + smug_users = Smug_Users.objects.all() + if smug_users_ is not None: + smug_users = smug_users.filter(smug_users_type_id=smug_users_) + serializer = Smug_UsersSerializer(smug_users, many=True) + return Response(serializer.data) + + def update(self, request, pk): + """Handle PUT requests for a smug_users + + Returns: + Response -- Empty body with 204 status code + """ + + smug_users = smug_users.objects.get(pk=pk) + smug_users.user = request.data["user"] + smug_users.business_name = request.data["business_name"] + smug_users.business_owner = request.data["business_owner"] + + + return Response(None, status=status.HTTP_204_NO_CONTENT) + def destroy(self, request, pk): + """Handle DELETE requests for a smug_users + + Args: + request (object): DELETE request object + pk (num): Accepts the argument of the primary key of a smug_users + + Returns: + Status: If the method completes successfully, return a HTTP response 204 + """ + smug_users = smug_users.objects.get(pk=pk) + smug_users.delete() + return Response(None, status=status.HTTP_204_NO_CONTENT) + + +class Smug_UsersSerializer(serializers.ModelSerializer): + """JSON serializer for smug_users s + """ + class Meta: + model = Smug_Users + fields = ('id', 'business_name', 'business_owner') + depth = 2 + + + \ No newline at end of file diff --git a/smugfolioserver/__init__.py b/smugfolioserver/__init__.py index e69de29..3f0901b 100644 --- a/smugfolioserver/__init__.py +++ b/smugfolioserver/__init__.py @@ -0,0 +1 @@ +from .settings import MEDIA_URL, MEDIA_ROOT \ No newline at end of file diff --git a/smugfolioserver/db.sql b/smugfolioserver/db.sql index 3502d83..6517769 100644 --- a/smugfolioserver/db.sql +++ b/smugfolioserver/db.sql @@ -1,2 +1,5 @@ -DELETE FROM 'auth_user' -WHERE id=2; \ No newline at end of file +DELETE FROM smugfolioapi_images +WHERE id = 1; + +SELECT * +FROM smugfolioapi_images; \ No newline at end of file diff --git a/smugfolioserver/settings.py b/smugfolioserver/settings.py index 453d1b6..db8d1b8 100644 --- a/smugfolioserver/settings.py +++ b/smugfolioserver/settings.py @@ -148,4 +148,7 @@ STATIC_URL = 'static/' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' -SECRET_KEY = os.environ.get('MY_SECRET_KEY') \ No newline at end of file +SECRET_KEY = os.environ.get('MY_SECRET_KEY') + +MEDIA_ROOT = 'media' +MEDIA_URL = '/media/' \ No newline at end of file diff --git a/smugfolioserver/urls.py b/smugfolioserver/urls.py index dc3dd48..2675cba 100644 --- a/smugfolioserver/urls.py +++ b/smugfolioserver/urls.py @@ -16,12 +16,19 @@ Including another URLconf from django.contrib import admin from django.conf.urls import include from django.urls import path -from smugfolioapi.views import register_user, login_user +from smugfolioapi.views import register_user, login_user, ImageView, SmugUserView from rest_framework import routers from django.contrib import admin from django.urls import path +from django.conf.urls.static import static +from smugfolioapi.views.image_tags import ImageTagsView +from smugfolioapi.views.images import ImageView +from smugfolioserver import settings, MEDIA_ROOT, MEDIA_URL router = routers.DefaultRouter(trailing_slash=False) +router.register(r'images', ImageView, 'image') +router.register(r'smug_users',SmugUserView,'smug_user') +router.register(r'image_tags', ImageTagsView,'image_tag') urlpatterns = [ path('admin/', admin.site.urls), @@ -29,4 +36,4 @@ urlpatterns = [ path('login', login_user), path('api-auth', include('rest_framework.urls', namespace='rest_framework')), path('', include(router.urls)), -] +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)