Add custom admin view for the "Group" model

- Ref: https://github.com/Microdisseny/django-groupadmin-users
- Adds ability to edit users within a particular group from the group admin page!
This commit is contained in:
Oliver Walters 2020-10-03 17:37:20 +10:00
parent d81aa5c051
commit bedda66949
2 changed files with 66 additions and 2 deletions

View File

@ -138,6 +138,7 @@ INSTALLED_APPS = [
'part.apps.PartConfig',
'report.apps.ReportConfig',
'stock.apps.StockConfig',
'users.apps.UsersConfig',
# Third part add-ons
'django_filters', # Extended filter functionality

View File

@ -1,3 +1,66 @@
# -*- coding: utf-8 -*-
# from __future__ import unicode_literals
# from django.contrib import admin
from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.contrib.auth.models import Group
User = get_user_model()
class InvenTreeGroupAdminForm(forms.ModelForm):
class Meta:
model = Group
exclude = []
fields = [
'users',
'permissions',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
# Populate the users field with the current Group users.
self.fields['users'].initial = self.instance.user_set.all()
# Add the users field.
users = forms.ModelMultipleChoiceField(
queryset=User.objects.all(),
required=False,
widget=FilteredSelectMultiple('users', False),
label=_('Users'),
)
def save_m2m(self):
# Add the users to the Group.
# Deprecated in Django 1.10: Direct assignment to a reverse foreign key
# or many-to-many relation
self.instance.user_set.set(self.cleaned_data['users'])
def save(self, *args, **kwargs):
# Default save
instance = super().save()
# Save many-to-many data
self.save_m2m()
return instance
class RoleGroupAdmin(admin.ModelAdmin):
"""
Custom admin interface for the Group model
"""
form = InvenTreeGroupAdminForm
filter_horizontal = ['permissions']
admin.site.unregister(Group)
admin.site.register(Group, RoleGroupAdmin)