From 5d3881721fa4162c7bec957567a5ded61f7739b2 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sat, 23 Dec 2023 11:26:05 +0100 Subject: [PATCH] =?UTF-8?q?c'est=20celery=20qui=20s'occupera=20de=20l'int?= =?UTF-8?q?=C3=A9gration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agenda_culturel/celery.py | 16 +++++++++++ ...batchimportation_error_message_and_more.py | 28 +++++++++++++++++++ src/agenda_culturel/models.py | 7 ++++- .../batchimportation_form.html | 2 +- src/agenda_culturel/views.py | 13 +++++++-- 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/agenda_culturel/migrations/0013_batchimportation_error_message_and_more.py diff --git a/src/agenda_culturel/celery.py b/src/agenda_culturel/celery.py index fd98ffe..2bf5d8a 100644 --- a/src/agenda_culturel/celery.py +++ b/src/agenda_culturel/celery.py @@ -26,6 +26,22 @@ app.config_from_object("django.conf:settings", namespace="CELERY") app.autodiscover_tasks() +@app.task(bind=True) +def import_events_from_json(self, json, taskid): + from agenda_culturel.models import Event + + logger.info("Import events from json: {}".format(taskid)) + + # TODO + + +@app.task(bind=True) +def import_events_from_url(self, source, browsable_url, taskid): + from agenda_culturel.models import Event + + logger.info("Import events from url: {} {}".format(source, taskid)) + + # TODO app.conf.timezone = "Europe/Paris" diff --git a/src/agenda_culturel/migrations/0013_batchimportation_error_message_and_more.py b/src/agenda_culturel/migrations/0013_batchimportation_error_message_and_more.py new file mode 100644 index 0000000..3038964 --- /dev/null +++ b/src/agenda_culturel/migrations/0013_batchimportation_error_message_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.7 on 2023-12-23 10:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('agenda_culturel', '0012_remove_batchimportation_running_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='batchimportation', + name='error_message', + field=models.CharField(blank=True, max_length=512, null=True, verbose_name='Error message'), + ), + migrations.AddField( + model_name='batchimportation', + name='nb_imported', + field=models.PositiveIntegerField(default=0, verbose_name='Number of imported events'), + ), + migrations.AddField( + model_name='batchimportation', + name='nb_initial', + field=models.PositiveIntegerField(default=0, verbose_name='Number of collected events'), + ), + ] diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index ee0c5cf..ed00ef9 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -235,4 +235,9 @@ class BatchImportation(models.Model): source = models.URLField(verbose_name=_('Source'), help_text=_("URL of the source document"), max_length=1024, blank=True, null=True) browsable_url = models.URLField(verbose_name=_('Browsable url'), help_text=_("URL of the corresponding document that will be shown to visitors."), max_length=1024, blank=True, null=True) - status = models.CharField(_("Status"), max_length=20, choices=STATUS.choices, default=STATUS.RUNNING) \ No newline at end of file + status = models.CharField(_("Status"), max_length=20, choices=STATUS.choices, default=STATUS.RUNNING) + + error_message = models.CharField(verbose_name=_('Error message'), max_length=512, blank=True, null=True) + + nb_initial = models.PositiveIntegerField(verbose_name=_('Number of collected events'), default=0) + nb_imported = models.PositiveIntegerField(verbose_name=_('Number of imported events'), default=0) diff --git a/src/agenda_culturel/templates/agenda_culturel/batchimportation_form.html b/src/agenda_culturel/templates/agenda_culturel/batchimportation_form.html index 5fb4fe1..5e8761c 100644 --- a/src/agenda_culturel/templates/agenda_culturel/batchimportation_form.html +++ b/src/agenda_culturel/templates/agenda_culturel/batchimportation_form.html @@ -14,7 +14,7 @@

- JSON au format attendu pour l'import. Si le JSON est fourni ici, on ne lancera pas une récupération depuis l'URL donnée en paramètre. + JSON au format attendu pour l'import. Si le JSON est fourni ici, on ignorera les URL données au dessus, et on utilisera les informations fournies par le json sans réaliser d'importation supplémentaire d'événements depuis l'URL.

diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index 62ee72a..62b0617 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -32,6 +32,8 @@ from django.contrib.messages.views import SuccessMessageMixin from .calendar import CalendarMonth, CalendarWeek from .extractors import ExtractorAllURLs +from .celery import import_events_from_json, import_events_from_url + import unicodedata @@ -491,9 +493,14 @@ class BatchImportationCreateView(SuccessMessageMixin, LoginRequiredMixin, Create success_message = _('The import has been run successfully.') def form_valid(self, form): - # TODO run a celery script + response = super().form_valid(form) - return super().form_valid(form) + if "json" in form.data and form.data["json"] is not None and form.data["json"].strip() != "": + import_events_from_json.delay(form.data["json"], self.object.id) + else: + import_events_from_url.delay(self.object.source, self.object.browsable_url, self.object.id) + + return response @login_required(login_url="/accounts/login/") @@ -502,7 +509,7 @@ def cancel_import(request, pk): if request.method == 'POST': # TODO cancel the celery import - + import_process.status = BatchImportation.STATUS.CANCELED import_process.save(update_fields=["status"])