From ebfcf8823c88a28bc61e1356d8f49bed1027b7c8 Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sat, 21 Sep 2024 21:53:52 +0200 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20UX=20gestion=20r=C3=A9curre?= =?UTF-8?q?nts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix #148 (plus simple) --- src/agenda_culturel/celery.py | 9 ++++ .../templates/agenda_culturel/rimports.html | 35 ++++++++++++-- .../run_canceled_rimports_confirm.html | 27 +++++++++++ .../run_failed_rimports_confirm.html | 4 +- .../templatetags/rimports_extra.py | 2 +- src/agenda_culturel/urls.py | 3 ++ src/agenda_culturel/views.py | 48 +++++++++++++++---- 7 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 src/agenda_culturel/templates/agenda_culturel/run_canceled_rimports_confirm.html diff --git a/src/agenda_culturel/celery.py b/src/agenda_culturel/celery.py index 791977e..c51cfd6 100644 --- a/src/agenda_culturel/celery.py +++ b/src/agenda_culturel/celery.py @@ -264,6 +264,15 @@ def run_all_recurrent_imports_failed(self): run_recurrent_imports_from_list([imp.pk for imp in imports if imp.last_import().status == BatchImportation.STATUS.FAILED]) +@app.task(bind=True) +def run_all_recurrent_imports_canceled(self): + from agenda_culturel.models import RecurrentImport, BatchImportation + + logger.info("Run only canceled imports") + imports = RecurrentImport.objects.all().order_by("pk") + + run_recurrent_imports_from_list([imp.pk for imp in imports if imp.last_import().status == BatchImportation.STATUS.CANCELED]) + @app.task(bind=True) def weekly_imports(self): diff --git a/src/agenda_culturel/templates/agenda_culturel/rimports.html b/src/agenda_culturel/templates/agenda_culturel/rimports.html index a83b3a5..9e1ded4 100644 --- a/src/agenda_culturel/templates/agenda_culturel/rimports.html +++ b/src/agenda_culturel/templates/agenda_culturel/rimports.html @@ -1,6 +1,7 @@ {% extends "agenda_culturel/page.html" %} -{% block title %}{% block og_title %}Importations récurrentes{% endblock %}{% endblock %} +{% block title %}{% block og_title %}Importations récurrentes +{% if status %}{{ status }}{% endif %}{% endblock %}{% endblock %} {% load utils_extra %} {% load rimports_extra %} @@ -14,13 +15,37 @@
-

Importations récurrentes

+

Importations récurrentes {% if status %}{{ status }}{% endif %}

+ {% if status %} + tous ({{ nb_all }}) + {% endif %} + {% if not status or status != "failed" %} + {% if nb_failed == 0 %} +
échoués ({{ nb_failed }})
+ {% else %} + échoués ({{ nb_failed }}) + {% endif %} + {% endif %} + {% if not status or status != "canceled" %} + {% if nb_canceled == 0 %} +
annulés ({{ nb_canceled }})
+ {% else %} + annulés ({{ nb_canceled }}) + {% endif %} + {% endif %}
diff --git a/src/agenda_culturel/templates/agenda_culturel/run_canceled_rimports_confirm.html b/src/agenda_culturel/templates/agenda_culturel/run_canceled_rimports_confirm.html new file mode 100644 index 0000000..20373ab --- /dev/null +++ b/src/agenda_culturel/templates/agenda_culturel/run_canceled_rimports_confirm.html @@ -0,0 +1,27 @@ +{% extends "agenda_culturel/page.html" %} + +{% block title %}{% block og_title %}Lancer tous les imports annulés{% endblock %}{% endblock %} + +{% block fluid %}{% endblock %} + + +{% block content %} + +
+
+

Lancement des imports en annulés

+
+
{% csrf_token %} +

Êtes-vous sûr·e de vouloir lancer tous les imports récurrent ayant été annulés lors du dernier import ?

+ {{ form }} + + + +
+ +{% endblock %} \ No newline at end of file diff --git a/src/agenda_culturel/templates/agenda_culturel/run_failed_rimports_confirm.html b/src/agenda_culturel/templates/agenda_culturel/run_failed_rimports_confirm.html index 6d59d08..421a1d5 100644 --- a/src/agenda_culturel/templates/agenda_culturel/run_failed_rimports_confirm.html +++ b/src/agenda_culturel/templates/agenda_culturel/run_failed_rimports_confirm.html @@ -1,6 +1,8 @@ {% extends "agenda_culturel/page.html" %} -{% block title %}{% block og_title %}Lancer tous les imports{% endblock %}{% endblock %} +{% block title %}{% block og_title %}Lancer tous les imports échoués{% endblock %}{% endblock %} + +{% block fluid %}{% endblock %} {% block content %} diff --git a/src/agenda_culturel/templatetags/rimports_extra.py b/src/agenda_culturel/templatetags/rimports_extra.py index fa8aa59..a242acb 100644 --- a/src/agenda_culturel/templatetags/rimports_extra.py +++ b/src/agenda_culturel/templatetags/rimports_extra.py @@ -28,7 +28,7 @@ def show_badge_failed_rimports(placement="top"): if nb_failed != 0: return mark_safe( '/cancel", cancel_import, name="cancel_import"), path("rimports/", recurrent_imports, name="recurrent_imports"), path("rimports/run", run_all_rimports, name="run_all_rimports"), + path("rimports/failed", recurrent_imports_failed, name="recurrent_imports_failed"), path("rimports/failed/run", run_all_rimports_failed, name="run_all_rimports_failed"), + path("rimports/canceled", recurrent_imports_canceled, name="recurrent_imports_canceled"), + path("rimports/canceled/run", run_all_rimports_canceled, name="run_all_rimports_canceled"), path("rimports/add", RecurrentImportCreateView.as_view(), name="add_rimport"), path("rimports//view", view_rimport, name="view_rimport"), path( diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index 75fe1cb..706f3aa 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -76,6 +76,7 @@ from .celery import ( run_recurrent_import, run_all_recurrent_imports, run_all_recurrent_imports_failed, + run_all_recurrent_imports_canceled, import_events_from_url, import_events_from_urls, ) @@ -1146,23 +1147,44 @@ def cancel_import(request, pk): ## recurrent importations ######################### +@login_required(login_url="/accounts/login/") +@permission_required("agenda_culturel.view_recurrentimport") +def recurrent_imports_failed(request): + return recurrent_imports(request, BatchImportation.STATUS.FAILED) @login_required(login_url="/accounts/login/") @permission_required("agenda_culturel.view_recurrentimport") -def recurrent_imports(request): - paginator = Paginator(RecurrentImport.objects.all().order_by("-pk"), 10) - page = request.GET.get("page") +def recurrent_imports_canceled(request): + return recurrent_imports(request, BatchImportation.STATUS.CANCELED) + +@login_required(login_url="/accounts/login/") +@permission_required("agenda_culturel.view_recurrentimport") +def recurrent_imports(request, status=None): newest = BatchImportation.objects.filter(recurrentImport=OuterRef("pk")).order_by( "-created_date" ) - has_failed_import = nb_failed = ( - RecurrentImport.objects.annotate( + events = RecurrentImport.objects.annotate( last_run_status=Subquery(newest.values("status")[:1]) ) + + if status is None: + events_selected = events.all() + else: + events_selected = events.filter(last_run_status=status) + events_selected = events_selected.order_by("-pk") + + paginator = Paginator(events_selected, 10) + page = request.GET.get("page") + + nb_failed = (events .filter(last_run_status=BatchImportation.STATUS.FAILED) - .count() - ) > 0 + .count()) + nb_canceled = (events + .filter(last_run_status=BatchImportation.STATUS.CANCELED) + .count()) + nb_all = events.count() + try: response = paginator.page(page) @@ -1172,7 +1194,7 @@ def recurrent_imports(request): response = paginator.page(paginator.num_pages) return render( - request, "agenda_culturel/rimports.html", {"paginator_filter": response, "has_failed_import": has_failed_import} + request, "agenda_culturel/rimports.html", {"paginator_filter": response, "nb_all": nb_all, "nb_failed": nb_failed, "nb_canceled": nb_canceled, "status": status} ) @@ -1276,6 +1298,16 @@ def run_all_rimports_failed(request): else: return render(request, "agenda_culturel/run_failed_rimports_confirm.html") +def run_all_rimports_canceled(request): + if request.method == "POST": + # run recurrent import + run_all_recurrent_imports_canceled.delay() + + messages.success(request, _("Imports has been launched.")) + return HttpResponseRedirect(reverse_lazy("recurrent_imports")) + else: + return render(request, "agenda_culturel/run_canceled_rimports_confirm.html") + ######################### ## duplicated events