From 7dfe98589d0d02ee5ae652a44f97fdb759ead3c9 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Thu, 26 Oct 2023 14:22:45 +0200 Subject: [PATCH] Label : pouvoir uploader un label dans l'admin (#946) * Label admin: add logo upload form (with S3 dropzone) * Add has_logo field in list --- config/settings/base.py | 4 +++ lemarche/labels/admin.py | 28 +++++++++++++++-- lemarche/labels/models.py | 4 +++ .../templates/labels/admin_change_form.html | 30 +++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 lemarche/templates/labels/admin_change_form.html diff --git a/config/settings/base.py b/config/settings/base.py index 283dda19a..c382f90b4 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -460,6 +460,7 @@ SIAE_LOGO_FOLDER_NAME = "siae_logo" SIAE_IMAGE_FOLDER_NAME = "siae_image" SIAE_CLIENT_REFERENCE_LOGO_FOLDER_NAME = "client_reference_logo" +LABEL_LOGO_FOLDER_NAME = "label_logo" USER_IMAGE_FOLDER_NAME = "user_image" SIAE_EXPORT_FOLDER_NAME = "siae_export" STAT_EXPORT_FOLDER_NAME = "stat_export" @@ -482,6 +483,9 @@ "client_reference_logo": { "key_path": SIAE_CLIENT_REFERENCE_LOGO_FOLDER_NAME, }, + "label_logo": { + "key_path": LABEL_LOGO_FOLDER_NAME, + }, "user_image": { "key_path": USER_IMAGE_FOLDER_NAME, }, diff --git a/lemarche/labels/admin.py b/lemarche/labels/admin.py index ee5293996..43707b121 100644 --- a/lemarche/labels/admin.py +++ b/lemarche/labels/admin.py @@ -5,16 +5,18 @@ from lemarche.labels.models import Label from lemarche.utils.admin.admin_site import admin_site from lemarche.utils.fields import pretty_print_readonly_jsonfield +from lemarche.utils.s3 import S3Upload @admin.register(Label, site=admin_site) class LabelAdmin(admin.ModelAdmin): - list_display = ["id", "name", "siae_count_annotated_with_link", "created_at"] + list_display = ["id", "name", "siae_count_annotated_with_link", "has_logo", "created_at"] search_fields = ["id", "name", "description"] search_help_text = "Cherche sur les champs : ID, Nom, Description" readonly_fields = [ "siae_count_annotated_with_link", + "has_logo", "logo_url_display", "data_last_sync_date", "logs_display", @@ -29,7 +31,7 @@ class LabelAdmin(admin.ModelAdmin): "fields": ("name", "slug", "description", "website"), }, ), - ("Logo", {"fields": ("logo_url", "logo_url_display")}), + ("Logo (voir en bas pour uploader)", {"fields": ("logo_url", "logo_url_display")}), ("Structures", {"fields": ("siae_count_annotated_with_link",)}), ( "Source de données", @@ -44,6 +46,22 @@ class LabelAdmin(admin.ModelAdmin): ("Dates", {"fields": ("created_at", "updated_at")}), ) + change_form_template = "labels/admin_change_form.html" + + class Media: + js = ( + "https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js", + "vendor/dropzone-5.9.3/dropzone.min.js", + "js/s3_upload.js", + ) + + def change_view(self, request, object_id, form_url="", extra_context=None): + extra_context = extra_context or {} + s3_upload = S3Upload(kind="label_logo") + extra_context["s3_form_values_label_logo"] = s3_upload.form_values + extra_context["s3_upload_config_label_logo"] = s3_upload.config + return super(LabelAdmin, self).change_view(request, object_id, form_url, extra_context=extra_context) + def get_queryset(self, request): qs = super().get_queryset(request) qs = qs.with_siae_stats() @@ -61,6 +79,12 @@ def get_prepopulated_fields(self, request, obj=None): return {"slug": ("name",)} return {} + def has_logo(self, instance): + return instance.has_logo + + has_logo.boolean = True + has_logo.short_description = "Logo ?" + def logo_url_display(self, instance): if instance.logo_url: return mark_safe( diff --git a/lemarche/labels/models.py b/lemarche/labels/models.py index 65ea850fe..71b693b91 100644 --- a/lemarche/labels/models.py +++ b/lemarche/labels/models.py @@ -44,3 +44,7 @@ def save(self, *args, **kwargs): """Generate the slug field before saving.""" self.set_slug() super().save(*args, **kwargs) + + @property + def has_logo(self): + return len(self.logo_url) > 0 diff --git a/lemarche/templates/labels/admin_change_form.html b/lemarche/templates/labels/admin_change_form.html new file mode 100644 index 000000000..e7fd55323 --- /dev/null +++ b/lemarche/templates/labels/admin_change_form.html @@ -0,0 +1,30 @@ +{% extends 'admin/change_form.html' %} +{% load static bootstrap4 theme_inclusion %} + +{% block after_related_objects %} + {{ block.super }} + + {% import_static_JS_theme_inclusion %} + +
+
+ + {% include "storage/s3_upload_form.html" with dropzone_form_id="logo_form" %} +
+
+ + {{ s3_form_values_label_logo|json_script:"s3-form-values-label-logo" }} + {{ s3_upload_config_label_logo|json_script:"s3-upload-config-label-logo" }} + +{% endblock %}