# Path: accounts/models.py
# class BlogUser(AbstractUser):
#     nickname = models.CharField('昵称', max_length=100, blank=True)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
#     source = models.CharField("创建来源", max_length=100, blank=True)
# 
#     def get_absolute_url(self):
#         return reverse(
#             'blog:author_detail', kwargs={
#                 'author_name': self.username})
# 
#     def __str__(self):
#         return self.email
# 
#     def get_full_url(self):
#         site = get_current_site().domain
#         url = "https://{site}{path}".format(site=site,
#                                             path=self.get_absolute_url())
#         return url
# 
#     class Meta:
#         ordering = ['-id']
#         verbose_name = "用户"
#         verbose_name_plural = verbose_name
#         get_latest_by = 'id'


from django import forms
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.contrib.auth.forms import UserChangeForm
from django.contrib.auth.forms import UsernameField
from django.utils.translation import gettext_lazy as _
from .models import BlogUser
    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("两次密码不一致")
        return password2

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.source = 'adminsite'
            user.save()
        return user


class BlogUserChangeForm(UserChangeForm):
    password = ReadOnlyPasswordHashField(
        label=_("Password"),
        help_text=_(
            "Raw passwords are not stored, so there is no way to see this "
            "user's password, but you can change the password using "
            "<a href=\"{}\">this form</a>."
        ),
    )
    email = forms.EmailField(label="Email", widget=forms.EmailInput)

    class Meta:

# Path: accounts/models.py
# class BlogUser(AbstractUser):
#     nickname = models.CharField('昵称', max_length=100, blank=True)
#     created_time = models.DateTimeField('创建时间', default=now)
#     last_mod_time = models.DateTimeField('修改时间', default=now)
#     source = models.CharField("创建来源", max_length=100, blank=True)
# 
#     def get_absolute_url(self):
#         return reverse(
#             'blog:author_detail', kwargs={
#                 'author_name': self.username})
# 
#     def __str__(self):
#         return self.email
# 
#     def get_full_url(self):
#         site = get_current_site().domain
#         url = "https://{site}{path}".format(site=site,
#                                             path=self.get_absolute_url())
#         return url
# 
#     class Meta:
#         ordering = ['-id']
#         verbose_name = "用户"
#         verbose_name_plural = verbose_name
#         get_latest_by = 'id'


# Path: accounts/admin.py
from django import forms
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.contrib.auth.forms import UserChangeForm
from django.contrib.auth.forms import UsernameField
from django.utils.translation import gettext_lazy as _
from .models import BlogUser

    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("两次密码不一致")
        return password2

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.source = 'adminsite'
            user.save()
        return user


class BlogUserChangeForm(UserChangeForm):
    password = ReadOnlyPasswordHashField(
        label=_("Password"),
        help_text=_(
            "Raw passwords are not stored, so there is no way to see this "
            "user's password, but you can change the password using "
            "<a href=\"{}\">this form</a>."
        ),
    )
    email = forms.EmailField(label="Email", widget=forms.EmailInput)

    class Meta:

        model = BlogUser