From 722dcbbaaa9768e16edb5633008907af0bd820ce Mon Sep 17 00:00:00 2001 From: Tudor Amariei Date: Thu, 31 Oct 2024 13:03:00 +0200 Subject: [PATCH] Remove models for the importer --- backend/importer/admin.py | 62 ------------------- ...ions_remove_importjob_csv_file_and_more.py | 37 +++++++++++ .../migrations/0005_delete_importjob.py | 16 +++++ backend/importer/models.py | 45 -------------- 4 files changed, 53 insertions(+), 107 deletions(-) delete mode 100644 backend/importer/admin.py create mode 100644 backend/importer/migrations/0004_alter_importjob_options_remove_importjob_csv_file_and_more.py create mode 100644 backend/importer/migrations/0005_delete_importjob.py delete mode 100644 backend/importer/models.py diff --git a/backend/importer/admin.py b/backend/importer/admin.py deleted file mode 100644 index 6d4d3dc6..00000000 --- a/backend/importer/admin.py +++ /dev/null @@ -1,62 +0,0 @@ -from django.conf import settings -from django.contrib import admin -from django.core.management import call_command -from django.db.models import QuerySet -from django.utils.translation import gettext_lazy as _ -from django_q.tasks import async_task -from unfold.admin import ModelAdmin - -from .models import ImportJob -from .tasks.processor import process_import_task - - -@admin.register(ImportJob) -class ImportAdmin(ModelAdmin): - list_display = ("import_type", "csv_file", "uploaded_at", "status") - list_filter = ("import_type", "uploaded_at") - search_fields = ("import_type",) - - fieldsets = ((None, {"fields": ("import_type", "csv_file", "has_header")}),) - - actions = ( - "process_import", - "transfer_logos", - "transfer_donor_forms_dry_run", - "transfer_donor_forms", - "transfer_donor_forms_schedule", - ) - - @admin.action(description=_("Process the selected import jobs")) - def process_import(self, request, queryset: QuerySet[ImportJob]): - import_obj: ImportJob - for import_obj in queryset: - if settings.IMPORT_METHOD == "async": - async_task("importer.tasks.processor.process_import_task", import_obj.id) - else: - process_import_task(import_obj.id) - - self.message_user(request, _("The import has been processed.")) - - @admin.action(description=_("Schedule the transfer of logos to the current storage")) - def transfer_logos(self, request, queryset: QuerySet[ImportJob]): - call_command("import_logos", "--schedule") - - self.message_user(request, _("The logos have been transferred.")) - - @admin.action(description=_("Transfer of donor forms to the current storage (dry run)")) - def transfer_donor_forms_dry_run(self, request, queryset: QuerySet[ImportJob]): - call_command("import_donor_forms", "--batch_size=50", "--dry_run") - - self.message_user(request, _("The donor forms have been transferred.")) - - @admin.action(description=_("Transfer of donor forms to the current storage")) - def transfer_donor_forms(self, request, queryset: QuerySet[ImportJob]): - call_command("import_donor_forms", "--batch_size=50") - - self.message_user(request, _("The donor forms have been transferred.")) - - @admin.action(description=_("Schedule the transfer of donor forms to the current storage")) - def transfer_donor_forms_schedule(self, request, queryset: QuerySet[ImportJob]): - call_command("import_donor_forms", "--batch_size=50", "--schedule") - - self.message_user(request, _("The donor forms have been scheduled for transfer.")) diff --git a/backend/importer/migrations/0004_alter_importjob_options_remove_importjob_csv_file_and_more.py b/backend/importer/migrations/0004_alter_importjob_options_remove_importjob_csv_file_and_more.py new file mode 100644 index 00000000..d9b34f93 --- /dev/null +++ b/backend/importer/migrations/0004_alter_importjob_options_remove_importjob_csv_file_and_more.py @@ -0,0 +1,37 @@ +# Generated by Django 5.1.2 on 2024-10-31 11:01 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("importer", "0003_alter_importjob_csv_file"), + ] + + operations = [ + migrations.AlterModelOptions( + name="importjob", + options={}, + ), + migrations.RemoveField( + model_name="importjob", + name="csv_file", + ), + migrations.RemoveField( + model_name="importjob", + name="has_header", + ), + migrations.RemoveField( + model_name="importjob", + name="import_type", + ), + migrations.RemoveField( + model_name="importjob", + name="status", + ), + migrations.RemoveField( + model_name="importjob", + name="uploaded_at", + ), + ] diff --git a/backend/importer/migrations/0005_delete_importjob.py b/backend/importer/migrations/0005_delete_importjob.py new file mode 100644 index 00000000..2b8c5f6f --- /dev/null +++ b/backend/importer/migrations/0005_delete_importjob.py @@ -0,0 +1,16 @@ +# Generated by Django 5.1.2 on 2024-10-31 11:02 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("importer", "0004_alter_importjob_options_remove_importjob_csv_file_and_more"), + ] + + operations = [ + migrations.DeleteModel( + name="ImportJob", + ), + ] diff --git a/backend/importer/models.py b/backend/importer/models.py deleted file mode 100644 index b7894636..00000000 --- a/backend/importer/models.py +++ /dev/null @@ -1,45 +0,0 @@ -from django.db import models -from django.utils import timezone -from django.utils.translation import gettext_lazy as _ -from django_q.tasks import async_task - - -class ImportModelTypeChoices(models.TextChoices): - USER = "users.User", "User" - NGO = "donations.Ngo", "Ngo" - DONOR = "donations.Donor", "Donor" - PARTNER = "partners.Partner", "Partner" - - -class ImportStatusChoices(models.TextChoices): - PENDING = "pending", _("Pending") - PROCESSING = "working", _("Processing") - DONE = "done", _("Done") - ERROR = "error", _("Error") - - -class ImportJob(models.Model): - import_type = models.CharField(_("Import type"), max_length=32, choices=ImportModelTypeChoices.choices) - status = models.CharField( - _("Status"), max_length=10, choices=ImportStatusChoices.choices, default=ImportStatusChoices.PENDING - ) - - has_header = models.BooleanField(_("Has header"), blank=False, null=False, default=True) - csv_file = models.FileField(_("File"), upload_to="imports/%Y/%m/%d/") - - uploaded_at = models.DateTimeField(_("Uploaded at"), auto_now_add=timezone.now) - - def process_import(self): - import_id = self.id - - async_task("importer.tasks.processor.process_import_task", import_id) - - class Meta: - verbose_name = _("Import") - verbose_name_plural = _("Imports") - - def save(self, force_insert=False, force_update=False, using=None, update_fields=None): - super().save(force_insert, force_update, using, update_fields) - - if self.status == ImportStatusChoices.PENDING: - self.process_import()