Skip to content

Commit

Permalink
Add a method to connect an organization from NGO Hub
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Nov 12, 2024
1 parent ed4bb00 commit a6511cf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
41 changes: 39 additions & 2 deletions backend/hub/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -126,7 +126,7 @@ class OrganizationAdmin(admin.ModelAdmin):
autocomplete_fields = ["city"]
list_per_page = 20

inlines = (OrganizationUsersInline,)
inlines = (OrganizationUsersInline, OrganizationCandidatesInline)

actions = (update_organizations,)

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion backend/hub/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a6511cf

Please sign in to comment.