ajout d'upload d'image

This commit is contained in:
Guamss 2025-12-06 21:54:47 +01:00
parent 7c4a1301cb
commit 418e50002f
6 changed files with 36 additions and 3 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
.pydevproject .pydevproject
poetry.lock poetry.lock
.env .env
media/*

View File

@ -27,11 +27,15 @@ SECRET_KEY = os.getenv("DJANGO_SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = True
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
ALLOWED_HOSTS = [ ALLOWED_HOSTS = [
'localhost', 'localhost',
'127.0.0.1' '127.0.0.1'
] ]
CORS_ALLOWED_ORIGINS = [ CORS_ALLOWED_ORIGINS = [
"http://localhost:5173", "http://localhost:5173",
'http://127.0.0.1:5173' 'http://127.0.0.1:5173'
@ -57,6 +61,7 @@ AUTH_USER_MODEL = 'books.CustomUser'
MIDDLEWARE = [ MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware', 'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',

View File

@ -18,8 +18,10 @@ from django.contrib import admin
from django.urls import path from django.urls import path
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
from api import settings
from books import views from books import views
from books.views import UserProfileView, BookListCreateView from books.views import UserProfileView, BookListCreateView
from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
@ -27,5 +29,8 @@ urlpatterns = [
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('api/profile', UserProfileView.as_view(), name='user_profile'), path('api/profile', UserProfileView.as_view(), name='user_profile'),
path('api/books', BookListCreateView.as_view(), name='book_list_create'), path('api/books', BookListCreateView.as_view(), name='book_list_create'),
path('api/users/<int:user_id>', views.find_user_by_id, name='user-detail') path('api/users/<int:user_id>', views.find_user_by_id, name='user-detail'),
] ]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View File

@ -1,9 +1,22 @@
import os
from uuid import uuid4
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.core.validators import MaxValueValidator from django.core.validators import MaxValueValidator
def upload_avatar(instance, filename):
ext = filename.split('.')[-1]
filename = '{}.{}'.format(uuid4().hex, ext)
return os.path.join('avatars/', filename)
def upload_illustration(instance, filename):
ext = filename.split('.')[-1]
filename = '{}.{}'.format(uuid4().hex, ext)
return os.path.join('illustrations/', filename)
class Book(models.Model): class Book(models.Model):
PLAN_TO_READ = 'PLAN' PLAN_TO_READ = 'PLAN'
READING = 'READING' READING = 'READING'
@ -32,6 +45,7 @@ class Book(models.Model):
) )
title = models.CharField(max_length=255) title = models.CharField(max_length=255)
author = models.CharField(max_length=255) author = models.CharField(max_length=255)
illustration = models.ImageField(upload_to=upload_illustration)
state = models.CharField( state = models.CharField(
_('state'), _('state'),
max_length=10, max_length=10,
@ -47,5 +61,7 @@ class Book(models.Model):
class CustomUser(AbstractUser): class CustomUser(AbstractUser):
avatar = models.ImageField(upload_to=upload_avatar)
def __str__(self): def __str__(self):
return self.username return self.username

View File

@ -23,7 +23,6 @@ class BookListCreateView(APIView):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['GET']) @api_view(['GET'])
def find_user_by_id(request, user_id): def find_user_by_id(request, user_id):
try: try:
@ -32,7 +31,10 @@ def find_user_by_id(request, user_id):
books_serializer = BookSerializer(books_queryset, many=True) books_serializer = BookSerializer(books_queryset, many=True)
return Response({ return Response({
'username': user.username, 'username': user.username,
'firstName': user.first_name,
'lastName': user.last_name,
'email': user.email, 'email': user.email,
'profilePicture': user.avatar.url if user.avatar else '',
'books': books_serializer.data, 'books': books_serializer.data,
}) })
@ -48,6 +50,9 @@ class UserProfileView(APIView):
books_serializer = BookSerializer(books_queryset, many=True) books_serializer = BookSerializer(books_queryset, many=True)
return Response({ return Response({
'username': user.username, 'username': user.username,
'firstName': user.first_name,
'lastName': user.last_name,
'email': user.email, 'email': user.email,
'profilePicture': user.avatar.url if user.avatar else '',
'books': books_serializer.data, 'books': books_serializer.data,
}) })

View File

@ -15,3 +15,4 @@ django-cors-headers = "^4.9.0"
psycopg = "^3.1" psycopg = "^3.1"
gunicorn = "^22.0" gunicorn = "^22.0"
dotenv = "^0.9.9" dotenv = "^0.9.9"
pillow = "^12.0.0"