Skip to content

Commit

Permalink
.funding_names_done
Browse files Browse the repository at this point in the history
  • Loading branch information
silvadealmeida committed Aug 1, 2024
1 parent 7042093 commit 9f4276a
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 4 deletions.
4 changes: 3 additions & 1 deletion geonode/base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)

Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -738,6 +739,7 @@ class Meta:
"files",
"was_approved",
"was_published",
"funders",
)


Expand Down
4 changes: 3 additions & 1 deletion geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
118 changes: 118 additions & 0 deletions geonode/layers/templates/datasets/dataset_metadata_advanced.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,119 @@ <h2 class="page-title">{% trans "Edit Metadata" %}</h2>
{% endblock thesauri %}
</div>

{% block funder_form %}
<label > Fundings</label>
{{funder_form.management_form }}
<div id="DELETE" class="form-group">
<input type="hidden" name="FORM-xx-id" value="1" id="id_FORM-xx-id" hidden>
<input type="checkbox" name="FORM-xx-DELETE" id="id_FORM-xx-DELETE" hidden>

</div>
<div class="panel ">
<!-- Tab panes -->
<div class="panel-heading panel-heading-nav">
<ul class="nav nav-tabs allTabs">
{% for form in funder_form %}
{% if forloop.counter == 1 %}
<li role="presentation" class="active">
<a href="#{{ form.prefix }}" aria-controls="{{ form.prefix }}" role="tab" data-toggle="tab">
<span class="tabTex">{{ forloop.counter }} </span>
<button type="button" class="nav-remove"><icon class="fa fa-minus-circle"></icon>
</button></a>
</li>
{% else %}
<li role="presentation" class="">
<a href="#{{ form.prefix }}" aria-controls="{{ form.prefix }}" role="tab" data-toggle="tab">
<span class="tabTex">{{ forloop.counter }} </span>
<button type="button" class="nav-remove"><icon class="fa fa-minus-circle"></icon>
</button></a>
</li>
{% endif %}
{%endfor%}
<li role="presentation" class="nav-item enpt tab li-add" >
<a ><button id="nav-add" class="btn-primary nav-add " type="button"><icon class="fa fa-plus-circle"> new </button></a>
</li>
<li role="presentation" class="nav-item enpt tab nav-empty templateTab hidden" >
<a href="" aria-controls="{{ form.prefix }}" role="tab" data-toggle="tab">
<span class="newTabTex"></span>
<button type="button" class="nav-remove"><icon class="fa fa-minus-circle"></icon>
</button></a>
</li>

</ul>
</div>
<!-- Tab panes -->
<div class="tab-content allContent">
{% for form in funder_form %}
{% if forloop.counter == 1 %}
<div class="tab-pane active" id="{{ form.prefix }}" role="tabpanel" >
<div>
{% if forloop.first %}

{% for field in form.visible_fields %}

<label for="{{ field.name }}">{{ field.label|capfirst }}</label>
<div id="{{ field.name }}" class="form-group">
{{ field }}
{{ field.errors.as_ul }}
</div>

{% endfor %}
{% endif %}
{% for hidden in form.hidden_fields %}
<div>
{{ hidden }}
</div>
{% endfor %}
</div>
</div>
{% else %}
<div class="tab-pane " id="{{ form.prefix }}" role="tabpanel" >
<div>

{% for field in form.visible_fields %}
<label for="{{ field.name }}">{{ field.label|capfirst }}</label>
<div id="{{ field.name }}" class="form-group">
{{ field }}
{{ field.errors.as_ul }}
</div>
{% endfor %}
{% for hidden in form.hidden_fields %}
<div>
{{ hidden }}
</div>
{% endfor %}
</div>
</div>
{% endif %}
{% endfor %}

<div class="tab-pane hidden templateContent" id="templateContent" role="tabpanel" >
<div>

{% for field in funder_form.empty_form.visible_fields %}
<label for="{{ field.name }}">{{ field.label|capfirst }}</label>
<div id="{{ field.name }}" class="form-group">
{{ field }}
{{ field.errors.as_ul }}
</div>
{% endfor %}
{% for hidden in funder_form.empty_form.hidden_fields %}
<div>
{{ hidden }}
</div>
{% endfor %}
</div>
</div>

</div>
</div>

{% endblock funder_form %}




{% block category %}
<div class="row">
<div class="col-md-12">
Expand Down Expand Up @@ -263,4 +376,9 @@ <h4 class="modal-title">{% trans "ERROR" %}</h4>
}
});
</script>
<script>
let prefix = "{{funder_form.prefix}}";
</script>

<script type="text/javascript" src="{% static "geonode/js/utils/formsetsInTabs.js" %}"></script>
{% endblock %}
25 changes: 23 additions & 2 deletions geonode/layers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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()
Expand All @@ -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"]
Expand Down Expand Up @@ -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"),
Expand Down
120 changes: 120 additions & 0 deletions geonode/static/geonode/js/utils/formsetsInTabs.js
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 9f4276a

Please sign in to comment.