From cf980b650eed250d25c04af821ed34e9c8bbcc0b Mon Sep 17 00:00:00 2001 From: Chatewgne Date: Tue, 30 Jul 2024 12:29:01 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20[FEAT]=20Minor=20fixes=20for=20Anno?= =?UTF-8?q?tation=20Categories=20in=20HD=20Views=20(refs=20#4032)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geotrek/api/v2/serializers.py | 1 - geotrek/common/forms.py | 14 +++++++++++++- ...ry.py => 0037_annotationcategory_and_more.py} | 7 ++++++- geotrek/common/static/common/js/annotations.js | 16 ++++++++++++++-- geotrek/common/static/common/style.css | 5 ++++- .../common/hdviewpoint_annotation_form.html | 6 +----- .../templates/common/sql/post_90_defaults.sql | 1 + 7 files changed, 39 insertions(+), 11 deletions(-) rename geotrek/common/migrations/{0037_annotationcategory.py => 0037_annotationcategory_and_more.py} (81%) diff --git a/geotrek/api/v2/serializers.py b/geotrek/api/v2/serializers.py index 6e04942d47..e591e076ee 100644 --- a/geotrek/api/v2/serializers.py +++ b/geotrek/api/v2/serializers.py @@ -410,7 +410,6 @@ def get_annotations(self, obj): feature["properties"]['category'] = None return annotations - class Meta(TimeStampedSerializer.Meta): model = common_models.HDViewPoint fields = TimeStampedSerializer.Meta.fields + ( diff --git a/geotrek/common/forms.py b/geotrek/common/forms.py index 7cf44919c2..2d206871a4 100644 --- a/geotrek/common/forms.py +++ b/geotrek/common/forms.py @@ -549,6 +549,18 @@ def _init_layout(self): formactions, ) + def clean_annotations_categories(self): + data = self.cleaned_data["annotations_categories"] + if data is None: + return {} + return data + + def clean_annotations(self): + data = self.cleaned_data["annotations"] + if data is None: + return {} + return data + class Meta: model = HDViewPoint - fields = ('annotations', 'annotations_categories' ) + fields = ('annotations', 'annotations_categories') diff --git a/geotrek/common/migrations/0037_annotationcategory.py b/geotrek/common/migrations/0037_annotationcategory_and_more.py similarity index 81% rename from geotrek/common/migrations/0037_annotationcategory.py rename to geotrek/common/migrations/0037_annotationcategory_and_more.py index 04e28c8727..d6aaa80801 100644 --- a/geotrek/common/migrations/0037_annotationcategory.py +++ b/geotrek/common/migrations/0037_annotationcategory_and_more.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.13 on 2024-07-23 13:24 +# Generated by Django 4.2.13 on 2024-07-30 09:01 from django.db import migrations, models @@ -25,4 +25,9 @@ class Migration(migrations.Migration): 'ordering': ['label'], }, ), + migrations.AddField( + model_name='hdviewpoint', + name='annotations_categories', + field=models.JSONField(blank=True, default=dict), + ), ] diff --git a/geotrek/common/static/common/js/annotations.js b/geotrek/common/static/common/js/annotations.js index 4c229404cc..9476a55767 100644 --- a/geotrek/common/static/common/js/annotations.js +++ b/geotrek/common/static/common/js/annotations.js @@ -243,7 +243,7 @@ function initAnnotationsWidget(map) { var category_selector = $('#id_annotation_category').clone(); category_selector.attr('for-annotation', id); category_selector.on("change", update_annotation_category_event) - entry.find('.entry-name').after(category_selector); + entry.find('.entry-validate').before(category_selector); } entry.find('.entry-name').text(annotation.name()); if (query.editing == id) { @@ -251,6 +251,13 @@ function initAnnotationsWidget(map) { entry.find('.entry-validate').show(); } $('#annotationlist').append(entry); + if (annotation.type() == 'point' & $("#div_id_annotations_categories textarea").val() != "") { + var annotations_categories = JSON.parse($("#div_id_annotations_categories textarea").val()); + previous_category = annotations_categories[id] + if (typeof previous_category !== 'undefined') { + $('#id_annotation_category[for-annotation="' + id + '"]').val(parseInt(previous_category)) + } + } }); $('#annotationheader').css( 'display', $('#annotationlist .entry').length <= 1 ? 'none' : 'block'); @@ -265,7 +272,11 @@ function initAnnotationsWidget(map) { } function update_annotation_category(annotation_id, category_id) { - var annotations_categories = JSON.parse($("#div_id_annotations_categories textarea").val()) + if ($("#div_id_annotations_categories textarea").val() != "") { + var annotations_categories = JSON.parse($("#div_id_annotations_categories textarea").val()) + } else { + var annotations_categories = {} + } if (category_id == "") { delete annotations_categories[annotation_id] } else { @@ -358,6 +369,7 @@ function initAnnotationsWidget(map) { layer.mode(null); layer.removeAllAnnotations(); layer.mode(mode); + $("#div_id_annotations_categories textarea").val("") fromButtonSelect = false; break; } diff --git a/geotrek/common/static/common/style.css b/geotrek/common/static/common/style.css index ae54c302b1..2b416722e1 100644 --- a/geotrek/common/static/common/style.css +++ b/geotrek/common/static/common/style.css @@ -43,7 +43,7 @@ fieldset { } #id_annotation_category { - width: 25rem; + width: 20rem; } .loader-wrapper { @@ -177,7 +177,10 @@ fieldset { .entry { margin-right: 10px; + margin-bottom: 5px; + overflow: auto; } + .entry .entry-name { margin-left: 10px; margin-right: 10px; diff --git a/geotrek/common/templates/common/hdviewpoint_annotation_form.html b/geotrek/common/templates/common/hdviewpoint_annotation_form.html index 232a5aee1a..e4b3b33898 100644 --- a/geotrek/common/templates/common/hdviewpoint_annotation_form.html +++ b/geotrek/common/templates/common/hdviewpoint_annotation_form.html @@ -34,14 +34,10 @@ {% url 'common:hdviewpoint-drf-detail' object.pk as base_tile_url %} {{object.annotations|json_script:"geojson_annotations"}}
+ {% endblock mainpanel %} - -{% block extrabody %} - {{ block.super}} - -{% endblock extrabody %} diff --git a/geotrek/common/templates/common/sql/post_90_defaults.sql b/geotrek/common/templates/common/sql/post_90_defaults.sql index 1c7858d415..b176a0630a 100644 --- a/geotrek/common/templates/common/sql/post_90_defaults.sql +++ b/geotrek/common/templates/common/sql/post_90_defaults.sql @@ -100,6 +100,7 @@ ALTER TABLE common_hdviewpoint ALTER COLUMN legend SET DEFAULT ''; ALTER TABLE common_hdviewpoint ALTER COLUMN author SET DEFAULT ''; ALTER TABLE common_hdviewpoint ALTER COLUMN uuid SET DEFAULT gen_random_uuid(); ALTER TABLE common_hdviewpoint ALTER COLUMN annotations SET DEFAULT '{}'::jsonb; +ALTER TABLE common_hdviewpoint ALTER COLUMN annotations_categories SET DEFAULT '{}'::jsonb; ALTER TABLE common_hdviewpoint ALTER COLUMN date_insert SET DEFAULT now(); ALTER TABLE common_hdviewpoint ALTER COLUMN date_update SET DEFAULT now();