Skip to content

Commit

Permalink
Add inline field for users to add groups
Browse files Browse the repository at this point in the history
  • Loading branch information
tienne-B committed Jul 17, 2024
1 parent 38d7884 commit 0e2ff13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tabbycat/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class UserPermissionInline(admin.TabularInline):
fields = ('permission', 'tournament')


class MembershipInline(admin.TabularInline):
model = Membership
fields = ('group',)


class CustomUserLabelsMixin:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -30,33 +35,36 @@ def __init__(self, *args, **kwargs):


class UserChangeFormExtended(CustomUserLabelsMixin, UserChangeForm):
membership = ModelMultipleChoiceField(queryset=Membership.objects.all(), required=False)
group_set = ModelMultipleChoiceField(queryset=Group.objects.all(), required=False)


class UserCreationFormExtended(CustomUserLabelsMixin, UserCreationForm):
membership = ModelMultipleChoiceField(queryset=Membership.objects.all(), required=False)
group_set = ModelMultipleChoiceField(queryset=Group.objects.all(), required=False)


class UserAdmin(BaseUserAdmin):
list_display = ('username', 'email', 'is_active', 'is_staff', 'is_superuser')
inlines = (UserPermissionInline,)
inlines = (UserPermissionInline, MembershipInline)

fieldsets = ( # Hide groups and user permission fields
(_('Personal info'), {'fields': ('username', 'email', 'password')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'membership')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)

add_fieldsets = ( # Set permissions when creating
(None, {
'fields': ('username', 'password1', 'password2', 'email', 'is_staff', 'is_superuser', 'membership'),
'fields': ('username', 'password1', 'password2', 'email', 'is_staff', 'is_superuser', 'group_set'),
}),
)

add_form_template = 'admin/change_form.html'
add_form = UserCreationFormExtended
form = UserChangeFormExtended

def get_queryset(self, request):
return super().get_queryset(request).prefetch_related('group_set')


@admin.register(UserPermission)
class UserPermissionAdmin(admin.ModelAdmin):
Expand Down
1 change: 1 addition & 0 deletions tabbycat/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Group(models.Model):
tournament = models.ForeignKey('tournaments.Tournament', models.CASCADE, verbose_name=_("tournament"))
permissions = ChoiceArrayField(blank=True, default=list,
base_field=models.CharField(max_length=50, choices=Permission.choices), verbose_name=_("permissions"))
users = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Membership', related_name='group_set')

class Meta:
constraints = [UniqueConstraint(fields=['name', 'tournament'])]
Expand Down

0 comments on commit 0e2ff13

Please sign in to comment.