强制用户在Django Admin中保存之前提供配置文件

时间:2016-11-08 12:04:38

标签: django python-3.x django-models django-admin

我们试图强制用户在从django管理员保存之前提供个人资料。 这是我的个人资料模型

class UserProfile(models.Model):

    user=models.OneToOneField(AUTH_USER_MODEL,related_name='profile',primary_key=True)
    #other fields

    def get_user_info(user):
        return UserProfile.objects.get(user=user)
    @receiver(post_save, sender=AUTH_USER_MODEL)
    def create_profile_for_new_user(sender, created, instance, **kwargs):
        if created:
            profile = UserProfile(user=instance)
            profile.save()

使用authtools,因此用户个人资料发件人为AUTH_USER_MODEL。 如果我在管理员中添加新用户,即使他们未提供或填写个人资料,也会保存这些用户。 我想阻止它们被保存,直到它们填写配置文件字段。 有关如何做到这一点的任何见解? 这是我的admin.py

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import PasswordResetForm
from django.utils.crypto import get_random_string

from authtools.admin import NamedUserAdmin
from authtools.forms import UserCreationForm

User = get_user_model()


class UserCreationForm(UserCreationForm):
    """
    A UserCreationForm with optional password inputs.
    """

    def __init__(self, *args, **kwargs):
        super(UserCreationForm, self).__init__(*args, **kwargs)
        self.fields['password1'].required = False
        self.fields['password2'].required = False
        # If one field gets autocompleted but not the other, our 'neither
        # password or both password' validation will be triggered.
        self.fields['password1'].widget.attrs['autocomplete'] = 'off'
        self.fields['password2'].widget.attrs['autocomplete'] = 'off'

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = super(UserCreationForm, self).clean_password2()
        if bool(password1) ^ bool(password2):
            raise forms.ValidationError("Fill out both fields")
        return password2


class UserAdmin(NamedUserAdmin):
    """
    A UserAdmin that sends a password-reset email when creating a new user,
    unless a password was entered.
    """
    inlines = [ UserProfileInline, ]
    add_form = UserCreationForm
    add_fieldsets = (
        (None, {
            'description': (
                "Enter the new user's name and email address and click save."
                " The user will be emailed a link allowing them to login to"
                " the site and set their password."
            ),
            'fields': ('email', 'name',),
        }),
        ('Password', {
            'description': "Optionally, you may set the user's password here.",
            'fields': ('password1', 'password2'),
            'classes': ('collapse', 'collapse-closed'),
        }),
    )

    def save_model(self, request, obj, form, change):
        if not change and not obj.has_usable_password():
            # Django's PasswordResetForm won't let us reset an unusable
            # password. We set it above super() so we don't have to save twice.
            obj.set_password(get_random_string())
            reset_password = True
        else:
            reset_password = True

        super(UserAdmin, self).save_model(request, obj, form, change)

        if reset_password:
            reset_form = PasswordResetForm({'email': obj.email})
            assert reset_form.is_valid()
            reset_form.save(
                subject_template_name='registration/account_creation_subject.txt',
                email_template_name='registration/account_creation_email.html',
            )


admin.site.unregister(User)
admin.site.register(User, UserAdmin)

1 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法是创建自定义中间件并检查登录用户是否填写了配置文件。如果没有,中间件会将用户重定向到一个视图,在那里他可以填写个人资料。

示例:

class ProfileMiddleware(MiddlewareMixin):
    def __init__(self, get_response=None):
        self.get_response = get_response

    def process_view(self, request, view_func, view_args, view_kwargs):
        if not request.user.profile:
            return HttpResponseRedirect(reverse('create-profile'))
        return None

Haven未对代码进行测试,但它应该让您入门。这是最干净的方式,它也会强制用户填写个人资料,否则他无法在网站上打开页面。

希望这有帮助。