From 68558a7fd0209b218bec30a0e0dda8bd1e115188 Mon Sep 17 00:00:00 2001 From: vitor Date: Mon, 13 Feb 2023 00:20:53 -0300 Subject: [PATCH] image fields --- .env | 3 +++ apps/core/migrations/0001_initial.py | 21 +++++++++++++++ apps/core/models.py | 2 ++ apps/core/urls.py | 3 ++- apps/core/views.py | 34 ++++++++++++++++++++++--- frontend/src/components/EditProfile.vue | 7 ++++- frontend/src/components/Image.vue | 26 +++++++++++++++++++ requirements.txt | 4 +-- twikker/settings.py | 5 ++++ 9 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 apps/core/migrations/0001_initial.py create mode 100644 frontend/src/components/Image.vue diff --git a/.env b/.env index e69de29..2819469 100644 --- a/.env +++ b/.env @@ -0,0 +1,3 @@ +CLOUDINARY_CLOUD_NAME=cccccccccccc +CLOUDINARY_API_KEY=ccccccccccccccc +CLOUDINARY_API_SECRET=ccccccccccccc \ No newline at end of file diff --git a/apps/core/migrations/0001_initial.py b/apps/core/migrations/0001_initial.py new file mode 100644 index 0000000..4ae6dfd --- /dev/null +++ b/apps/core/migrations/0001_initial.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.3 on 2023-02-13 03:09 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Image', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('image_url', models.URLField()), + ], + ), + ] diff --git a/apps/core/models.py b/apps/core/models.py index 71a8362..467bfb4 100644 --- a/apps/core/models.py +++ b/apps/core/models.py @@ -1,3 +1,5 @@ from django.db import models # Create your models here. +class Image(models.Model): + image_url = models.URLField() \ No newline at end of file diff --git a/apps/core/urls.py b/apps/core/urls.py index 50f724b..ec725d0 100644 --- a/apps/core/urls.py +++ b/apps/core/urls.py @@ -1,8 +1,9 @@ from django.urls import path from django.contrib.auth import views -from apps.core.views import logout_view, UsersList +from apps.core.views import ImageViewSet, logout_view, UsersList urlpatterns = [ path('api/v1/auth/logout/', logout_view, name='logout'), path('api/users/', UsersList.as_view(), name='api_get_users'), + path('api/upload_image/', ImageViewSet.as_view({'post': 'upload_image'}), name='api_upload_image'), ] \ No newline at end of file diff --git a/apps/core/views.py b/apps/core/views.py index bd4be8d..66a30f1 100644 --- a/apps/core/views.py +++ b/apps/core/views.py @@ -1,14 +1,33 @@ +import os from rest_framework import generics from django.views.decorators.csrf import csrf_exempt - +from rest_framework import viewsets +from rest_framework.response import Response +from rest_framework.decorators import action +import requests +import json from django.contrib.auth.models import User +from apps.core.models import Image + from .serializers import UserSerializer from django.contrib.auth import logout from django.http import JsonResponse +from cloudinary import uploader +import cloudinary -@csrf_exempt +CLOUDINARY_CLOUD_NAME = os.environ.get('CLOUDINARY_CLOUD_NAME') +CLOUDINARY_API_KEY = os.environ.get('CLOUDINARY_API_KEY') +CLOUDINARY_API_SECRET = os.environ.get('CLOUDINARY_API_SECRET') +cloudinary.config( + cloud_name = CLOUDINARY_CLOUD_NAME, + api_key = CLOUDINARY_API_KEY, + api_secret = CLOUDINARY_API_SECRET, + secure = True +) + +@csrf_exempt def logout_view(request): logout(request) return JsonResponse({'message': 'Logged out successfully'}, status=200) @@ -17,4 +36,13 @@ class UsersList(generics.ListAPIView): serializer_class = UserSerializer def get_queryset(self): - return User.objects.all() \ No newline at end of file + return User.objects.all() + +class ImageViewSet(viewsets.ModelViewSet): + @action(detail=False, methods=['post']) + def upload_image(self, request): + image = request.data['file'] + result = uploader.upload(image, upload_preset="ml_default") + image_url = result['secure_url'] + Image.objects.create(image_url=image_url) + return Response({'image_url': image_url}) \ No newline at end of file diff --git a/frontend/src/components/EditProfile.vue b/frontend/src/components/EditProfile.vue index 3e75bff..172df94 100644 --- a/frontend/src/components/EditProfile.vue +++ b/frontend/src/components/EditProfile.vue @@ -2,6 +2,7 @@
+

Edit profile

@@ -28,4 +29,8 @@
- \ No newline at end of file + + + \ No newline at end of file diff --git a/frontend/src/components/Image.vue b/frontend/src/components/Image.vue new file mode 100644 index 0000000..eca5c01 --- /dev/null +++ b/frontend/src/components/Image.vue @@ -0,0 +1,26 @@ + + + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 647866f..34991cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,7 +23,7 @@ six==1.16.0 sqlparse==0.4.3 Twisted==22.10.0 txaio==22.2.1 -typing_extensions==4.4.0 -zope.interface==5.5.1 +typing_extensions==4.4.0zope.interface==5.5.1 +cloudinary djoser django-cors-headers \ No newline at end of file diff --git a/twikker/settings.py b/twikker/settings.py index b80de0b..fdb585b 100644 --- a/twikker/settings.py +++ b/twikker/settings.py @@ -147,3 +147,8 @@ # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + + +# cloudinary settings +from cloudinary.uploader import upload +from cloudinary.utils import cloudinary_url