saving completed views

This commit is contained in:
2022-03-17 09:49:53 -05:00
parent c3a389de28
commit ff6891dd70
6 changed files with 200 additions and 6 deletions
+4
View File
@@ -7,5 +7,9 @@ class Images(models.Model):
smug_user = models.ForeignKey("Smug_Users", on_delete=models.CASCADE)
upload_date = models.DateTimeField(auto_now_add=True)
@property
def imagecount(self):
return self.__imagecount
# 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/
+3 -1
View File
@@ -1,2 +1,4 @@
from .auth import login_user, register_user
from .images import ImageView
from .images import ImageView
from .smug_users import SmugUserView
from .image_tags import Image_Tags
+91
View File
@@ -0,0 +1,91 @@
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
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=request.data['id'])
image_Tags.image_tag_name = request.data["image_tag_name"]
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 a 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
+1
View File
@@ -3,6 +3,7 @@ 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
class ImageView(ViewSet):
"""SmugFolio images s view"""
+97
View File
@@ -0,0 +1,97 @@
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 create(self, request):
"""Handle POST operations
Returns
Response -- JSON serialized smug_users instance
"""
# object must be retrived when using a foreign key
user = User.objects.get(pk=request.data['users'])
smug_users = smug_users.objects.create(
user=request.data["user"],
business_name=request.data["business_name"],
business_owner=request.data["business_owner"],
)
serializer = Smug_UsersSerializer(smug_users)
return Response(serializer.data, status=201)
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
+4 -5
View File
@@ -16,18 +16,17 @@ Including another URLconf
from django.contrib import admin
from django.conf.urls import include
from django.urls import path
from smugfolioapi.models.smug_users import Smug_Users
from smugfolioapi.views import register_user, login_user, ImageView
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 smugfolioapi.views.image_tags import ImageTagsView
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')
router.register(r'smug_users',SmugUserView,'smug_user')
router.register(r'image_tags', ImageTagsView,'image_tag')
urlpatterns = [
path('admin/', admin.site.urls),