Skip to content

Commit

Permalink
Implement form data completion
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Jan 4, 2024
1 parent f5b5dd6 commit 759a239
Show file tree
Hide file tree
Showing 17 changed files with 569 additions and 322 deletions.
47 changes: 47 additions & 0 deletions backend/donations/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django import forms
from django.utils.translation import gettext_lazy as _
from localflavor.ro.forms import ROCNPField

from donations.models import Donor


class DonorInputForm(forms.ModelForm):
street = forms.CharField(max_length=100, label=_("Strada"))
street_number = forms.CharField(max_length=10, label=_("Număr"))
block = forms.CharField(max_length=10, label=_("Bloc"), required=False)
entrance = forms.CharField(max_length=10, label=_("Scara"), required=False)
floor = forms.CharField(max_length=10, label=_("Etaj"), required=False)
apartment = forms.CharField(max_length=10, label=_("Apartament"), required=False)
terms = forms.BooleanField(label=_("Terms"), required=True)

ngo_id = forms.IntegerField(widget=forms.HiddenInput(), required=False)
personal_identifier = ROCNPField(label="CNP")

class Meta:
model = Donor
fields = [
"first_name",
"last_name",
"initial",
"city",
"county",
"phone",
"email",
"is_anonymous",
"two_years",
]

@staticmethod
def _clean_checkbox(value):
if value == "on":
return True
return False

def clean_is_anonymous(self):
return self._clean_checkbox(self.cleaned_data["is_anonymous"])

def clean_two_years(self):
return self._clean_checkbox(self.cleaned_data["two_years"])

def clean_terms(self):
return self._clean_checkbox(self.cleaned_data["terms"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.9 on 2024-01-04 13:58

from django.db import migrations, models
import django_cryptography.fields


class Migration(migrations.Migration):
dependencies = [
("donations", "0002_alter_ngo_form_url"),
]

operations = [
migrations.AddField(
model_name="donor",
name="initial",
field=models.CharField(blank=True, default="", max_length=5, verbose_name="initials"),
),
migrations.AddField(
model_name="donor",
name="personal_identifier",
field=django_cryptography.fields.encrypt(
models.CharField(blank=True, default="", max_length=13, verbose_name="CNP")
),
),
migrations.AlterField(
model_name="ngo",
name="form_url",
field=models.CharField(blank=True, default="", max_length=255, unique=True, verbose_name="form url"),
),
]
23 changes: 12 additions & 11 deletions backend/donations/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django_cryptography.fields import encrypt


class Ngo(models.Model):
Expand Down Expand Up @@ -59,7 +60,7 @@ class Ngo(models.Model):
is_verified = models.BooleanField(verbose_name=_("is verified"), db_index=True, default=False)

# originally: special_status
# if the ngo has a special status (eg. social ngo) they are entitled to 3.5% donation, not 2%
# if the ngo has a special status (e.g. social ngo) they are entitled to 3.5% donation, not 2%
has_special_status = models.BooleanField(verbose_name=_("has special status"), db_index=True, default=False)

# originally: accepts_forms
Expand Down Expand Up @@ -94,6 +95,12 @@ class Donor(models.Model):

first_name = models.CharField(verbose_name=_("first name"), blank=True, null=False, default="", max_length=100)
last_name = models.CharField(verbose_name=_("last name"), blank=True, null=False, default="", max_length=100)
initial = models.CharField(verbose_name=_("initials"), blank=True, null=False, default="", max_length=5)

personal_identifier = encrypt(
models.CharField(verbose_name=_("CNP"), blank=True, null=False, default="", max_length=13)
)

city = models.CharField(
verbose_name=_("city"),
blank=True,
Expand All @@ -110,20 +117,20 @@ class Donor(models.Model):
max_length=100,
db_index=True,
)
email = models.EmailField(verbose_name=_("email"), blank=False, null=False, db_index=True)

# originally: tel
phone = models.CharField(verbose_name=_("telephone"), blank=True, null=False, default="", max_length=30)
email = models.EmailField(verbose_name=_("email"), blank=False, null=False, db_index=True)

# orinally: "anonymous"
# originally: "anonymous"
is_anonymous = models.BooleanField(
verbose_name=_("anonymous"),
db_index=True,
default=True,
help_text=_("If the user would like the ngo to see the donation"),
)

# orinally: "income"
# originally: "income"
income_type = models.CharField(
verbose_name=_("income type"),
max_length=30,
Expand All @@ -140,17 +147,11 @@ class Donor(models.Model):
)

geoip = models.JSONField(verbose_name=_("Geo IP"), blank=True, null=False, default=dict)
# {
# "country": country,
# "region": region,
# "city": city,
# "lat_long": lat_long,
# "ip_address": ip_address
# }

pdf_url = models.URLField(verbose_name=_("PDF URL"), blank=True, null=False, default="", max_length=255)
filename = models.CharField(verbose_name=_("filename"), blank=True, null=False, default="", max_length=100)
has_signed = models.BooleanField(verbose_name=_("has signed"), db_index=True, default=False)

date_created = models.DateTimeField(verbose_name=_("date created"), db_index=True, auto_now_add=timezone.now)

class Meta:
Expand Down
4 changes: 2 additions & 2 deletions backend/donations/views/my_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from django.utils import timezone
from django.utils.decorators import method_decorator

from ..models import Donor, Ngo
from .base import AccountHandler
from ..models import Donor, Ngo


class MyAccountDetailsHandler(AccountHandler):
Expand Down Expand Up @@ -90,7 +90,7 @@ def post(self, request, *args, **kwargs):
ngo.address = post.get("ong-adresa")
ngo.county = post.get("ong-judet")
ngo.active_region = post.get("ong-activitate")
ngo.form_url = post.get("ong-url")
ngo.form_url = post.get("ong-url").lower()
ngo.registration_number = post.get("ong-cif")
ngo.bank_account = post.get("ong-cont")
ngo.has_special_status = True if post.get("special-status") == "on" else False
Expand Down
91 changes: 86 additions & 5 deletions backend/donations/views/ngo.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,98 @@
from django.conf import settings
from django.http import Http404
from django.shortcuts import redirect, render
from django.urls import reverse

from .base import BaseHandler
from ..forms import DonorInputForm
from ..models import Donor, Ngo


class DonationSucces(BaseHandler):
pass
template_name = "succes.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
ngo = Ngo.objects.get(form_url=kwargs["ngo_url"].lower())
context["ngo"] = ngo
return context

def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)

return render(self.request, self.template_name, context)


class FormSignature(BaseHandler):
pass
template_name = "signature.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
ngo = Ngo.objects.get(form_url=kwargs["ngo_url"].lower())
context["ngo"] = ngo
return context

def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)

return render(self.request, self.template_name, context)

class NgoHandler(BaseHandler):
pass
def post(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)

return redirect(reverse("ngo-twopercent-success", kwargs={"ngo_url": context["ngo_url"]}))


class TwoPercentHandler(BaseHandler):
pass
def get_context_data(self, request, **kwargs):
ngo_url: str = kwargs["ngo_url"].lower()
if not Ngo.objects.filter(form_url=ngo_url).exists():
raise Http404("Nu exista o asociație cu acest URL")

ngo = Ngo.objects.get(form_url=ngo_url)
form_counties = settings.FORM_COUNTIES

context = {"is_authenticated": False, "ngo_url": ngo_url, "ngo": ngo, "counties": form_counties}

if request.user.is_authenticated and request.user.ngo == ngo:
context["is_authenticated"] = True
return context

context["limit"] = settings.DONATIONS_LIMIT
context["can_donate"] = True

return context

def get(self, request, *args, **kwargs):
context = self.get_context_data(request, **kwargs)

template = "twopercent.html"

if context["is_authenticated"]:
template = "ngo/ngo-details.html"

return render(request, template, context)

def post(self, request, *args, **kwargs):
post = request.POST
context = self.get_context_data(request, **kwargs)

form = DonorInputForm(post)
if not form.is_valid():
context.update(form.cleaned_data)
context["errors"] = {"fields": list(form.errors.values())}

return render(request, "twopercent.html", context)

new_donor: Donor = form.save(commit=False)
new_donor.ngo = context["ngo"]

new_donor.save()

new_donor.pdf_url = self._generate_pdf(form.cleaned_data, context["ngo"])

return redirect(reverse("ngo-twopercent-signature", kwargs={"ngo_url": context["ngo_url"]}))

@staticmethod
def _generate_pdf(donor_data, param):
return "PDF_URL"
47 changes: 4 additions & 43 deletions backend/redirectioneaza/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import environ
import sentry_sdk
from django.utils import timezone
from localflavor.ro.ro_counties import COUNTIES_CHOICES

# Constants for memory sizes
KIBIBYTE = 1024
Expand Down Expand Up @@ -404,49 +405,9 @@

DEFAULT_NGO_LOGO = env.str("DEFAULT_NGO_LOGO")

LIST_OF_COUNTIES = [
"Alba",
"Arad",
"Arges",
"Bacau",
"Bihor",
"Bistrita-Nasaud",
"Botosani",
"Braila",
"Brasov",
"Buzau",
"Calarasi",
"Caras-Severin",
"Cluj",
"Constanta",
"Covasna",
"Dambovita",
"Dolj",
"Galati",
"Giurgiu",
"Gorj",
"Harghita",
"Hunedoara",
"Ialomita",
"Iasi",
"Ilfov",
"Maramures",
"Mehedinti",
"Mures",
"Neamt",
"Olt",
"Prahova",
"Salaj",
"Satu Mare",
"Sibiu",
"Suceava",
"Teleorman",
"Timis",
"Tulcea",
"Valcea",
"Vaslui",
"Vrancea",
]
LIST_OF_COUNTIES = [county[1] for county in COUNTIES_CHOICES]
FORM_COUNTIES = deepcopy(LIST_OF_COUNTIES)
FORM_COUNTIES.pop(LIST_OF_COUNTIES.index("București"))
CONTACT_EMAIL_ADDRESS = env.str("CONTACT_EMAIL_ADDRESS")

# Django Q2
Expand Down
Loading

0 comments on commit 759a239

Please sign in to comment.