Skip to content

Commit

Permalink
Update permissions for updating orgs/candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Aug 22, 2024
1 parent 2eb12a2 commit eeebc2e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
57 changes: 42 additions & 15 deletions backend/hub/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")
Expand All @@ -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 = [
Expand Down Expand Up @@ -206,15 +228,15 @@ 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

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")

Expand All @@ -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

Expand All @@ -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

Expand Down
11 changes: 10 additions & 1 deletion backend/hub/templates/hub/candidate/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ <h2 class="title border-b uppercase">
{% if not candidate.is_proposed %}Editează {% endif %}Candidatură
</h2>

<a href="{% url 'candidate-detail' candidate.id %}">Vezi profilul public al candidaturii</a>
<p>
<a href="{% url 'candidate-detail' candidate.id %}">Vezi profilul public al candidaturii</a>
{% if not CANDIDATE_REGISTRATION_ENABLED and candidate.is_proposed %}
|
<strong>
Actualizarea datelor se poate face doar contactând
<a href="mailto:{{ contact_email }}">administratorii platformei</a>
</strong>
{% endif %}
</p>
<br>
<br>

Expand Down
7 changes: 7 additions & 0 deletions backend/hub/templates/hub/ngo/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ <h2 class="title border-b uppercase">Profilul organizației</h2>

{% if organization.status == 'accepted' %}
<a href="{% url 'ngo-detail' organization.id %}">Vezi profilul public al organizației</a>
{% if not CANDIDATE_REGISTRATION_ENABLED %}
|
<strong>
Actualizarea datelor se poate face doar contactând
<a href="mailto:{{ contact_email }}">administratorii platformei</a>
</strong>
{% endif %}
<br><br>
{% endif %}

Expand Down

0 comments on commit eeebc2e

Please sign in to comment.