From 380271b48d32cca4db18eda5fc9634ca2a16bce4 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sun, 15 Oct 2023 11:05:46 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20d'une=20t=C3=A2che=20pour=20traiter=20l?= =?UTF-8?q?es=20=C3=A9v=C3=A9nements=20soumis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agenda_culturel/admin.py | 5 ++- src/agenda_culturel/celery.py | 26 +++++++------ src/agenda_culturel/forms.py | 11 ++++++ .../migrations/0001_initial.py | 38 +++++++++++++++++++ ...bmissionform_alter_event_reference_urls.py | 31 +++++++++++++++ src/agenda_culturel/models.py | 16 +++++++- .../templates/agenda_culturel/submission.html | 15 ++++++++ src/agenda_culturel/urls.py | 2 + src/agenda_culturel/views.py | 24 +++++++++++- 9 files changed, 151 insertions(+), 17 deletions(-) create mode 100644 src/agenda_culturel/forms.py create mode 100644 src/agenda_culturel/migrations/0001_initial.py create mode 100644 src/agenda_culturel/migrations/0002_eventsubmissionform_alter_event_reference_urls.py create mode 100644 src/agenda_culturel/templates/agenda_culturel/submission.html diff --git a/src/agenda_culturel/admin.py b/src/agenda_culturel/admin.py index 023636f..1fd3116 100644 --- a/src/agenda_culturel/admin.py +++ b/src/agenda_culturel/admin.py @@ -1,4 +1,5 @@ from django.contrib import admin -from .models import Event +from .models import Event, EventSubmissionForm -admin.site.register(Event) \ No newline at end of file +admin.site.register(Event) +admin.site.register(EventSubmissionForm) diff --git a/src/agenda_culturel/celery.py b/src/agenda_culturel/celery.py index 6137f4d..e467f4e 100644 --- a/src/agenda_culturel/celery.py +++ b/src/agenda_culturel/celery.py @@ -2,6 +2,8 @@ import os from celery import Celery from celery.schedules import crontab +from celery.utils.log import get_task_logger + # Set the default Django settings module for the 'celery' program. APP_ENV = os.getenv("APP_ENV", "dev") @@ -9,6 +11,9 @@ os.environ.setdefault("DJANGO_SETTINGS_MODULE", f"agenda_culturel.settings.{APP_ app = Celery("agenda_culturel") +logger = get_task_logger(__name__) + + # Using a string here means the worker doesn't have to serialize # the configuration object to child processes. # - namespace='CELERY' means all celery-related configuration keys @@ -20,16 +25,15 @@ app.autodiscover_tasks() @app.task(bind=True) -def debug_task(self): - print(f"Request: {self.request!r}") +def create_event_from_submission(self, url): + logger.info(f"{url=}") + try: + logger.info("About to create event from submission") + # TODO + except BadHeaderError: + logger.info("BadHeaderError") + except Exception as e: + logger.error(e) -app.conf.beat_schedule = { - "delete_job_files": { - "task": "test_periodic_task", - # Every 1 minute for testing purposes - "schedule": crontab(minute="*/1"), - }, -} - -app.conf.timezone = "UTC" +app.conf.timezone = "Europe/Paris" diff --git a/src/agenda_culturel/forms.py b/src/agenda_culturel/forms.py new file mode 100644 index 0000000..e8822eb --- /dev/null +++ b/src/agenda_culturel/forms.py @@ -0,0 +1,11 @@ +from django.forms import ModelForm +from django.views.generic import FormView + +from .models import EventSubmissionForm + + +class EventSubmissionModelForm(ModelForm): + class Meta: + model = EventSubmissionForm + fields = ["url"] + diff --git a/src/agenda_culturel/migrations/0001_initial.py b/src/agenda_culturel/migrations/0001_initial.py new file mode 100644 index 0000000..88e987d --- /dev/null +++ b/src/agenda_culturel/migrations/0001_initial.py @@ -0,0 +1,38 @@ +# Generated by Django 4.2.1 on 2023-10-14 16:33 + +import django.contrib.postgres.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Event', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_date', models.DateTimeField(auto_now_add=True)), + ('modified_date', models.DateTimeField(auto_now=True)), + ('title', models.CharField(help_text='Short title', max_length=512, verbose_name='Title')), + ('status', models.CharField(choices=[('published', 'Published'), ('trash', 'Trash'), ('draft', 'Draft')], default='published', max_length=20, verbose_name='Status')), + ('start_day', models.DateField(help_text='Day of the event', verbose_name='Day of the event')), + ('start_time', models.TimeField(blank=True, help_text='Starting time', null=True, verbose_name='Starting time')), + ('end_day', models.DateField(blank=True, help_text='End day of the event, only required if different from the start day.', null=True, verbose_name='End day of the event')), + ('end_time', models.TimeField(blank=True, help_text='Final time', null=True, verbose_name='Final time')), + ('location', models.CharField(help_text='Address of the event', max_length=512, verbose_name='Location')), + ('description', models.TextField(blank=True, help_text='General description of the event', null=True, verbose_name='Description')), + ('image', models.URLField(blank=True, help_text='URL of the illustration image', null=True, verbose_name='Illustration')), + ('image_alt', models.CharField(blank=True, help_text='Alternative text used by screen readers for the image', max_length=512, null=True, verbose_name='Illustration description')), + ('reference_urls', django.contrib.postgres.fields.ArrayField(base_field=models.URLField(), blank=True, help_text='List of all the urls where this event can be found.', null=True, size=None, verbose_name='URLs')), + ], + options={ + 'verbose_name': 'Event', + 'verbose_name_plural': 'Events', + }, + ), + ] diff --git a/src/agenda_culturel/migrations/0002_eventsubmissionform_alter_event_reference_urls.py b/src/agenda_culturel/migrations/0002_eventsubmissionform_alter_event_reference_urls.py new file mode 100644 index 0000000..8bf1127 --- /dev/null +++ b/src/agenda_culturel/migrations/0002_eventsubmissionform_alter_event_reference_urls.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.1 on 2023-10-15 08:51 + +import django.contrib.postgres.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('agenda_culturel', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='EventSubmissionForm', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('url', models.URLField(help_text='URL where this event can be found.', max_length=512, verbose_name='URL')), + ], + options={ + 'verbose_name': 'Event submission form', + 'verbose_name_plural': 'Event submissions forms', + 'db_table': 'eventsubmissionform', + }, + ), + migrations.AlterField( + model_name='event', + name='reference_urls', + field=django.contrib.postgres.fields.ArrayField(base_field=models.URLField(max_length=512), blank=True, help_text='List of all the urls where this event can be found.', null=True, size=None, verbose_name='URLs'), + ), + ] diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 1ed025c..3951155 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -35,7 +35,7 @@ class Event(models.Model): image = models.URLField(verbose_name=_('Illustration'), help_text=_("URL of the illustration image"), max_length=200, blank=True, null=True) image_alt = models.CharField(verbose_name=_('Illustration description'), help_text=_('Alternative text used by screen readers for the image'), blank=True, null=True, max_length=512) - reference_urls = ArrayField(models.URLField(max_length=200), verbose_name=_('URLs'), help_text=_("List of all the urls where this event can be found."), blank=True, null=True) + reference_urls = ArrayField(models.URLField(max_length=512), verbose_name=_('URLs'), help_text=_("List of all the urls where this event can be found."), blank=True, null=True) def get_absolute_url(self): return reverse("view_event", kwargs={"pk": self.pk, "extra": self.title}) @@ -45,4 +45,16 @@ class Event(models.Model): class Meta: verbose_name = _('Event') - verbose_name_plural = _('Events') \ No newline at end of file + verbose_name_plural = _('Events') + + +class EventSubmissionForm(models.Model): + url = models.URLField(max_length=512, verbose_name=_('URL'), help_text=_("URL where this event can be found.")) + + class Meta: + db_table = "eventsubmissionform" + verbose_name = _("Event submission form") + verbose_name_plural = _("Event submissions forms") + + def __str__(self): + return self.url \ No newline at end of file diff --git a/src/agenda_culturel/templates/agenda_culturel/submission.html b/src/agenda_culturel/templates/agenda_culturel/submission.html new file mode 100644 index 0000000..4a0b8b3 --- /dev/null +++ b/src/agenda_culturel/templates/agenda_culturel/submission.html @@ -0,0 +1,15 @@ + + + + + Proposer un événement + + +

Formulaire de proposition d'événement

+
+ {% csrf_token %} + {{ form.as_p }} + +
+ + \ No newline at end of file diff --git a/src/agenda_culturel/urls.py b/src/agenda_culturel/urls.py index f65c555..0dbd4ab 100644 --- a/src/agenda_culturel/urls.py +++ b/src/agenda_culturel/urls.py @@ -11,8 +11,10 @@ urlpatterns = [ path("", EventListView.as_view(), name="home"), re_path(r'^(?P' + '|'.join([dm.value for dm in DisplayModes]) + ')/$', view_interval, name='view_interval'), path("event/-", EventDetailView.as_view(), name="view_event"), + path("proposer", EventSubmissionFormView.as_view(), name="event_submission_form"), path("admin/", admin.site.urls), path("test_app/", include("test_app.urls")), + ] if settings.DEBUG: diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index a12bfc9..f71f0d9 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -1,5 +1,9 @@ from django.shortcuts import render -from django.views.generic import ListView, DetailView +from django.views.generic import ListView, DetailView, FormView + +from .forms import EventSubmissionModelForm +from .celery import create_event_from_submission + from .models import Event from django.utils import timezone from enum import StrEnum @@ -33,4 +37,20 @@ class EventDetailView(DetailView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["now"] = timezone.now() - return context \ No newline at end of file + return context + + +class EventSubmissionFormView(FormView): + form_class = EventSubmissionModelForm + template_name = "agenda_culturel/submission.html" + success_url = "/" + + def form_valid(self, form): + form.save() + self.create_event(form.cleaned_data) + + return super().form_valid(form) + + def create_event(self, valid_data): + url = valid_data["url"] + create_event_from_submission.delay(url)