change the users in the groupform

This commit is contained in:
Oskar Hahn 2012-08-11 11:36:55 +02:00
parent cdf160dfc3
commit 6795ccd25b
3 changed files with 41 additions and 16 deletions

View File

@ -47,14 +47,39 @@ class GroupForm(forms.ModelForm, CssClassMixin):
permissions = LocalizedModelMultipleChoiceField( permissions = LocalizedModelMultipleChoiceField(
queryset=Permission.objects.all(), label=_("Persmissions"), queryset=Permission.objects.all(), label=_("Persmissions"),
required=False) required=False)
users = forms.ModelMultipleChoiceField(
queryset=OpenSlidesUser.objects.all(),
label=_("Users"), required=False)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(GroupForm, self).__init__(*args, **kwargs) # Initial users
if kwargs.get('instance', None) is not None: if kwargs.get('instance', None) is not None:
self.fields['permissions'].initial = ( initial = kwargs.setdefault('initial', {})
[p.pk for p in kwargs['instance'].permissions.all()]) initial['users'] = [user.pk for user in kwargs['instance'].user_set.all()]
super(GroupForm, self).__init__(*args, **kwargs)
def save(self, commit=True):
instance = forms.ModelForm.save(self, False)
old_save_m2m = self.save_m2m
def save_m2m():
old_save_m2m()
instance.user_set.clear()
for user in self.cleaned_data['users']:
instance.user_set.add(user)
self.save_m2m = save_m2m
if commit:
instance.save()
self.save_m2m()
return instance
def clean_name(self): def clean_name(self):
# Do not allow to change the name "anonymous" or give another group
# this name
data = self.cleaned_data['name'] data = self.cleaned_data['name']
if self.instance.name.lower() == 'anonymous': if self.instance.name.lower() == 'anonymous':
# Editing the anonymous-user # Editing the anonymous-user

View File

@ -90,9 +90,10 @@ class OpenSlidesUser(User, PersonMixin):
return ('user_delete', [str(self.id)]) return ('user_delete', [str(self.id)])
def __unicode__(self): def __unicode__(self):
name = self.get_full_name() or _("No Name yet")
if self.name_surfix: if self.name_surfix:
return u"%s (%s)" % (self.get_full_name(), self.name_surfix) return u"%s (%s)" % (name, self.name_surfix)
return u"%s" % self.get_full_name() return u"%s" % name
class Meta: class Meta:
# Rename permissions # Rename permissions

View File

@ -357,16 +357,7 @@ class GroupOverviewView(ListView):
model = OpenSlidesGroup model = OpenSlidesGroup
class GroupEditMixin(object): class GroupCreateView(CreateView):
"""
Methodes for the GroupCreateView and GroupUpdateView.
"""
def get_form_class(self):
delete_default_permissions()
return self.form_class
class GroupCreateView(CreateView, GroupEditMixin):
""" """
Create a new group. Create a new group.
""" """
@ -378,8 +369,12 @@ class GroupCreateView(CreateView, GroupEditMixin):
success_url = 'user_group_overview' success_url = 'user_group_overview'
apply_url = 'user_group_edit' apply_url = 'user_group_edit'
def get(self, request, *args, **kwargs):
delete_default_permissions()
return super(GroupCreateView, self).get(request, *args, **kwargs)
class GroupUpdateView(UpdateView, GroupEditMixin):
class GroupUpdateView(UpdateView):
""" """
Update an existing group. Update an existing group.
""" """
@ -391,6 +386,10 @@ class GroupUpdateView(UpdateView, GroupEditMixin):
success_url = 'user_group_overview' success_url = 'user_group_overview'
apply_url = 'user_group_edit' apply_url = 'user_group_edit'
def get(self, request, *args, **kwargs):
delete_default_permissions()
return super(GroupUpdateView, self).get(request, *args, **kwargs)
class GroupDeleteView(DeleteView): class GroupDeleteView(DeleteView):
""" """