From 30ef912c0b01a1e71440325e0ff5faca87db87a7 Mon Sep 17 00:00:00 2001 From: Mikan <72257910+Mikan-DS@users.noreply.github.com> Date: Sun, 18 May 2025 21:46:02 +0300 Subject: [PATCH] users --- users/admin.py | 38 ++++----------------- users/migrations/0002_alter_account_role.py | 19 +++++++++++ users/migrations/0003_alter_account_role.py | 19 +++++++++++ users/models.py | 2 +- 4 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 users/migrations/0002_alter_account_role.py create mode 100644 users/migrations/0003_alter_account_role.py diff --git a/users/admin.py b/users/admin.py index 8d40910..a3d05ef 100644 --- a/users/admin.py +++ b/users/admin.py @@ -1,52 +1,27 @@ from django import forms from django.contrib import admin from django.contrib.auth.admin import UserAdmin as BaseUserAdmin +from django.contrib.auth.forms import UserCreationForm, UserChangeForm from django.utils.translation import gettext_lazy as _ from .models import Account, Role -class AccountChangeForm(forms.ModelForm): - password = forms.CharField( - label=_("Password"), - widget=forms.PasswordInput, - required=False, - help_text=_("Raw passwords are not stored, so there is no way to see this user's password.") - ) +class AccountChangeForm(UserChangeForm): + class Meta: model = Account fields = '__all__' - def save(self, commit=True): - password = self.cleaned_data.get('password') - if password: - self.instance.set_password(password) - return super().save(commit=commit) -class AccountCreationForm(forms.ModelForm): - password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput) - password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput) +class AccountCreationForm(UserCreationForm): class Meta: model = Account fields = ('login',) - def clean_password2(self): - password1 = self.cleaned_data.get("password1") - password2 = self.cleaned_data.get("password2") - if password1 and password2 and password1 != password2: - raise forms.ValidationError(_("Passwords don't match")) - return password2 - - def save(self, commit=True): - user = super().save(commit=False) - user.set_password(self.cleaned_data["password1"]) - if commit: - user.save() - return user - @admin.register(Account) class AccountAdmin(BaseUserAdmin): @@ -74,7 +49,7 @@ class AccountAdmin(BaseUserAdmin): }), ) - @admin.display(description=_('Full name')) + @admin.display(description=_('ФИО')) def full_name(self, obj): return str(obj) @@ -82,4 +57,5 @@ class AccountAdmin(BaseUserAdmin): @admin.register(Role) class RoleAdmin(admin.ModelAdmin): list_display = ('name',) - search_fields = ('name',) \ No newline at end of file + search_fields = ('name',) + diff --git a/users/migrations/0002_alter_account_role.py b/users/migrations/0002_alter_account_role.py new file mode 100644 index 0000000..e81b322 --- /dev/null +++ b/users/migrations/0002_alter_account_role.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.21 on 2025-05-18 18:30 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='role', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='users.role', unique=True, verbose_name='Роль пользователя'), + ), + ] diff --git a/users/migrations/0003_alter_account_role.py b/users/migrations/0003_alter_account_role.py new file mode 100644 index 0000000..ad5896a --- /dev/null +++ b/users/migrations/0003_alter_account_role.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.21 on 2025-05-18 18:39 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0002_alter_account_role'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='role', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='users.role', verbose_name='Роль пользователя'), + ), + ] diff --git a/users/models.py b/users/models.py index 4758e2d..8bce7f6 100644 --- a/users/models.py +++ b/users/models.py @@ -46,7 +46,7 @@ class Account(ContactMixin, AbstractBaseUser, PermissionsMixin): objects = AccountManager() def __str__(self): - return self.login + return super().__str__() or self.login class Meta: verbose_name = "Аккаунт"