From a860faad3117bec2e9a93506866d3ee3d440643c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=93=D0=BE=D0=B2=D0=BE?= =?UTF-8?q?=D1=80=D0=BB=D0=B8=D0=B2=D1=8B=D1=85?= Date: Sun, 15 Oct 2023 23:01:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D0=B5=D0=BB=D1=8C=20=D1=8E=D0=B7=D0=B5=D1=80=D0=B0?= =?UTF-8?q?,=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B8=D0=BB=20=D0=B5?= =?UTF-8?q?=D0=BC=D1=83=20=D0=B0=D0=B4=D0=BC=D0=B8=D0=BD=D0=BA=D1=83.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/healthy_food/settings.py | 2 + backend/users/admin.py | 36 +++++++++++++- backend/users/models.py | 82 +++++++++++++++++++++++++++++++- 3 files changed, 116 insertions(+), 4 deletions(-) diff --git a/backend/healthy_food/settings.py b/backend/healthy_food/settings.py index b4feec6..65c8a2f 100644 --- a/backend/healthy_food/settings.py +++ b/backend/healthy_food/settings.py @@ -141,3 +141,5 @@ # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +AUTH_USER_MODEL = 'users.User' diff --git a/backend/users/admin.py b/backend/users/admin.py index 4185d36..abf0009 100644 --- a/backend/users/admin.py +++ b/backend/users/admin.py @@ -1,3 +1,35 @@ -# from django.contrib import admin +from django.contrib import admin -# Register your models here. +from .models import User + + +class UserAdmin(admin.ModelAdmin): + fields = [ + 'username', + 'is_staff', + 'is_active', + 'role', + 'first_name', + 'last_name', + 'email', + 'date_joined', + 'last_login', + 'location', + 'birth_date', + ] + + list_display = [ + 'id', + 'username', + 'is_staff', + 'is_active', + 'role', + 'email', + 'date_joined', + 'last_login', + ] + search_fields = ('role',) + ordering = ('id',) + + +admin.site.register(User, UserAdmin) diff --git a/backend/users/models.py b/backend/users/models.py index 0b4331b..fe9f592 100644 --- a/backend/users/models.py +++ b/backend/users/models.py @@ -1,3 +1,81 @@ -# from django.db import models +from django.contrib.auth.models import AbstractUser +from django.db import models +from django.utils import timezone -# Create your models here. + +class User(AbstractUser): + """ + Расширение встроенной модели User.""" + USER = 'user' + MODERATOR = 'moderator' + ADMIN = 'admin' + + CHOISES = [ + (USER, 'Аутентифицированный пользователь'), + (MODERATOR, 'Модератор'), + (ADMIN, 'Администратор'), + ] + + username: str = models.CharField( + 'Username', + unique=True, + max_length=150, + ) + email: str = models.EmailField( + 'E-mail address', + unique=True, + blank=False, + max_length=254, + ) + role: str = models.CharField( + max_length=9, + choices=CHOISES, + default='user' + ) + first_name: str = models.CharField( + 'first name', + max_length=150, + blank=True + ) + last_name: str = models.CharField( + 'last name', + max_length=150, + blank=True + ) + date_joined = models.DateTimeField( + "date joined", + default=timezone.now + ) + last_login = models.DateTimeField( + 'last_login', + blank=True, + null=True + ) + location: str = models.CharField( + 'your city', + max_length=30, + blank=True + ) + birth_date = models.DateField( + 'birth_date', + null=True, + blank=True + ) + + class Meta: + ordering = ('id',) + + def __str__(self): + return self.username + + @property + def is_moderator(self): + return self.role == User.MODERATOR + + @property + def is_admin(self): + return self.role == User.ADMIN + + @property + def is_user(self): + return self.role == User.USER