This commit is contained in:
Mikan
2025-05-18 21:46:02 +03:00
parent d70c5f73f5
commit 30ef912c0b
4 changed files with 46 additions and 32 deletions

View File

@@ -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',)
search_fields = ('name',)

View File

@@ -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='Роль пользователя'),
),
]

View File

@@ -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='Роль пользователя'),
),
]

View File

@@ -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 = "Аккаунт"