saving completed image view for API

This commit is contained in:
2022-03-16 14:16:16 -05:00
parent c5f5c6d74a
commit c3a389de28
6 changed files with 110 additions and 5 deletions
+1 -1
View File
@@ -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.
+2 -1
View File
@@ -1 +1,2 @@
from .auth import login_user, register_user
from .auth import login_user, register_user
from .images import ImageView
+98
View File
@@ -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
+2 -2
View File
@@ -1,2 +1,2 @@
DELETE FROM 'auth_user'
WHERE id=2;
ALTER TABLE smugfolioapi_images
RENAME 'smug_user_id_id' TO 'smug_user_id';
+7 -1
View File
@@ -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),