From eeebc2ea6e4b84c918f1f9c9d4590a12f0dd88dd Mon Sep 17 00:00:00 2001 From: Tudor Amariei Date: Thu, 22 Aug 2024 11:06:46 +0300 Subject: [PATCH] Update permissions for updating orgs/candidates --- backend/hub/forms.py | 57 ++++++++++++++----- .../hub/templates/hub/candidate/update.html | 11 +++- backend/hub/templates/hub/ngo/update.html | 7 +++ 3 files changed, 59 insertions(+), 16 deletions(-) diff --git a/backend/hub/forms.py b/backend/hub/forms.py index 74cf9426..ed99040e 100644 --- a/backend/hub/forms.py +++ b/backend/hub/forms.py @@ -9,9 +9,8 @@ from django_recaptcha.fields import ReCaptchaField from civil_society_vote.common.messaging import send_email -from hub.models import Candidate, City, Domain, FeatureFlag, Organization +from hub.models import Candidate, City, Domain, FLAG_CHOICES, FeatureFlag, Organization -# from django_crispy_bulma.widgets import EmailInput ORG_FIELD_ORDER = [ "name", @@ -138,15 +137,31 @@ def __init__(self, *args, **kwargs): except (ValueError, TypeError): pass # invalid input, fallback to empty queryset - if self.instance: - if self.instance.is_fully_editable: - for field_name in self.fields: - if field_name in Organization.required_fields(): - self.fields[field_name].required = True - else: - for field_name in self.fields: - if field_name in Organization.ngohub_fields(): - self.fields[field_name].disabled = True + if not self.instance: + return + + self._set_fields_permissions() + + def _set_fields_permissions(self): + # All the required fields for a fully editable organization should be required in votong + if self.instance.is_fully_editable: + for field_name in self.fields: + if field_name in Organization.required_fields(): + self.fields[field_name].required = True + + return + + # If registration is closed, updating the organization/candidate shouldn't be possible + if not FeatureFlag.flag_enabled(FLAG_CHOICES.enable_candidate_registration): + for field_name in self.fields: + self.fields[field_name].disabled = True + + return + + # Disable the fields that should be received from NGO Hub + for field_name in self.fields: + if field_name in Organization.ngohub_fields(): + self.fields[field_name].disabled = True def clean_email(self): email = self.cleaned_data.get("email") @@ -160,6 +175,13 @@ def clean_email(self): raise ValidationError(_("An organization with the same email address is already registered.")) return self.cleaned_data.get("email") + def save(self, commit=True): + if not FeatureFlag.flag_enabled(FLAG_CHOICES.enable_candidate_registration): + # This should not happen unless someone messes with the form code + raise PermissionDenied + + return super().save(commit) + class CandidateCommonForm(forms.ModelForm): field_order = [ @@ -206,7 +228,7 @@ def __init__(self, *args, **kwargs): self.initial["org"] = self.user.orgs.first().id - if FeatureFlag.flag_enabled("single_domain_round"): + if FeatureFlag.flag_enabled(FLAG_CHOICES.single_domain_round): self.fields["domain"].widget.attrs["disabled"] = True self.initial["domain"] = Domain.objects.first().id @@ -214,7 +236,7 @@ def clean_org(self): return self.user.orgs.first().id def clean_domain(self): - if FeatureFlag.flag_enabled("single_domain_round"): + if FeatureFlag.flag_enabled(FLAG_CHOICES.single_domain_round): return Domain.objects.first() return self.cleaned_data.get("domain") @@ -240,7 +262,7 @@ class Meta(CandidateCommonForm.Meta): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - if not FeatureFlag.flag_enabled("enable_candidate_registration"): + if not FeatureFlag.flag_enabled(FLAG_CHOICES.enable_candidate_registration): for key in self.fields.keys(): self.fields[key].widget.attrs["disabled"] = True @@ -251,8 +273,13 @@ def __init__(self, *args, **kwargs): for key in self.fields.keys(): self.fields[key].widget.attrs["required"] = True + # If registration is closed, updating the organization/candidate shouldn't be possible + if not FeatureFlag.flag_enabled(FLAG_CHOICES.enable_candidate_registration): + for field_name in self.fields: + self.fields[field_name].disabled = True + def save(self, commit=True): - if not FeatureFlag.flag_enabled("enable_candidate_registration"): + if not FeatureFlag.flag_enabled(FLAG_CHOICES.enable_candidate_registration): # This should not happen unless someone messes with the form code raise PermissionDenied diff --git a/backend/hub/templates/hub/candidate/update.html b/backend/hub/templates/hub/candidate/update.html index e5fdd667..01d93660 100644 --- a/backend/hub/templates/hub/candidate/update.html +++ b/backend/hub/templates/hub/candidate/update.html @@ -36,7 +36,16 @@

{% if not candidate.is_proposed %}Editează {% endif %}Candidatură

- Vezi profilul public al candidaturii +

+ Vezi profilul public al candidaturii + {% if not CANDIDATE_REGISTRATION_ENABLED and candidate.is_proposed %} + | + + Actualizarea datelor se poate face doar contactând + administratorii platformei + + {% endif %} +



diff --git a/backend/hub/templates/hub/ngo/update.html b/backend/hub/templates/hub/ngo/update.html index 4a9793f4..603e41cf 100644 --- a/backend/hub/templates/hub/ngo/update.html +++ b/backend/hub/templates/hub/ngo/update.html @@ -42,6 +42,13 @@

Profilul organizației

{% if organization.status == 'accepted' %} Vezi profilul public al organizației + {% if not CANDIDATE_REGISTRATION_ENABLED %} + | + + Actualizarea datelor se poate face doar contactând + administratorii platformei + + {% endif %}

{% endif %}