From c3a389de28c291f9bf30dbd6e85b4e9bceb36b1a Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Wed, 16 Mar 2022 14:16:16 -0500 Subject: [PATCH] saving completed image view for API --- smugfolioapi/models/images.py | 2 +- smugfolioapi/views/__init__.py | 3 +- smugfolioapi/views/image_tag_assignments.py | 0 smugfolioapi/views/images.py | 98 +++++++++++++++++++++ smugfolioserver/db.sql | 4 +- smugfolioserver/urls.py | 8 +- 6 files changed, 110 insertions(+), 5 deletions(-) delete mode 100644 smugfolioapi/views/image_tag_assignments.py diff --git a/smugfolioapi/models/images.py b/smugfolioapi/models/images.py index 0aa21a4..3c6498f 100644 --- a/smugfolioapi/models/images.py +++ b/smugfolioapi/models/images.py @@ -4,7 +4,7 @@ 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) # 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. diff --git a/smugfolioapi/views/__init__.py b/smugfolioapi/views/__init__.py index 0801135..f769e06 100644 --- a/smugfolioapi/views/__init__.py +++ b/smugfolioapi/views/__init__.py @@ -1 +1,2 @@ -from .auth import login_user, register_user \ No newline at end of file +from .auth import login_user, register_user +from .images import ImageView \ No newline at end of file diff --git a/smugfolioapi/views/image_tag_assignments.py b/smugfolioapi/views/image_tag_assignments.py deleted file mode 100644 index e69de29..0000000 diff --git a/smugfolioapi/views/images.py b/smugfolioapi/views/images.py index e69de29..a001d8c 100644 --- a/smugfolioapi/views/images.py +++ b/smugfolioapi/views/images.py @@ -0,0 +1,98 @@ +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 + +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 + smug_user = Smug_Users.objects.get(pk=request.data['smug_user']) + images = Images.objects.create( + file_name=request.data["file_name"], + file_path=request.data["file_path"], + album_id=request.data["album_id"], + 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["skill_level"] + images.upload_date = request.data["upload_date"] + + 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') + depth = 2 + + + \ No newline at end of file diff --git a/smugfolioserver/db.sql b/smugfolioserver/db.sql index 3502d83..0e49153 100644 --- a/smugfolioserver/db.sql +++ b/smugfolioserver/db.sql @@ -1,2 +1,2 @@ -DELETE FROM 'auth_user' -WHERE id=2; \ No newline at end of file +ALTER TABLE smugfolioapi_images +RENAME 'smug_user_id_id' TO 'smug_user_id'; \ No newline at end of file diff --git a/smugfolioserver/urls.py b/smugfolioserver/urls.py index dc3dd48..37a1f70 100644 --- a/smugfolioserver/urls.py +++ b/smugfolioserver/urls.py @@ -16,12 +16,18 @@ 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.models.smug_users import Smug_Users +from smugfolioapi.views import register_user, login_user, ImageView from rest_framework import routers from django.contrib import admin from django.urls import path +from smugfolioapi.views.images import ImageView + router = routers.DefaultRouter(trailing_slash=False) +router.register(r'images', ImageView, 'image') +# router.register(r'smug_users',Smug_Users,'smug_user') +# router.register(r'image_tags', Image_Tags,'image_tag') urlpatterns = [ path('admin/', admin.site.urls),