c'est celery qui s'occupera de l'intégration

This commit is contained in:
Jean-Marie Favreau 2023-12-23 11:26:05 +01:00
parent a47178dac1
commit 5d3881721f
5 changed files with 61 additions and 5 deletions

View File

@ -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"

View File

@ -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'),
),
]

View File

@ -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)
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)

View File

@ -14,7 +14,7 @@
<p>
<label for="id_json">JSON (facultatif) :</label>
<textarea id="id_json" name="json" rows="10"></textarea>
<span class="helptext">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.</span>
<span class="helptext">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.</span>
</p>
<input type="submit" value="Envoyer">

View File

@ -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"])