Skip to content

Commit

Permalink
Fix generating the NGO prefilled form
Browse files Browse the repository at this point in the history
  • Loading branch information
danniel committed Jan 31, 2024
1 parent 6c5f1d9 commit bde6223
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.9 on 2024-01-31 08:25

from django.db import migrations, models
import donations.models.main
import functools


class Migration(migrations.Migration):

dependencies = [
("donations", "0003_alter_donor_pdf_file"),
]

operations = [
migrations.RemoveField(
model_name="ngo",
name="custom_form",
),
migrations.AddField(
model_name="ngo",
name="prefilled_form",
field=models.FileField(
blank=True,
storage=donations.models.main.select_public_storage,
upload_to=functools.partial(donations.models.main.ngo_directory_path, *("forms",), **{}),
verbose_name="form with prefilled ngo data",
),
),
]
25 changes: 25 additions & 0 deletions backend/donations/migrations/0005_alter_ngo_prefilled_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.9 on 2024-01-31 08:56

from django.db import migrations, models
import donations.models.main
import functools


class Migration(migrations.Migration):

dependencies = [
("donations", "0004_remove_ngo_custom_form_ngo_prefilled_form"),
]

operations = [
migrations.AlterField(
model_name="ngo",
name="prefilled_form",
field=models.FileField(
blank=True,
storage=donations.models.main.select_public_storage,
upload_to=functools.partial(donations.models.main.ngo_directory_path, *("prefilled_forms",), **{}),
verbose_name="form with prefilled ngo data",
),
),
]
12 changes: 6 additions & 6 deletions backend/donations/models/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ def _id_code(prefix: str, id: int) -> str:

def ngo_directory_path(subdir, instance, filename) -> str:
# file will be uploaded to MEDIA_ROOT/ngo-<id>-<hash>/<subdir>/<filename>
return "ngo-{0}-{1}/{2}/{3}".format(instance.pk, _id_code("ngo", instance.pk), subdir, filename)
return "{0}/ngo-{1}-{2}/{3}".format(subdir, instance.pk, _id_code("ngo", instance.pk), filename)


def year_ngo_donor_directory_path(subdir, instance, filename) -> str:
timestamp = timezone.now()
return "{0}/ngo-{1}-{2}/{3}/{4}_{5}_{6}".format(
return "{0}/{1}/ngo-{2}-{3}/{4}_{5}_{6}".format(
subdir,
timestamp.date().year,
instance.ngo.pk if instance.ngo else 0,
_id_code("ngo", instance.ngo.pk if instance.ngo else 0),
subdir,
instance.pk,
_id_code("donor", instance.pk),
filename,
Expand Down Expand Up @@ -151,12 +151,12 @@ class Ngo(models.Model):
null=False,
max_length=255,
)
custom_form = models.FileField(
verbose_name=_("form with ngo data"),
prefilled_form = models.FileField(
verbose_name=_("form with prefilled ngo data"),
blank=True,
null=False,
storage=select_public_storage,
upload_to=partial(ngo_directory_path, "forms"),
upload_to=partial(ngo_directory_path, "prefilled_forms"),
)

date_created = models.DateTimeField(verbose_name=_("date created"), db_index=True, auto_now_add=timezone.now)
Expand Down
51 changes: 44 additions & 7 deletions backend/donations/views/api.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import json

import logging
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied, BadRequest
from django.http import HttpResponse
from django.http import JsonResponse
from django.core.files import File
from django.http import HttpResponse, JsonResponse, Http404
from django.shortcuts import redirect
from django.urls import reverse
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator

from ..models.main import Ngo
from ..models.jobs import Job
from ..pdf import create_pdf
from .base import BaseHandler, AccountHandler


Expand Down Expand Up @@ -51,8 +53,43 @@ def get(self, request, *args, **kwargs):


class GetNgoForm(BaseHandler):
def get(self, request, *args, **kwargs):
raise NotImplementedError("GetNgoForm not implemented yet")
def get(self, request, ngo_url, *args, **kwargs):
try:
ngo = Ngo.objects.get(slug=ngo_url)
except Ngo.DoesNotExist:
raise Http404()

# if we have an form created for this ngo, return the url
if ngo.prefilled_form:
return redirect(ngo.prefilled_form.url)

# else, create a new one and save it for future use
ngo_dict = {
"name": ngo.name,
"cif": ngo.registration_number,
"account": ngo.bank_account.upper(),
# do not add any checkmark on this form regarding the number of years
"years_checkmark": False,
# "two_years": False,
"special_status": ngo.has_special_status,
}
donor = {
# we assume that ngos are looking for people with income from wages
"income": "wage"
}
pdf = create_pdf(donor, ngo_dict)

# filename = "Formular 2% - {0}.pdf".format(ngo.name)
filename = "Formular_donatie.pdf"
ngo.prefilled_form.save(filename, File(pdf))

# close the file after it has been uploaded
pdf.close()

ngo.form_url = ngo.prefilled_form.url
ngo.save()

return redirect(ngo.prefilled_form.url)


class GetNgoForms(AccountHandler):
Expand Down
4 changes: 2 additions & 2 deletions backend/donations/views/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ def get(self, request):
# get all the ngos
ngos = Ngo.objects.all()

logger.info("Removing form_url and custom_form from {0} ngos.".format(len(ngos)))
logger.info("Removing form_url and prefilled_form from {0} ngos.".format(len(ngos)))

# loop through them and remove the form_url
# this will force an update on it when downloaded again
for ngo in ngos:
ngo.form_url = ""
ngo.custom_form.delete()
ngo.prefilled_form.delete()

return HttpResponse("ok")

0 comments on commit bde6223

Please sign in to comment.