diff --git a/castellum/contacts/forms.py b/castellum/contacts/forms.py index fd6a6e029482c0f02e2dfb73f12e9fb92b791bbe..9a898110ecfb262c804691eb66aca8e7ee963591 100644 --- a/castellum/contacts/forms.py +++ b/castellum/contacts/forms.py @@ -83,7 +83,7 @@ class ContactForm(forms.ModelForm): ] def __init__(self, *args, **kwargs): - user = kwargs.pop('user') + self.user = kwargs.pop('user') super().__init__(*args, **kwargs) self.address = self.get_address_form(**kwargs) @@ -93,7 +93,7 @@ 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.fields['guardians_remove'].choices = self.get_guardians_rm_choices(self.user) def get_address_form(self, **kwargs): address_kwargs = kwargs.copy() @@ -182,13 +182,14 @@ class ContactForm(forms.ModelForm): contact = Contact.objects.get(pk=contact.pk) if self.cleaned_data.get('guardians_pane') == 'guardians': - contact.guardians.remove(*[ - subject.contact for subject in self.cleaned_data.get('guardians_remove', []) - ]) - - contact.guardians.add(*[ - subject.contact for subject in self.cleaned_data.get('guardians_add', []) - ]) + if self.user.has_perm('subjects.change_subject'): + contact.guardians.remove(*[ + subject.contact for subject in self.cleaned_data.get('guardians_remove', []) + ]) + + contact.guardians.add(*[ + subject.contact for subject in self.cleaned_data.get('guardians_add', []) + ]) else: contact.guardians.set([]) diff --git a/castellum/contacts/templates/contacts/__guardian_item.html b/castellum/contacts/templates/contacts/__guardian_item.html index 82fbd160aa4063f5476f9079bad77e2d5e653d4c..77d72184230e69253adb5f0b68e489a24452891f 100644 --- a/castellum/contacts/templates/contacts/__guardian_item.html +++ b/castellum/contacts/templates/contacts/__guardian_item.html @@ -1,19 +1,22 @@ {% load i18n auth %}
+ {% has_perm 'subjects.view_subject' user as can_view_subject %} + {% has_perm 'subjects.change_subject' user as can_change_subject %}
{% translate 'Guardian' %}
{% if name == 'guardians_remove' %} - + {% else %} - + {% endif %}
- {% has_perm 'subjects.view_subject' user as can_view_subject %} {% if slug and can_view_subject %} {% translate 'Details' %} {% endif %} - - + {% if can_change_subject %} + + + {% endif %}
diff --git a/castellum/contacts/templates/contacts/contact_form.html b/castellum/contacts/templates/contacts/contact_form.html index bf47abf0625df22ad8d7d8cfbb1f9c59c33b4502..f77673078c3e986c56d2ee94f0a50c210ea45534 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 %} @@ -43,11 +43,14 @@ {% 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 %} + {% has_perm 'subjects.change_subject' user as can_change_subject %} + {% if can_change_subject %} + {% 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 %} - + + {% endif %}