post save user is inactive based on assigned user_group - Django Allauth

依然范特西╮ 提交于 2021-01-29 06:10:56

问题


I have two different user groups group_a and group_b. On register a user can choose the group he/she belongs to. I am using Django Allauth to handle all the user stuff so I've made a custom account adapter to handle the extra logic:

custom_adapter.py

class UserAccountAdapter(DefaultAccountAdapter):
    def save_user(self, request, user, form, commit=True):

        user = super(UserAccountAdapter, self).save_user(request, user, form, commit=False)
        user.voornaam = form.cleaned_data.get('first_name')
        user.achternaam = form.cleaned_data.get('last_name')
        user.user_group = form.cleaned_data.get('user_group')
        user.save()
        user.groups.add(user.user_group)
        user.save()
        if user.groups == 'group_b':
            user.is_active = False
        else:
            user.is_active = True
        user.save()
        return user

On register the user gets assigned to the group it selects as intended. Afterwards I want to assign user.is_active = False to the people who select group_b. In the above example I am using 3 save methods (I think this is way to much under DRY principle) but I tried every possible combination. When I am printing out the user.groups it is set to None after every save method in the above example. But it still gets assigned the right way when i look in the admin panel.

What is the best method to tackle this?

**EDIT

I've added my User model below for extra clearance:

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=254, unique=True)
    voornaam = models.CharField(max_length=254, null=True, blank=True)
    achternaam = models.CharField(max_length=254, null=True, blank=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    last_login = models.DateTimeField(null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

回答1:


Fixed it!

I was missing the filter attribute to filter on the name of the group and was missing the .exists().

For anyone interested, below code did the trick:

class UserAccountAdapter(DefaultAccountAdapter):
    def save_user(self, request, user, form, commit=True):

        user = super(UserAccountAdapter, self).save_user(request, user, form, commit=False)
        user.voornaam = form.cleaned_data.get('first_name')
        user.achternaam = form.cleaned_data.get('last_name')
        user.user_group = form.cleaned_data.get('user_group')
        user.save()
        user.groups.add(user.user_group)
        if user.groups.filter(name='zakelijk').exists():
            user.is_active = False
        else:
            user.is_active = True
        user.save()
        return user


来源:https://stackoverflow.com/questions/63160354/post-save-user-is-inactive-based-on-assigned-user-group-django-allauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!