diff --git a/insalan/user/admin.py b/insalan/user/admin.py index 143945c6..26ad9f08 100644 --- a/insalan/user/admin.py +++ b/insalan/user/admin.py @@ -3,5 +3,12 @@ from django.contrib.auth.admin import UserAdmin from .models import User -admin.site.register(User, UserAdmin) +class CustomUserAdmin(UserAdmin): + fieldsets = UserAdmin.fieldsets + ( + ("Image", { + 'fields': ('image',), + }), + ) + +admin.site.register(User, CustomUserAdmin) # Register your models here. diff --git a/insalan/user/models.py b/insalan/user/models.py index 2fdbc37e..551e3155 100644 --- a/insalan/user/models.py +++ b/insalan/user/models.py @@ -17,7 +17,7 @@ from django.utils import timezone from django.utils.http import urlencode from django.utils.translation import gettext_lazy as _ - +from django.core.validators import FileExtensionValidator class UserManager(BaseUserManager): """ @@ -39,6 +39,7 @@ def create_user( user = self.model( email=self.normalize_email(email), username=username, + image=None, date_joined=timezone.make_aware(datetime.now()), **extra_fields ) @@ -80,6 +81,16 @@ def __init__(self, *args, **kwargs): USERNAME_FIELD = "username" EMAIL_FIELD = "email" + image = models.FileField( + verbose_name=_("photo de profil"), + blank=True, + null=True, + upload_to="profile-pictures", + validators=[ + FileExtensionValidator(allowed_extensions=["png", "jpg", "jpeg", "svg"]) + ], + ) + email = models.EmailField( verbose_name=_("Courriel"), max_length=255, unique=True, blank=False ) diff --git a/insalan/user/serializers.py b/insalan/user/serializers.py index c5e8a2fe..cc9b7b0d 100644 --- a/insalan/user/serializers.py +++ b/insalan/user/serializers.py @@ -7,6 +7,8 @@ from django.utils.translation import gettext_lazy as _ from rest_framework import serializers from rest_framework.validators import UniqueValidator +from django.core.validators import FileExtensionValidator + from .models import User, UserMailer @@ -35,6 +37,12 @@ class UserRegisterSerializer(serializers.ModelSerializer): first_name = serializers.CharField(max_length=50, required=False) last_name = serializers.CharField(max_length=50, required=False) password_validation = serializers.CharField(write_only=True, required=True) + image = serializers.FileField( + required=False, + validators=[ + FileExtensionValidator(allowed_extensions=["png", "jpg", "jpeg", "svg"]) + ], + ) class Meta: """Meta class, used to set parameters""" @@ -48,6 +56,7 @@ class Meta: "is_staff", "is_superuser", "email", + "image", "email_active", "password", "password_validation",