Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add profile picture in user model #66

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion insalan/user/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 12 additions & 1 deletion insalan/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down
9 changes: 9 additions & 0 deletions insalan/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"""
Expand All @@ -48,6 +56,7 @@ class Meta:
"is_staff",
"is_superuser",
"email",
"image",
"email_active",
"password",
"password_validation",
Expand Down