From d604309ea90624fe3a340c8dc4f96bf5db4fdfaa Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Tue, 15 Nov 2011 22:49:48 +0100 Subject: [PATCH] Make sure the submitter is always listed as an organisation admin This fixes organisation submitting again, and also makes it impossible to orphan oneself from an organisation. --- pgweb/core/forms.py | 13 ++++++++++--- pgweb/util/helpers.py | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pgweb/core/forms.py b/pgweb/core/forms.py index 366fc006..a1aaffeb 100644 --- a/pgweb/core/forms.py +++ b/pgweb/core/forms.py @@ -14,7 +14,11 @@ class OrganisationForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(OrganisationForm, self).__init__(*args, **kwargs) - self.fields['remove_manager'].queryset = self.instance.managers.all() + if self.instance and self.instance.pk: + self.fields['remove_manager'].queryset = self.instance.managers.all() + else: + del self.fields['remove_manager'] + del self.fields['add_manager'] def clean_add_manager(self): if self.cleaned_data['add_manager']: @@ -39,14 +43,17 @@ class OrganisationForm(forms.ModelForm): def save(self, commit=True): model = super(OrganisationForm, self).save(commit=False) - if self.cleaned_data['add_manager']: + if self.cleaned_data.has_key('add_manager') and self.cleaned_data['add_manager']: model.managers.add(User.objects.get(email=self.cleaned_data['add_manager'])) - if self.cleaned_data['remove_manager']: + if self.cleaned_data.has_key('remove_manager') and self.cleaned_data['remove_manager']: for toremove in self.cleaned_data['remove_manager']: model.managers.remove(toremove) return model + def apply_submitter(self, model, User): + model.managers.add(User) + class MergeOrgsForm(forms.Form): merge_into = forms.ModelChoiceField(queryset=Organisation.objects.all()) merge_from = forms.ModelChoiceField(queryset=Organisation.objects.all()) diff --git a/pgweb/util/helpers.py b/pgweb/util/helpers.py index dfcbb238..d62ea42f 100644 --- a/pgweb/util/helpers.py +++ b/pgweb/util/helpers.py @@ -26,6 +26,11 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for r.submitter = request.user r.save() + # If we have a callback with the current user + if hasattr(form, 'apply_submitter'): + form.apply_submitter(r, request.user) + r.save() + # In case fixedfields include a manytomany field, we need to make sure the main form is saved first, # so we can access the field without an exception. if fixedfields: -- 2.39.5