Added all views that can be tested without a client

This commit is contained in:
2022-03-17 15:56:28 -05:00
parent 3cbb666213
commit 14a98f65ed
9 changed files with 57 additions and 12 deletions
View File
@@ -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'),
),
]
+10 -5
View File
@@ -1,15 +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 = 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
# @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/
+1
View File
@@ -4,6 +4,7 @@ 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"""
+10 -3
View File
@@ -4,6 +4,9 @@ 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"""
@@ -35,7 +38,7 @@ class ImageView(ViewSet):
images_ = request.query_params.get('type', None)
images = Images.objects.all()
if images_ is not None:
images = images.filter(images_type_id=images_)
images = images.filter(images_type_id=images_)
serializer = ImagesSerializer(images, many=True)
return Response(serializer.data)
@@ -46,12 +49,16 @@ class ImageView(ViewSet):
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"],
smug_user=smug_user,
picture= data,
smug_user= smug_user,
upload_date = request.data["upload_date"]
)
serializer = ImagesSerializer(images)
@@ -93,7 +100,7 @@ class ImagesSerializer(serializers.ModelSerializer):
"""
class Meta:
model = Images
fields = ('id', 'file_name', 'file_path', 'album_id', 'smug_user_id', 'upload_date')
fields = ('id', 'file_name', 'file_path', 'album_id', 'smug_user_id', 'upload_date', 'picture', 'imagecount')
depth = 2
+1
View File
@@ -0,0 +1 @@
from .settings import MEDIA_URL, MEDIA_ROOT
+5 -2
View File
@@ -1,2 +1,5 @@
ALTER TABLE smugfolioapi_images
RENAME 'smug_user_id_id' TO 'smug_user_id';
DELETE FROM smugfolioapi_images
WHERE id = 1;
SELECT *
FROM smugfolioapi_images;
+4 -1
View File
@@ -148,4 +148,7 @@ STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
SECRET_KEY = os.environ.get('MY_SECRET_KEY')
SECRET_KEY = os.environ.get('MY_SECRET_KEY')
MEDIA_ROOT = 'media'
MEDIA_URL = '/media/'
+3 -1
View File
@@ -20,8 +20,10 @@ from smugfolioapi.views import register_user, login_user, ImageView, SmugUserVie
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')
@@ -34,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)