Skip to content

Commit

Permalink
Import NGO logos from the old storage
Browse files Browse the repository at this point in the history
  • Loading branch information
danniel committed Feb 16, 2024
1 parent f6e53e2 commit 863df10
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
File renamed without changes.
44 changes: 44 additions & 0 deletions backend/importer/management/commands/import_logos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import requests
import tempfile
import mimetypes

from django.db.models import Q
from django.core.files import File
from django.core.management import BaseCommand

from donations.models.main import Ngo


def _import_logos(batch_size=50):
"""
Download an reupload the logo files one by one
"""
for ngo in (
Ngo.objects.exclude(Q(logo_url__isnull=True) | Q(logo_url=""))
.filter(Q(logo="") | Q(logo__isnull=True))
.all()[:batch_size]
):
print(f"\nProcessing '{ngo.name}'")

if not ngo.logo_url.startswith("http"):
print("... skipped: Logo URL does not start with http")
continue

r = requests.get(ngo.logo_url)
if r.status_code != 200:
print("... skipped: Request status =", r.status_code)

ext = mimetypes.guess_extension(r.headers["content-type"])
with tempfile.TemporaryFile() as fp:
fp.write(r.content)
fp.seek(0)
ngo.logo.save(f"logo{ext}", File(fp))
print("... new logo:", ngo.logo)


class Command(BaseCommand):
help = "Import NGO logos from the old storage"

def handle(self, *args, **options):
_import_logos()
self.stdout.write(self.style.SUCCESS("NGO logo import done"))
3 changes: 2 additions & 1 deletion backend/importer/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ def parse_imported_date(date_str: str):


IMPORT_DETAILS = {
# TODO: The old "logo" field should be saved to the new "logo_url" field
ImportModelTypeChoices.NGO.value: {
"default_header": (
"slug,has_special_status,description,name,bank_account,registration_number,address,county,"
"active_region,phone,website,email,is_verified,date_created,is_active,logo,is_accepting_forms"
"active_region,phone,website,email,is_verified,date_created,is_active,logo_url,is_accepting_forms"
),
"fields_mapping": {
"date_created": parse_imported_date,
Expand Down
4 changes: 2 additions & 2 deletions backend/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ django-environ~=0.11.2
django-recaptcha~=4.0.0

# encrypting data
cryptography~=42.0.2
cryptography~=42.0.3

# django-q
django-q2~=1.6.1
Expand All @@ -27,7 +27,7 @@ jinja2~=3.1.3
python-geoip-geolite2~=2015.303

# web server
gevent~=23.9.1
gevent~=24.2.1
gunicorn~=21.2.0

# file storage
Expand Down

0 comments on commit 863df10

Please sign in to comment.