On sépare import et renseignement à la main des événements

This commit is contained in:
Jean-Marie Favreau 2024-08-15 15:54:44 +02:00
parent d4cf8b256b
commit b9c63219bb
7 changed files with 119 additions and 28 deletions

View File

@ -23,6 +23,7 @@ from .models import (
ModerationAnswer,
ModerationQuestion,
Place,
Category,
)
from django.utils.translation import gettext_lazy as _
from string import ascii_uppercase as auc
@ -39,6 +40,13 @@ logger = logging.getLogger(__name__)
class URLSubmissionForm(Form):
url = URLField(max_length=512)
category = ModelChoiceField(
label=_("Category"),
queryset=Category.objects.all().order_by("name"),
empty_label=_("Unknown"),
required=False,
)
class DynamicArrayWidgetURLs(DynamicArrayWidget):

View File

@ -18,6 +18,9 @@ $enable-responsive-typography: true;
[role="button"] {
margin: 0.1em 0;
}
[role="button"].large {
width: 100%;
}
:root {
--spacing: 0.4rem;
@ -442,7 +445,7 @@ $yellow-900: #616918 !default;
background-size: $iconsize auto;
padding-left: calc(var(--form-element-spacing-vertical) * 2 + #{$iconsize});
}
.message.danger {
.message.danger, .message.error {
--background-color: #{$red-50};
--icon: var(--icon-invalid);
--color: #{$red-900};

View File

@ -2,7 +2,17 @@
{% load static %}
{% block title %}Modifier {{ object.title }}{% endblock %}
{% block title %}
{% if object %}
Édition de l'événement {{ object.title }} ({{ object.start_day }})
{% else %}
{% if from_import %}
Ajuster l'événement importé
{% else %}
Ajouter un événement
{% endif %}
{% endif %}
{% endblock %}
{% block entete_header %}
<script src="{% url 'jsi18n' %}"></script>
@ -21,6 +31,7 @@
<script src="{% static 'js/django_better_admin_arrayfield.min.js' %}"></script>
<script src="{% static 'js/adjust_datetimes.js' %}"></script>
{% endblock %}
{% block fluid %}{% endblock %}
{% block content %}
@ -32,7 +43,12 @@
{% if object %}
<h1>Édition de l'événement {{ object.title }} ({{ object.start_day }})</h1>
{% else %}
<h1>Édition de l'événement importé</h1>
{% if from_import %}
<h1>Ajuster l'événement importé</h1>
{% else %}
<h1>Ajouter un événement</h1>
<p>Si l'événement est déjà décrit en ligne (par exemple sur Facebook), vous pouvez tenter un <a href="{% url 'add_event_url' %}">import automatique</a>.</p>
{% endif %}
{% endif %}
</header>

View File

@ -0,0 +1,34 @@
{% extends "agenda_culturel/page.html" %}
{% block title %}Ajouter un événement{% endblock %}
{% load cat_extra %}
{% block entete_header %}
{% css_categories %}
{% endblock %}
{% block fluid %}{% endblock %}
{% block content %}
<article>
<header>
<h1>Ajouter un événement</h1>
{% url 'event_import' as local_url %}
{% include "agenda_culturel/static_content.html" with name="import_proxy" url_path=local_url %}
</header>
<p>On peut importer automatiquement un événement depuis d'autres sites internet (principalement depuis Facebook pour
l'instant), à partir de l'adresse (url) de la page de l'événement.
</p>
<a href="{% url 'add_event_url' %}" role="button" class="large">Importer depuis une url</a>
<p>Si l'événement n'est pas disponible en ligne, ou si l'import ne fonctionne pas, on peut
ajouter un événement en utilisant un formulaire complet.
</p>
<a href="{% url 'add_event_details' %}" role="button" class="large">Remplir le formulaire d'événement</a>
</article>
{% endblock %}

View File

@ -23,17 +23,11 @@
{% block content %}
<h1>Ajouter un événement</h1>
<article>
<header>
{% url 'event_import_form' as local_url %}
{% include "agenda_culturel/static_content.html" with name="import" url_path=local_url %}
</header>
</article>
<article>
<header>
<h2>Ajout automatique</h2>
<p>Si l'événement est déjà en ligne sur un autre site internet, on essaye de l'importer...</p>
<h1>Importer un événement</h1>
{% url 'event_import_form' as local_url %}
{% include "agenda_culturel/static_content.html" with name="import" url_path=local_url %}
</header>
<div id="container"></div>
<form method="post" action="">
@ -42,22 +36,13 @@
{{ form.as_p }}
<input type="submit" value="Lancer l'import" id="import-button" data-target="modal-import" onClick="toggleModalNoEscape(event)">
</form>
<p>Si l'import automatique ne marche pas, ou si l'événement n'est pas en ligne, vous pouvez
tout de même ajouter l'événement en le décrivant sur la page <a href="{% url 'add_event_details' %}" >d'ajout d'événement</a>.</p>
</article>
<dialog id="modal-import">
<article aria-busy="true">
Veuillez patienter, lien en cours d'importation...
</article>
</dialog>
<article>
<header>
<h2>Ajout manuel</h2>
<p>Si l'import automatique ne marche pas, ou si l'événement n'est pas en ligne, vous pouvez l'ajouter en remplissant le
formulaire ci-dessous.</p>
</header>
<form method="post">{% csrf_token %}
{{ form_event.as_p }}
<input type="submit" value="Enregistrer">
</form>
</article>
{% endblock %}

View File

@ -37,7 +37,9 @@ urlpatterns = [
name="set_duplicate",
),
path("event/<int:pk>/moderate", EventModerateView.as_view(), name="moderate_event"),
path("ajouter", import_from_url, name="add_event"),
path("ajouter", import_event_proxy, name="add_event"),
path("ajouter/url", import_from_url, name="add_event_url"),
path("ajouter/details", EventCreateView.as_view(), name="add_event_details"),
path("admin/", admin.site.urls),
path("accounts/", include("django.contrib.auth.urls")),
path("test_app/", include("test_app.urls")),

View File

@ -518,13 +518,45 @@ def change_status_event(request, pk, status):
{"status": status, "event": event, "cancel_url": cancel_url},
)
def import_event_proxy(request):
return render(request, "agenda_culturel/event_import.html")
class EventCreateView(SuccessMessageMixin, CreateView):
model = Event
success_url = reverse_lazy("home")
success_message = _("The event has been submitted and will be published as soon as it has been validated by the moderation team.")
form_class = EventForm
def import_from_details(request):
form = EventForm(request.POST, is_authenticated=request.user.is_authenticated)
if form.is_valid():
new_event = form.save()
if request.user.is_authenticated:
messages.success(request, _("The event is saved."))
return HttpResponseRedirect(new_event.get_absolute_url())
else:
messages.success(
request,
_(
"The event has been submitted and will be published as soon as it has been validated by the moderation team."
),
)
return HttpResponseRedirect(reverse("home"))
else:
return render(
request, "agenda_culturel/event_form.html", context={"form": form}
)
def import_from_url(request):
import logging
logging.getLogger(__name__)
# if a form has been sent and contains an event
if request.method == "POST" and "title" in request.POST:
# the result of this form is checked
form = EventForm(request.POST, is_authenticated=request.user.is_authenticated)
if form.is_valid():
new_event = form.save()
@ -555,20 +587,25 @@ def import_from_url(request):
form_event = EventForm(initial=initial)
# if the form has been sent
if request.method == "POST":
form = URLSubmissionForm(request.POST)
# if the form is valid
if form.is_valid():
# the url is cleaned
cd = form.cleaned_data
url = cd.get("url")
url = Extractor.clean_url(url)
# we check if the url is known
existing = Event.objects.filter(uuids__contains=[url])
# if it's unknown
if len(existing) == 0:
event = None
# we import it
logger.warning('on url2event')
u2e = URL2Events(ChromiumHeadlessDownloader(), single_event=True)
events_structure = u2e.process(
url, published=request.user.is_authenticated
@ -582,6 +619,12 @@ def import_from_url(request):
events_structure["events"][0],
events_structure["header"]["url"],
)
# add a category if the user selected one
if event.category is None or event.category == Category.get_default_category():
if cd.get("category") is not None:
event.category = cd.get("category")
# TODO: use celery to import the other events
if event is not None:
@ -599,7 +642,7 @@ def import_from_url(request):
return render(
request,
"agenda_culturel/event_form.html",
context={"form": form},
context={"form": form, "from_import": True},
)
else:
form = EventForm(