mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +00:00
users: Minor refactor when creating django groups
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
parent
526a3018e4
commit
b36d4419c4
@ -50,6 +50,15 @@ class ValidNewUsernameCheckMixin:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def _create_django_groups():
|
||||||
|
"""Ensure that all groups are present in the Django's group table."""
|
||||||
|
group_choices = UsersAndGroups.get_group_choices()
|
||||||
|
for group_name, _label in group_choices:
|
||||||
|
Group.objects.get_or_create(name=group_name)
|
||||||
|
|
||||||
|
return group_choices
|
||||||
|
|
||||||
|
|
||||||
class GroupsFieldMixin:
|
class GroupsFieldMixin:
|
||||||
"""Mixin to set common properties for the group field."""
|
"""Mixin to set common properties for the group field."""
|
||||||
|
|
||||||
@ -58,9 +67,7 @@ class GroupsFieldMixin:
|
|||||||
|
|
||||||
Also ensure that all the groups are created in django.
|
Also ensure that all the groups are created in django.
|
||||||
"""
|
"""
|
||||||
group_choices = dict(UsersAndGroups.get_group_choices())
|
groups_dict = dict(_create_django_groups())
|
||||||
for group in group_choices:
|
|
||||||
Group.objects.get_or_create(name=group)
|
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
@ -74,8 +81,8 @@ class GroupsFieldMixin:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
# Show choices only from groups declared by apps.
|
# Show choices only from groups declared by apps.
|
||||||
if group_name in group_choices:
|
if group_name in groups_dict:
|
||||||
label = group_choices[group_name]
|
label = groups_dict[group_name]
|
||||||
if group_name == 'admin' and self.is_last_admin_user:
|
if group_name == 'admin' and self.is_last_admin_user:
|
||||||
label = {'label': label, 'disabled': True}
|
label = {'label': label, 'disabled': True}
|
||||||
|
|
||||||
@ -399,9 +406,7 @@ class FirstBootForm(ValidNewUsernameCheckMixin, auth.forms.UserCreationForm):
|
|||||||
_('Failed to add new user to admin group: {error}'.format(
|
_('Failed to add new user to admin group: {error}'.format(
|
||||||
error=error)))
|
error=error)))
|
||||||
|
|
||||||
# Create initial Django groups
|
_create_django_groups()
|
||||||
for group_choice in UsersAndGroups.get_group_choices():
|
|
||||||
auth.models.Group.objects.get_or_create(name=group_choice[0])
|
|
||||||
|
|
||||||
admin_group = auth.models.Group.objects.get(name='admin')
|
admin_group = auth.models.Group.objects.get(name='admin')
|
||||||
admin_group.user_set.add(user)
|
admin_group.user_set.add(user)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user