policies: Show grouped Dropdown for Target

This commit is contained in:
Jens Langhammer 2020-07-04 00:16:16 +02:00
parent d3b0992456
commit 16b966c16e
5 changed files with 70 additions and 4 deletions

View File

@ -0,0 +1,28 @@
# Generated by Django 3.0.7 on 2020-07-03 22:13
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("passbook_core", "0003_default_user"),
]
operations = [
migrations.AlterModelOptions(
name="application",
options={
"verbose_name": "Application",
"verbose_name_plural": "Applications",
},
),
migrations.AlterModelOptions(
name="user",
options={
"permissions": (("reset_user_password", "Reset Password"),),
"verbose_name": "User",
"verbose_name_plural": "Users",
},
),
]

View File

@ -71,6 +71,8 @@ class User(GuardianUserMixin, AbstractUser):
class Meta: class Meta:
permissions = (("reset_user_password", "Reset Password"),) permissions = (("reset_user_password", "Reset Password"),)
verbose_name = _("User")
verbose_name_plural = _("Users")
class Provider(models.Model): class Provider(models.Model):
@ -121,6 +123,11 @@ class Application(PolicyBindingModel):
def __str__(self): def __str__(self):
return self.name return self.name
class Meta:
verbose_name = _("Application")
verbose_name_plural = _("Applications")
class Source(PolicyBindingModel): class Source(PolicyBindingModel):
"""Base Authentication source, i.e. an OAuth Provider, SAML Remote or LDAP Server""" """Base Authentication source, i.e. an OAuth Provider, SAML Remote or LDAP Server"""

28
passbook/lib/widgets.py Normal file
View File

@ -0,0 +1,28 @@
"""Utility Widgets"""
from functools import partial
from itertools import groupby
from operator import attrgetter
from django.forms.models import ModelChoiceField, ModelChoiceIterator
class GroupedModelChoiceIterator(ModelChoiceIterator):
"""ModelChoiceField which groups objects by their verbose_name"""
def __iter__(self):
if self.field.empty_label is not None:
yield ("", self.field.empty_label)
queryset = self.queryset
# Can't use iterator() when queryset uses prefetch_related()
if not queryset._prefetch_related_lookups:
queryset = queryset.iterator()
# We can't use DB-level sorting as we sort by subclass
queryset = sorted(queryset, key=lambda x: x._meta.verbose_name)
for group, objs in groupby(queryset, key=lambda x: x._meta.verbose_name):
yield (group, [self.choice(obj) for obj in objs])
class GroupedModelChoiceField(ModelChoiceField):
"""ModelChoiceField which groups objects by their verbose_name"""
iterator = GroupedModelChoiceIterator

View File

@ -1,7 +1,9 @@
"""General fields""" """General fields"""
from django import forms from django import forms
from passbook.policies.models import PolicyBinding, PolicyBindingModel from passbook.lib.widgets import GroupedModelChoiceField
from passbook.policies.models import Policy, PolicyBinding, PolicyBindingModel
GENERAL_FIELDS = ["name"] GENERAL_FIELDS = ["name"]
GENERAL_SERIALIZER_FIELDS = ["pk", "name"] GENERAL_SERIALIZER_FIELDS = ["pk", "name"]
@ -10,10 +12,11 @@ GENERAL_SERIALIZER_FIELDS = ["pk", "name"]
class PolicyBindingForm(forms.ModelForm): class PolicyBindingForm(forms.ModelForm):
"""Form to edit Policy to PolicyBindingModel Binding""" """Form to edit Policy to PolicyBindingModel Binding"""
target = forms.ModelChoiceField( target = GroupedModelChoiceField(
queryset=PolicyBindingModel.objects.all().select_subclasses(), queryset=PolicyBindingModel.objects.all().select_subclasses(),
to_field_name="pbm_uuid", to_field_name="pbm_uuid",
) )
policy = GroupedModelChoiceField(queryset=Policy.objects.all().select_subclasses(),)
class Meta: class Meta:

View File

@ -210,7 +210,7 @@ paths:
parameters: parameters:
- name: pbm_uuid - name: pbm_uuid
in: path in: path
description: A UUID string identifying this application. description: A UUID string identifying this Application.
required: true required: true
type: string type: string
format: uuid format: uuid
@ -479,7 +479,7 @@ paths:
parameters: parameters:
- name: id - name: id
in: path in: path
description: A unique integer value identifying this user. description: A unique integer value identifying this User.
required: true required: true
type: integer type: integer
/flows/bindings/: /flows/bindings/: