From 9f4276a02dff1a874fdd914e9d1df36e83ec8025 Mon Sep 17 00:00:00 2001 From: silvadealmeida Date: Thu, 1 Aug 2024 11:09:19 +0200 Subject: [PATCH] .funding_names_done --- geonode/base/forms.py | 4 +- geonode/base/models.py | 4 +- .../datasets/dataset_metadata_advanced.html | 118 +++++++++++++++++ geonode/layers/views.py | 25 +++- .../static/geonode/js/utils/formsetsInTabs.js | 120 ++++++++++++++++++ 5 files changed, 267 insertions(+), 4 deletions(-) create mode 100644 geonode/static/geonode/js/utils/formsetsInTabs.js diff --git a/geonode/base/forms.py b/geonode/base/forms.py index 048c9bf5daf..62bc078506a 100644 --- a/geonode/base/forms.py +++ b/geonode/base/forms.py @@ -418,7 +418,7 @@ class ResourceBaseForm(TranslationModelForm, LinkedResourceForm): abstract_translated = forms.CharField( label=_("translated abstract"), help_text=ResourceBase.abstract_translated_help_text, - required=True, + required=False, widget=TinyMCE(), ) @@ -450,6 +450,7 @@ class ResourceBaseForm(TranslationModelForm, LinkedResourceForm): widget=ResourceBaseDateTimePicker(options={"format": "YYYY-MM-DD"}), ) date_issued = forms.DateTimeField( + required=False, label=_("Date Issued*"), localize=True, input_formats=["%Y-%m-%d"], @@ -738,6 +739,7 @@ class Meta: "files", "was_approved", "was_published", + "funders", ) diff --git a/geonode/base/models.py b/geonode/base/models.py index 90d666e4251..858e25bbf29 100644 --- a/geonode/base/models.py +++ b/geonode/base/models.py @@ -846,7 +846,9 @@ class ResourceBase(PolymorphicModel, PermissionLevelMixin, ItemBase): # internal fields uuid = models.CharField(max_length=36, unique=True, default=uuid.uuid4) title = models.CharField(_("title"), max_length=255, help_text=_("name by which the cited resource is known")) - title_translated = models.CharField(_("title_translated"), max_length=255, help_text=title_translated_help_text) + title_translated = models.CharField( + _("title_translated"), max_length=255, blank=True, help_text=title_translated_help_text + ) abstract = models.TextField(_("abstract"), max_length=2000, help_text=abstract_help_text) abstract_translated = models.TextField( diff --git a/geonode/layers/templates/datasets/dataset_metadata_advanced.html b/geonode/layers/templates/datasets/dataset_metadata_advanced.html index c7169f960cb..d1c4b202bce 100644 --- a/geonode/layers/templates/datasets/dataset_metadata_advanced.html +++ b/geonode/layers/templates/datasets/dataset_metadata_advanced.html @@ -127,6 +127,119 @@

{% trans "Edit Metadata" %}

{% endblock thesauri %} + {% block funder_form %} + + {{funder_form.management_form }} +
+ + + +
+
+ +
+ +
+ +
+ {% for form in funder_form %} + {% if forloop.counter == 1 %} +
+
+ {% if forloop.first %} + + {% for field in form.visible_fields %} + + +
+ {{ field }} + {{ field.errors.as_ul }} +
+ + {% endfor %} + {% endif %} + {% for hidden in form.hidden_fields %} +
+ {{ hidden }} +
+ {% endfor %} +
+
+ {% else %} +
+
+ + {% for field in form.visible_fields %} + +
+ {{ field }} + {{ field.errors.as_ul }} +
+ {% endfor %} + {% for hidden in form.hidden_fields %} +
+ {{ hidden }} +
+ {% endfor %} +
+
+ {% endif %} + {% endfor %} + + + +
+
+ + {% endblock funder_form %} + + + + {% block category %}
@@ -263,4 +376,9 @@ } }); + + + {% endblock %} \ No newline at end of file diff --git a/geonode/layers/views.py b/geonode/layers/views.py index 709553b9964..b5ed1a52584 100644 --- a/geonode/layers/views.py +++ b/geonode/layers/views.py @@ -36,7 +36,7 @@ from django.contrib.auth import get_user_model from django.utils.translation import ugettext as _ from django.core.exceptions import PermissionDenied -from django.forms.models import inlineformset_factory +from django.forms.models import inlineformset_factory, modelformset_factory from django.template.response import TemplateResponse from django.contrib.auth.decorators import login_required from django.views.decorators.csrf import csrf_exempt @@ -53,7 +53,7 @@ from geonode.base.auth import get_or_create_token from geonode.base.forms import CategoryForm, TKeywordForm, ThesaurusAvailableForm from geonode.base.views import batch_modify -from geonode.base.models import Thesaurus, TopicCategory +from geonode.base.models import Thesaurus, TopicCategory, Funder from geonode.base.enumerations import CHARSETS from geonode.decorators import check_keyword_write_perms from geonode.layers.forms import DatasetForm, DatasetTimeSerieForm, LayerAttributeForm, NewLayerUploadForm @@ -355,6 +355,14 @@ def dataset_metadata( extra=0, form=LayerAttributeForm, ) + + FunderFormset = modelformset_factory( + Funder, + fields=["funding_reference", "award_number", "award_uri", "award_title"], + can_delete=True, + extra=0, + min_num=1, + ) current_keywords = [keyword.name for keyword in layer.keywords.all()] topic_category = layer.category @@ -411,6 +419,11 @@ def dataset_metadata( "errors": [re.sub(re.compile("<.*?>"), "", str(err)) for err in attribute_form.errors], } return HttpResponse(json.dumps(out), content_type="application/json", status=400) + funders_intial_values = Funder.objects.all().filter(resourcebase=layer) + funder_form = FunderFormset( + request.POST, + prefix="funder_form", + ) category_form = CategoryForm( request.POST, prefix="category_choice_field", @@ -468,6 +481,9 @@ def dataset_metadata( attribute_form = dataset_attribute_set( instance=layer, prefix="dataset_attribute_set", queryset=Attribute.objects.order_by("display_order") ) + + funders_intial_values = Funder.objects.all().filter(resourcebase=layer) + funder_form = FunderFormset(prefix="funder_form", queryset=funders_intial_values) category_form = CategoryForm( prefix="category_choice_field", initial=topic_category.id if topic_category else None ) @@ -538,6 +554,7 @@ def dataset_metadata( request.method == "POST" and dataset_form.is_valid() and attribute_form.is_valid() + and funder_form.is_valid() and category_form.is_valid() and tkeywords_form.is_valid() and timeseries_form.is_valid() @@ -561,6 +578,9 @@ def dataset_metadata( # update contact roles layer.set_contact_roles_from_metadata_edit(dataset_form) + funder_form.save() + instance = funder_form.save(commit=False) + layer.funders.add(*instance) layer.save() new_keywords = current_keywords if request.keyword_readonly else dataset_form.cleaned_data["keywords"] @@ -673,6 +693,7 @@ def dataset_metadata( "dataset_form": dataset_form, "attribute_form": attribute_form, "timeseries_form": timeseries_form, + "funder_form": funder_form, "category_form": category_form, "tkeywords_form": tkeywords_form, "preview": getattr(settings, "GEONODE_CLIENT_LAYER_PREVIEW_LIBRARY", "mapstore"), diff --git a/geonode/static/geonode/js/utils/formsetsInTabs.js b/geonode/static/geonode/js/utils/formsetsInTabs.js new file mode 100644 index 00000000000..5b78f4ac0be --- /dev/null +++ b/geonode/static/geonode/js/utils/formsetsInTabs.js @@ -0,0 +1,120 @@ + + +let totalForms = $('#id_' + prefix + '-TOTAL_FORMS').val(); +let initialForms = $('#id_' + prefix + '-INITIAL_FORMS').val(); +let maxForms = $('#id_' + prefix + '-MAX_NUM_FORMS').val(); +let minForms = $('#id_' + prefix + '-MIN_NUM_FORMS').val(); +let deleteInput = $("#id_FORM-xx-DELETE") +let hiddenInput = $("#id_FORM-xx-id") + +let allDelete = $('#DELETE') + +let actualForms = totalForms + +let templateTab = $('.templateTab') +let templateContent = $('.templateContent') +let allTabs = $('.allTabs') +let allforms = $('.allContent') + +reOrder() +hideDeleteCheckbox() + +$("#nav-add").on("click", function () { + label = Number(actualForms) + 1 + removeActive() + newTab = templateTab.clone(true).removeClass('hidden') + newTab.removeClass('templateTab').removeClass('nav-empty') + newTab.attr('id', '') + newTab.find('a').attr('href', '#' + prefix + '-' + actualForms) + newTab.attr('aria-controls', prefix + '-' + actualForms) + newTab.find('.newTabTex').text(label) + newTab.find('.newTabTex').addClass('tabTex').removeClass('newTabTex') + newTab.insertBefore($('.li-add')) + newTab.addClass('active') + + + newContent = templateContent.clone(true).removeClass('hidden') + newContent.removeClass('templateContent').removeClass('nav-empty') + newContent.attr('id', prefix + '-' + actualForms) + newContent.addClass('in active') + newContent.find('select, input').each( + function () { + $(this).attr('name', $(this).attr('name').replace("__prefix__", actualForms)) + $(this).attr('id', $(this).attr('id').replace("__prefix__", actualForms)) + }) + newContent.insertBefore($('.templateContent')) + + actualForms++ + $('#id_' + prefix + '-TOTAL_FORMS').attr("value", actualForms) +}); + + +$(".nav-remove").on("click", function () { + removeActive() + number = $(this).parent('a').attr('href').split('-')[1] + tabToRemove = $(this).parent('a').parent('li') + + tabToRemove.remove() + contentToRemove = $('#' + prefix + '-' + number) + contentToRemove.find('#DELETE input').prop("checked", true) + contentToRemove.removeAttr('role') + toDjango = contentToRemove.children('div') + toDjango.hide().insertAfter(templateContent) + contentToRemove.remove() + $('.allContent').find('.tab-pane').first().addClass('active') + $('.allTabs').find('li:first').addClass('active').find('a').attr('aria-expanded', true) + reOrder() + actualForms-- + + +}); + +function reOrder() { + counter = 0 + $('.allTabs').find('li:first').addClass('active').find('a').attr('aria-expanded', true) + + $('.allTabs').find('li a').each( + function () { + $(this).attr('href', '#' + prefix + '-' + counter) + $(this).attr('aria-controls', prefix + '-' + counter) + counterLabel = counter + 1 + $(this).find('.tabTex').text(counterLabel) + counter++ + } + ) + + counterCont = 0 + $('.allContent').find('.tab-pane').each( + function () { + if ($(this).attr('id') != 'templateContent') { + $(this).attr('id', prefix + '-' + counterCont) + $(this).find('#DELETE').find('input').attr('id', 'id_' + prefix + '-' + counterCont + '-DELETE') + $(this).find('#DELETE').find('input').attr('name', prefix + '-' + counterCont + '-DELETE') + $(this).find('div div:last').find('input').attr('id', 'id_' + prefix + '-' + counterCont + '-id') + $(this).find('div div:last').find('input').attr('name', prefix + '-' + counterCont + '-id') + counterCont++ + } + + } + ) + +} + +function removeActive() { + $('.allTabs').find('li').each( + function () { + $(this).removeClass('active') + $(this).find('a').attr('aria-expanded', false) + }) + $('.allContent').find('.tab-pane').each( + function () { + + $(this).removeClass('active') + }) + +} + +function hideDeleteCheckbox() { + allforms.find('#DELETE').hide() + allforms.find('#DELETE').prev().hide() +}