diff --git a/castellum/contacts/forms.py b/castellum/contacts/forms.py index fd6a6e029482c0f02e2dfb73f12e9fb92b791bbe..0947b0c790c0c3a5c5b5371df12987b5139e3a65 100644 --- a/castellum/contacts/forms.py +++ b/castellum/contacts/forms.py @@ -65,8 +65,6 @@ class ContactForm(forms.ModelForm): ('self', _('Full of age')), ('guardians', _('Has legal guardian')), ], widget=forms.RadioSelect) - guardians_remove = forms.ModelMultipleChoiceField(Subject.objects, required=False) - guardians_add = SubjectMultipleChoiceField(Subject.objects, required=False) class Meta: model = Contact @@ -93,7 +91,15 @@ class ContactForm(forms.ModelForm): elif any([self.instance.get_address(), self.instance.phone_number, self.instance.email]): self.fields['guardians_pane'].initial = 'self' - self.fields['guardians_remove'].choices = self.get_guardians_rm_choices(user) + self.guardians = self.get_guardians_rm_choices(user) + if user.has_perm('subjects.change_subject'): + self.fields['guardians_remove'] = forms.ModelMultipleChoiceField( + Subject.objects, required=False + ) + self.fields['guardians_remove'].choices = self.guardians + self.fields['guardians_add'] = SubjectMultipleChoiceField( + Subject.objects.filter(privacy_level__lte=user.get_privacy_level()), required=False + ) def get_address_form(self, **kwargs): address_kwargs = kwargs.copy() diff --git a/castellum/contacts/templates/contacts/contact_form.html b/castellum/contacts/templates/contacts/contact_form.html index bf47abf0625df22ad8d7d8cfbb1f9c59c33b4502..d0856e847951b54c15a5e44a473a6ab32673fa8a 100644 --- a/castellum/contacts/templates/contacts/contact_form.html +++ b/castellum/contacts/templates/contacts/contact_form.html @@ -1,5 +1,5 @@ {% extends view.base_template|default:"subjects/base.html" %} -{% load static i18n bootstrap4 %} +{% load static i18n auth bootstrap4 %} {% block title %} {% if object %} @@ -29,25 +29,42 @@
{% blocktranslate with count=form.guardians_blocked %}{{ count }} guardians not listed because of insufficient privacy level.{% endblocktranslate %}
{% endif %} - {% for widget in form.guardians_remove %} - {% include 'contacts/__guardian_item.html' with name=form.guardians_remove.name pk=widget.data.value label=widget.choice_label slug=widget.choice_label.subject.slug removed=widget.data.selected %} - {% endfor %} + {% has_perm 'subjects.change_subject' user as can_change_subject %} + {% if can_change_subject %} + + {% include 'contacts/__guardian_item.html' with name=form.guardians_add.name pk='{pk}' label='' removed=False %} + + + {% for widget in form.guardians_remove %} + {% include 'contacts/__guardian_item.html' with name=form.guardians_remove.name pk=widget.data.value label=widget.choice_label slug=widget.choice_label.subject.slug removed=widget.data.selected %} + {% endfor %} - {% for subject in form.cleaned_data.guardians_add %} - {% include 'contacts/__guardian_item.html' with name=form.guardians_add.name pk=subject.pk label=subject label=subject.contact.full_name removed=False %} - {% endfor %} + {% for subject in form.cleaned_data.guardians_add %} + {% include 'contacts/__guardian_item.html' with name=form.guardians_add.name pk=subject.pk label=subject label=subject.contact.full_name removed=False %} + {% endfor %} - + + {% else %} + {% has_perm 'subjects.view_subject' user as can_view_subject %} + {% for subject_id, contact in form.guardians %} +