2019-03-08 14:49:45 +00:00
|
|
|
"""passbook Core Group forms"""
|
|
|
|
from django import forms
|
2019-03-10 17:34:09 +00:00
|
|
|
from django.contrib.admin.widgets import FilteredSelectMultiple
|
2019-03-08 14:49:45 +00:00
|
|
|
|
|
|
|
from passbook.core.models import Group, User
|
|
|
|
|
|
|
|
|
|
|
|
class GroupForm(forms.ModelForm):
|
|
|
|
"""Group Form"""
|
|
|
|
|
2019-03-10 17:06:06 +00:00
|
|
|
members = forms.ModelMultipleChoiceField(
|
2019-12-31 11:51:16 +00:00
|
|
|
User.objects.all(),
|
|
|
|
required=False,
|
|
|
|
widget=FilteredSelectMultiple("users", False),
|
|
|
|
)
|
2019-03-08 14:49:45 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if self.instance.pk:
|
2019-12-31 11:51:16 +00:00
|
|
|
self.initial["members"] = self.instance.user_set.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.user_set.clear()
|
2019-12-31 11:51:16 +00:00
|
|
|
instance.user_set.add(*self.cleaned_data["members"])
|
2019-03-08 14:49:45 +00:00
|
|
|
return instance
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = Group
|
2019-12-31 11:51:16 +00:00
|
|
|
fields = ["name", "parent", "members", "attributes"]
|
2019-03-08 14:49:45 +00:00
|
|
|
widgets = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"name": forms.TextInput(),
|
2019-03-08 14:49:45 +00:00
|
|
|
}
|