Skip to content

Commit

Permalink
Remove pdf_url
Browse files Browse the repository at this point in the history
- the field is no longer used
- it hasn't been used since migrating to Django
  • Loading branch information
tudoramariei committed Nov 1, 2024
1 parent 1807b56 commit 6f6d16a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 86 deletions.
17 changes: 17 additions & 0 deletions backend/donations/migrations/0016_remove_donor_pdf_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.2 on 2024-10-31 14:40

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("donations", "0015_rename_last_name_donor_f_name"),
]

operations = [
migrations.RemoveField(
model_name="donor",
name="pdf_url",
),
]
1 change: 0 additions & 1 deletion backend/donations/models/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ class Donor(models.Model):

geoip = models.JSONField(verbose_name=_("Geo IP"), blank=True, null=False, default=dict)

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)

Expand Down
2 changes: 1 addition & 1 deletion backend/donations/views/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def get(self, request):
donor.f_name,
donor.email,
donor.has_signed,
donor.pdf_file.url if donor.pdf_file else donor.pdf_url,
donor.pdf_file.url if donor.pdf_file else "",
donor.ngo.name,
donor.ngo.email,
donor.ngo.is_accepting_forms,
Expand Down
3 changes: 0 additions & 3 deletions backend/donations/views/donations_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,8 @@ def _get_address_details(donation_object: Donor) -> Dict[str, str]:


def _get_pdf_url(donation: Donor) -> str:
# The 'pdf_file' property has priority over the old 'pdf_url' one
if donation.pdf_file:
source_url = donation.pdf_file.url
elif donation.pdf_url:
source_url = donation.pdf_url
else:
source_url = ""

Expand Down
86 changes: 5 additions & 81 deletions backend/importer/tasks/donor_forms.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import logging
import tempfile
from typing import List

import requests
from django.conf import settings
from django.core.files import File
from django.db.models import Count, Q, QuerySet
from django_q.tasks import async_task
from pypdf import PdfReader
from requests import Response
from sentry_sdk import capture_message

from donations.models.main import Donor, Ngo

from ..extract import DATA_ZONES, extract_data

logger = logging.getLogger(__name__)


Expand All @@ -37,11 +28,9 @@ def import_donor_forms_task(batch_size: int = 50, run_async: bool = False, dry_r
index: int = 0
processed_form_ids: List[int] = []
for index, ngo in enumerate(ngos_by_number_of_donors):
donor_forms_for_ngo: List[int] = (
Donor.objects.exclude(pdf_url="")
.filter(ngo=ngo, pdf_file="", date_created__gte="2023-12-31")
.values_list("pk", flat=True)[: batch_size + 1]
)
donor_forms_for_ngo: List[int] = Donor.objects.filter(
ngo=ngo, pdf_file="", date_created__gte="2023-12-31"
).values_list("pk", flat=True)[: batch_size + 1]

processed_form_ids.extend(donor_forms_for_ngo)

Expand All @@ -68,71 +57,6 @@ def execute_import(index, processed_form_ids: List[int], run_async: bool, dry_ru
def import_donor_forms(ids: List[int], dry_run: bool):
"""
Download and re-upload the donation form files one by one
XXX: Will be removed
"""
target_donors: QuerySet[Donor] = Donor.objects.filter(pk__in=ids).order_by("pk")

logger.info("Found %s donors to import", target_donors.count())

attempted_donors: int = 0
transferred_donors: int = 0

donor: Donor
for attempted_donors, donor in enumerate(target_donors):

logger.debug("Processing donation: %s", donor.pk)

if not donor.pdf_url.startswith("http"):
logger.debug("Skipped form %s: PDF URL does not start with http", donor.pk)
continue

r: Response = requests.get(donor.pdf_url)
if r.status_code != 200:
error_message = f"Donation form request status: {r.status_code} for donor {donor.pk}"
logger.warning(error_message)

if settings.ENABLE_SENTRY:
capture_message(error_message, level="error")

continue

with tempfile.TemporaryFile() as fp:
fp.write(r.content)
fp.seek(0)
donor.pdf_file.save("donation_form.pdf", File(fp), save=False)
fp.seek(0)
reader = PdfReader(fp)
page = reader.pages[0]

try:
donor.set_cnp(extract_data(page, DATA_ZONES["cnp"]))
donor.initial = extract_data(page, DATA_ZONES["father"])

donor.set_address_helper(
street_name=extract_data(page, DATA_ZONES["street_name"]),
street_number=extract_data(page, DATA_ZONES["street_number"]),
street_bl=extract_data(page, DATA_ZONES["street_bl"]),
street_sc=extract_data(page, DATA_ZONES["street_sc"]),
street_et=extract_data(page, DATA_ZONES["street_et"]),
street_ap=extract_data(page, DATA_ZONES["street_ap"]),
)
except Exception as e:
logger.error("Error extracting data from PDF: %s for donor %s", e, donor.pk)
continue

logger.debug("New form file: %s", donor.pdf_file)

if not dry_run:
donor.save()
else:
logger.info("Dry run: not saving the donor form")

transferred_donors += 1

if transferred_donors:
logger.info("Transferred %d out of %d donors", transferred_donors, attempted_donors + 1)
else:
error_message = f"No donors were transferred for the following list of {attempted_donors + 1} IDs: {ids}"
logger.error(error_message)

if settings.ENABLE_SENTRY:
capture_message(error_message, level="error")
...

0 comments on commit 6f6d16a

Please sign in to comment.