Merge pull request 'use modeliga avatar fieldni qoshildi' (#28) from feat/user into main
All checks were successful
Deploy to Production / build-and-deploy (push) Successful in 2m34s
All checks were successful
Deploy to Production / build-and-deploy (push) Successful in 2m34s
Reviewed-on: #28
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
from django.contrib.auth import admin
|
from django.contrib.auth import admin
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from django.utils.safestring import mark_safe
|
||||||
from unfold.admin import ModelAdmin
|
from unfold.admin import ModelAdmin
|
||||||
from unfold.forms import AdminPasswordChangeForm # UserCreationForm,
|
from unfold.forms import AdminPasswordChangeForm # UserCreationForm,
|
||||||
from unfold.forms import UserChangeForm
|
from unfold.forms import UserChangeForm
|
||||||
@@ -10,6 +11,7 @@ class CustomUserAdmin(admin.UserAdmin, ModelAdmin):
|
|||||||
# add_form = UserCreationForm
|
# add_form = UserCreationForm
|
||||||
form = UserChangeForm
|
form = UserChangeForm
|
||||||
list_display = (
|
list_display = (
|
||||||
|
"display_avatar",
|
||||||
"first_name",
|
"first_name",
|
||||||
"last_name",
|
"last_name",
|
||||||
"phone",
|
"phone",
|
||||||
@@ -17,7 +19,7 @@ class CustomUserAdmin(admin.UserAdmin, ModelAdmin):
|
|||||||
)
|
)
|
||||||
search_fields = ("phone", "first_name", "last_name", "username")
|
search_fields = ("phone", "first_name", "last_name", "username")
|
||||||
autocomplete_fields = ["groups", "user_permissions"]
|
autocomplete_fields = ["groups", "user_permissions"]
|
||||||
fieldsets = ((None, {"fields": ("phone",)}),) + (
|
fieldsets = ((None, {"fields": ("phone", "avatar",)}),) + (
|
||||||
(None, {"fields": ("username", "password")}),
|
(None, {"fields": ("username", "password")}),
|
||||||
(_("Personal info"), {"fields": ("first_name", "last_name", "email")}),
|
(_("Personal info"), {"fields": ("first_name", "last_name", "email")}),
|
||||||
(
|
(
|
||||||
@@ -36,6 +38,15 @@ class CustomUserAdmin(admin.UserAdmin, ModelAdmin):
|
|||||||
(_("Important dates"), {"fields": ("last_login", "date_joined")}),
|
(_("Important dates"), {"fields": ("last_login", "date_joined")}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def display_avatar(self, obj):
|
||||||
|
if obj.avatar:
|
||||||
|
return mark_safe(
|
||||||
|
f'<img src="{obj.avatar.url}" width="35" height="35" style="border-radius: 50%; object-fit: cover;" />'
|
||||||
|
)
|
||||||
|
return _("No Image")
|
||||||
|
|
||||||
|
display_avatar.short_description = _("Avatar")
|
||||||
|
|
||||||
|
|
||||||
class PermissionAdmin(ModelAdmin):
|
class PermissionAdmin(ModelAdmin):
|
||||||
list_display = ("name",)
|
list_display = ("name",)
|
||||||
|
|||||||
18
core/apps/accounts/migrations/0003_user_avatar.py
Normal file
18
core/apps/accounts/migrations/0003_user_avatar.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 5.2.7 on 2026-03-17 12:32
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('accounts', '0002_alter_user_role'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user',
|
||||||
|
name='avatar',
|
||||||
|
field=models.ImageField(blank=True, null=True, upload_to='avatars/'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -16,6 +16,7 @@ class User(auth_models.AbstractUser):
|
|||||||
choices=RoleChoice,
|
choices=RoleChoice,
|
||||||
default=RoleChoice.USER,
|
default=RoleChoice.USER,
|
||||||
)
|
)
|
||||||
|
avatar = models.ImageField(upload_to="avatars/", null=True, blank=True)
|
||||||
|
|
||||||
USERNAME_FIELD = "phone"
|
USERNAME_FIELD = "phone"
|
||||||
objects = UserManager()
|
objects = UserManager()
|
||||||
|
|||||||
@@ -19,5 +19,6 @@ class UserUpdateSerializer(serializers.ModelSerializer):
|
|||||||
model = get_user_model()
|
model = get_user_model()
|
||||||
fields = [
|
fields = [
|
||||||
"first_name",
|
"first_name",
|
||||||
"last_name"
|
"last_name",
|
||||||
|
"avatar"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from django.contrib.auth import get_user_model
|
|||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django_core import exceptions
|
from django_core import exceptions
|
||||||
from drf_spectacular.utils import extend_schema
|
from drf_spectacular.utils import extend_schema
|
||||||
from rest_framework import status, throttling, request
|
from rest_framework import status, throttling, request, parsers
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.exceptions import PermissionDenied
|
from rest_framework.exceptions import PermissionDenied
|
||||||
from rest_framework.viewsets import GenericViewSet
|
from rest_framework.viewsets import GenericViewSet
|
||||||
@@ -160,6 +160,11 @@ class ResetPasswordView(BaseViewSetMixin, GenericViewSet, UserService):
|
|||||||
@extend_schema(tags=["me"])
|
@extend_schema(tags=["me"])
|
||||||
class MeView(BaseViewSetMixin, GenericViewSet, UserService):
|
class MeView(BaseViewSetMixin, GenericViewSet, UserService):
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
parser_classes = (
|
||||||
|
parsers.MultiPartParser,
|
||||||
|
parsers.FormParser,
|
||||||
|
parsers.JSONParser,
|
||||||
|
)
|
||||||
|
|
||||||
def get_serializer_class(self):
|
def get_serializer_class(self):
|
||||||
match self.action:
|
match self.action:
|
||||||
|
|||||||
Reference in New Issue
Block a user