Skip to content

Commit

Permalink
fix the ngo presentation form
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Jan 30, 2025
1 parent a8e8c90 commit 0902f32
Show file tree
Hide file tree
Showing 10 changed files with 502 additions and 310 deletions.
48 changes: 48 additions & 0 deletions backend/donations/forms/ngo_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django import forms
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from localflavor.generic.forms import IBANFormField
from localflavor.ro.forms import ROCIFField


class NgoPresentationForm(forms.Form):
is_accepting_forms = forms.BooleanField(label=_("Is accepting forms"), required=False)

name = forms.CharField(label=_("Name"), max_length=255, required=True)
cif = ROCIFField(label=_("CUI/CIF"), required=True)
logo = forms.ImageField(label=_("Logo"), required=False)
website = forms.URLField(label=_("Website"), required=False)

contact_email = forms.EmailField(label=_("Contact email"), required=True)
display_email = forms.BooleanField(label=_("Display email"), required=False)

contact_phone = forms.CharField(label=_("Contact phone"), max_length=20, required=False)
display_phone = forms.BooleanField(label=_("Display phone"), required=False)

address = forms.CharField(label=_("Address"), max_length=255, required=True)
locality = forms.CharField(label=_("Locality"), max_length=255, required=False)
county = forms.ChoiceField(label=_("County"), choices=settings.FORM_COUNTIES_CHOICES, required=True)
active_region = forms.ChoiceField(label=_("Active region"), choices=settings.FORM_COUNTIES_CHOICES, required=True)

def __init__(self, *args, **kwargs):
is_fully_editable = kwargs.pop("is_fully_editable", True)

super().__init__(*args, **kwargs)

if not is_fully_editable:
self.fields["name"].widget.attrs["disabled"] = True
self.fields["cif"].widget.attrs["disabled"] = True
self.fields["logo"].widget.attrs["disabled"] = True
self.fields["website"].widget.attrs["disabled"] = True
self.fields["contact_email"].widget.attrs["disabled"] = True
self.fields["contact_phone"].widget.attrs["disabled"] = True
self.fields["address"].widget.attrs["disabled"] = True
self.fields["locality"].widget.attrs["disabled"] = True
self.fields["county"].widget.attrs["disabled"] = True
self.fields["active_region"].widget.attrs["disabled"] = True


class NgoFormForm(forms.Form):
slug = forms.SlugField(label=_("Slug"), max_length=50, required=True)
description = forms.CharField(label=_("Description"), widget=forms.Textarea, required=True)
iban = IBANFormField(label=_("IBAN"), include_countries=("RO",), required=True)
24 changes: 24 additions & 0 deletions backend/donations/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.core.exceptions import BadRequest, PermissionDenied
from django.core.files import File
from django.core.management import call_command
from django.db.models import QuerySet
from django.http import Http404, HttpResponse, HttpResponseBadRequest, JsonResponse
from django.shortcuts import redirect
from django.urls import reverse, reverse_lazy
Expand Down Expand Up @@ -76,6 +77,29 @@ class CheckNgoUrl(BaseTemplateView):
def get(self, request, ngo_url, *args, **kwargs):
return self.validate_ngo_slug(request.user, ngo_url)

@classmethod
def check_slug_is_blocked(cls, slug):
if slug.lower() in cls.ngo_url_block_list:
return True

return False

@classmethod
def check_slug_is_reused(cls, slug, user):
ngo_queryset: QuerySet[Ngo] = Ngo.objects

try:
if user.ngo:
ngo_queryset = ngo_queryset.exclude(id=user.ngo.id)
except AttributeError:
# Anonymous users don't have the .ngo attribute
pass

if ngo_queryset.filter(slug=slug.lower()).count():
return True

return False

@classmethod
def validate_ngo_slug(cls, user, slug):
if not slug or not user and not user.is_staff:
Expand Down
80 changes: 45 additions & 35 deletions backend/donations/views/ngo_account.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import List, Optional

from django.conf import settings
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied, ValidationError
from django.db.models import QuerySet
from django.http import Http404, HttpRequest, HttpResponseBadRequest
from django.http import Http404, HttpRequest
from django.shortcuts import redirect, render
from django.urls import reverse, reverse_lazy
from django.utils.decorators import method_decorator
Expand All @@ -15,7 +16,8 @@

from users.models import User

from ..common.validation.registration_number import clean_registration_number, extract_vat_id, ngo_id_number_validator
from ..common.validation.registration_number import extract_vat_id, ngo_id_number_validator
from ..forms.ngo_account import NgoFormForm, NgoPresentationForm
from ..models.donors import Donor
from ..models.jobs import Job
from ..models.ngos import Ngo
Expand Down Expand Up @@ -162,15 +164,15 @@ def post(self, request, *args, **kwargs):

is_fully_editable = ngo.ngohub_org_id is None

if is_fully_editable:
registration_number = clean_registration_number(post.get("cif", ""))
registration_number_errors: str = validate_registration_number(ngo, registration_number)

if registration_number_errors:
errors.append(registration_number_errors)
form = NgoPresentationForm(post, is_fully_editable=is_fully_editable)
if not form.is_valid():
messages.error(request, _("There are some errors on the presentation form."))
context.update({"ngo_presentation": form})

vat_information = extract_vat_id(registration_number)
return render(request, self.template_name, context)

if is_fully_editable:
vat_information = extract_vat_id(form.cleaned_data["cif"])
if ngo.registration_number != vat_information["registration_number"]:
ngo.registration_number = vat_information["registration_number"]
must_refresh_prefilled_form = True
Expand All @@ -179,22 +181,21 @@ def post(self, request, *args, **kwargs):
ngo.vat_id = vat_information["vat_id"]
must_refresh_prefilled_form = True

ngo_name = post.get("name", "").strip()
if ngo.name != ngo_name:
ngo.name = ngo_name

ngo.phone = post.get("contact-phone", "").strip()
ngo.email = post.get("contact-email", "").strip()
if ngo.name != form.cleaned_data["name"]:
ngo.name = form.cleaned_data["name"]
must_refresh_prefilled_form = True

ngo.website = post.get("website", "").strip()
ngo.address = post.get("address", "").strip()
ngo.county = post.get("county", "").strip()
ngo.active_region = post.get("active-region", "").strip()
ngo.phone = form.cleaned_data["contact_phone"]
ngo.email = form.cleaned_data["contact_email"]

ngo.is_accepting_forms = post.get("is_accepting_forms", "").strip() == "on"
ngo.website = form.cleaned_data["website"]
ngo.address = form.cleaned_data["address"]
ngo.county = form.cleaned_data["county"]
ngo.active_region = form.cleaned_data["active_region"]

ngo.display_email = post.get("display-email", "").strip() == "on"
ngo.display_phone = post.get("display-phone", "").strip() == "on"
ngo.is_accepting_forms = form.cleaned_data["is_accepting_forms"]
ngo.display_email = form.cleaned_data["display_email"]
ngo.display_phone = form.cleaned_data["display_phone"]

if errors:
return render(request, self.template_name, context)
Expand Down Expand Up @@ -261,23 +262,32 @@ def post(self, request, *args, **kwargs):

must_refresh_prefilled_form = False

ngo_slug = post.get("organization-slug", "").strip().lower()
ngo_slug_errors = CheckNgoUrl.validate_ngo_slug(user, ngo_slug)
ngo.slug = ngo_slug
form = NgoFormForm(post)

if isinstance(ngo_slug_errors, HttpResponseBadRequest):
errors.append(_("The URL is already used"))
if not form.is_valid():
messages.error(request, _("There are some errors on the redirection form."))
context.update({"ngo_form": form})
return render(request, self.template_name, context)

slug_has_errors = False

form_slug = form.cleaned_data["slug"]
if CheckNgoUrl().check_slug_is_blocked(form_slug):
slug_has_errors = True
form.add_error("slug", ValidationError(_("The URL is blocked")))

bank_account = post.get("iban", "").strip().upper().replace(" ", "").replace("<", "").replace(">", "")[:34]
bank_account_errors: str = validate_iban(bank_account)
if ngo.bank_account != bank_account:
ngo.bank_account = bank_account
must_refresh_prefilled_form = True
if CheckNgoUrl().check_slug_is_reused(form_slug, user):
slug_has_errors = True
form.add_error("slug", ValidationError(_("The URL is already used")))

if slug_has_errors:
context["ngo_form"] = form
return render(request, self.template_name, context)

ngo.description = post.get("description", "").strip()
ngo.slug = form_slug

if bank_account_errors:
errors.append(bank_account_errors)
ngo.bank_account = form.cleaned_data["iban"]
ngo.description = form.cleaned_data["description"]

if errors:
return render(request, self.template_name, context)
Expand Down
Loading

0 comments on commit 0902f32

Please sign in to comment.