This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/core/forms/groups.py

39 lines
1 KiB
Python
Raw Normal View History

2019-03-08 14:49:45 +00:00
"""passbook Core Group forms"""
from django import forms
2020-07-01 19:18:05 +00:00
from passbook.admin.fields import CodeMirrorWidget, YAMLField
2019-03-08 14:49:45 +00:00
from passbook.core.models import Group, User
class GroupForm(forms.ModelForm):
"""Group Form"""
members = forms.ModelMultipleChoiceField(
2019-12-31 11:51:16 +00:00
User.objects.all(),
required=False,
)
2019-03-08 14:49:45 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk:
self.initial["members"] = self.instance.users.values_list("pk", flat=True)
2019-03-08 14:49:45 +00:00
def save(self, *args, **kwargs):
instance = super().save(*args, **kwargs)
if instance.pk:
instance.users.clear()
instance.users.add(*self.cleaned_data["members"])
2019-03-08 14:49:45 +00:00
return instance
class Meta:
model = Group
fields = ["name", "is_superuser", "parent", "members", "attributes"]
2019-03-08 14:49:45 +00:00
widgets = {
2019-12-31 11:51:16 +00:00
"name": forms.TextInput(),
2020-07-01 19:18:05 +00:00
"attributes": CodeMirrorWidget,
}
field_classes = {
"attributes": YAMLField,
2019-03-08 14:49:45 +00:00
}