Skip to content

Commit

Permalink
Tweaks and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Feb 20, 2024
1 parent 12a2758 commit 7bdc41c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
5 changes: 3 additions & 2 deletions backend/donations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class DonorAdmin(admin.ModelAdmin):

exclude = ("personal_identifier", "address")

readonly_fields = ("date_created",)

fieldsets = (
(
_("NGO"),
Expand All @@ -69,11 +71,10 @@ class DonorAdmin(admin.ModelAdmin):
),
(
_("File"),
{"fields": ("pdf_url", "filename", "has_signed")},
{"fields": ("pdf_url", "filename", "has_signed", "pdf_file")},
),
(
_("Date"),
{"fields": ("date_created",)},
),
)
readonly_fields = ("date_created",)
5 changes: 4 additions & 1 deletion backend/donations/views/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import logging
import random
import string

from urllib.request import Request, urlopen
from datetime import datetime, date
Expand Down Expand Up @@ -173,7 +175,8 @@ def post(self, request, *args, **kwargs):

ngo = request.user.ngo
if not ngo:
ngo = Ngo.objects.create()
ngo = Ngo()
ngo.slug = "".join(random.choices(string.ascii_lowercase + "-", k=8))
ngo.save()
request.user.ngo = ngo
request.user.save()
Expand Down
2 changes: 1 addition & 1 deletion backend/importer/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ImportAdmin(admin.ModelAdmin):

fieldsets = ((None, {"fields": ("import_type", "csv_file", "has_header")}),)

actions = ("process_import", "transfer_logos")
actions = ("process_import", "transfer_logos", "transfer_donor_forms")

@admin.action(description=_("Process the selected import jobs"))
def process_import(self, request, queryset: QuerySet[ImportJob]):
Expand Down
23 changes: 13 additions & 10 deletions backend/importer/management/commands/import_donor_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@

def _import_donor_forms(batch_size=50):
"""
Download an reupload the donation form files one by one
Download and re-upload the donation form files one by one
"""
donor: Donor
for donor in (
Donor.objects.exclude(Q(pdf_url__isnull=True) | Q(pdf_url=""))
.filter(Q(pdf_file="") | Q(pdf_file__isnull=True))
.all()
.order_by("-date_created")[:batch_size]
):
logger.debug("Processing donation: %s", donor.name)
logger.debug("Processing donation: %s", donor.first_name)

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

r = requests.get(donor.pdf_url)
Expand All @@ -46,13 +47,15 @@ def _import_donor_forms(batch_size=50):
donor.set_cnp(extract_data(page, DATA_ZONES["cnp"]))
donor.initial = extract_data(page, DATA_ZONES["father"])

# print("Street Name =", extract_data(page, DATA_ZONES["street_name"]))
# print("Street Number =", extract_data(page, DATA_ZONES["street_number"]))
# print("BL =", extract_data(page, DATA_ZONES["street_bl"]))
# print("SC =", extract_data(page, DATA_ZONES["street_sc"]))
# print("ET =", extract_data(page, DATA_ZONES["street_et"]))
# print("AP =", extract_data(page, DATA_ZONES["street_ap"]))
# print("Percent =", extract_data(page, DATA_ZONES["percent"]))
address = {
"street_name": extract_data(page, DATA_ZONES["street_name"]),
"street_number": extract_data(page, DATA_ZONES["street_number"]),
"bl": extract_data(page, DATA_ZONES["street_bl"]),
"sc": extract_data(page, DATA_ZONES["street_sc"]),
"et": extract_data(page, DATA_ZONES["street_et"]),
"ap": extract_data(page, DATA_ZONES["street_ap"]),
}
donor.set_address(address)

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

0 comments on commit 7bdc41c

Please sign in to comment.