From a6511cfbff27fde72a4a912a5aecae91b6de10d0 Mon Sep 17 00:00:00 2001 From: Tudor Amariei Date: Tue, 12 Nov 2024 13:46:41 +0200 Subject: [PATCH] Add a method to connect an organization from NGO Hub --- backend/hub/admin.py | 41 +++++++++++++++++++++++++++++++++++++++-- backend/hub/forms.py | 28 +++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/backend/hub/admin.py b/backend/hub/admin.py index 6eee272c..3d978f43 100644 --- a/backend/hub/admin.py +++ b/backend/hub/admin.py @@ -21,7 +21,7 @@ from accounts.models import COMMITTEE_GROUP, User from civil_society_vote.common.messaging import send_email -from hub.forms import ImportCitiesForm +from hub.forms import ImportCitiesForm, OrganizationCreateFromNgohubForm from hub.models import ( COUNTIES, COUNTY_RESIDENCE, @@ -126,7 +126,7 @@ class OrganizationAdmin(admin.ModelAdmin): autocomplete_fields = ["city"] list_per_page = 20 - inlines = (OrganizationUsersInline,) + inlines = (OrganizationUsersInline, OrganizationCandidatesInline) actions = (update_organizations,) @@ -187,6 +187,8 @@ class OrganizationAdmin(admin.ModelAdmin): ) def has_add_permission(self, request): + if request.user.is_superuser: + return True return False def has_delete_permission(self, request, obj=None): @@ -227,6 +229,41 @@ def get_voting_domain(self, obj: Organization): get_voting_domain.short_description = _("voting domain") + def get_readonly_fields(self, request, obj=None): + if obj and obj.ngohub_org_id: + return ["ngohub_org_id"] + list(Organization.ngohub_fields()) + + return [] + + def get_form(self, request, obj=None, **kwargs): + if not obj and not kwargs["change"] and request.user.is_superuser: + kwargs["fields"] = list(OrganizationCreateFromNgohubForm().fields.keys()) + return OrganizationCreateFromNgohubForm + + return super().get_form(request, obj, **kwargs) + + def get_fieldsets(self, request, obj=None): + if not obj and request.user.is_superuser: + return ( + ( + _("Identification"), + { + "fields": ( + "ngohub_org_id", + "user_id", + ) + }, + ), + ) + + return self.fieldsets + + def get_inlines(self, request, obj=None): + if not obj and request.user.is_superuser: + return [] + + return self.inlines + class CandidateVoteInline(admin.TabularInline): model = CandidateVote diff --git a/backend/hub/forms.py b/backend/hub/forms.py index 69d0a224..3e85f8e0 100644 --- a/backend/hub/forms.py +++ b/backend/hub/forms.py @@ -8,10 +8,10 @@ from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ from django_recaptcha.fields import ReCaptchaField +from sentry_sdk import capture_message from civil_society_vote.common.messaging import send_email from hub.models import FLAG_CHOICES, PHASE_CHOICES, Candidate, City, Domain, FeatureFlag, Organization -from sentry_sdk import capture_message UserModel = get_user_model() @@ -112,6 +112,32 @@ def clean_accept_terms_and_conditions(self): return self.cleaned_data.get("accept_terms_and_conditions") +class OrganizationCreateFromNgohubForm(forms.ModelForm): + user_id = forms.IntegerField(min_value=1, required=True) + + class Meta: + model = Organization + fields = [ + "ngohub_org_id", + ] + + def save_m2m(self): + pass + + def save(self, commit=True): + user_id = self.cleaned_data.get("user_id") + ngohub_org_id = self.cleaned_data.get("ngohub_org_id") + + if Organization.objects.filter(ngohub_org_id=ngohub_org_id).exists(): + raise ValidationError(_("Organization already exists.")) + + user = UserModel.objects.get(pk=user_id) + organization = Organization.objects.create(ngohub_org_id=ngohub_org_id) + organization.users.add(user) + + return organization + + class OrganizationUpdateForm(forms.ModelForm): field_order = ORG_FIELD_ORDER