Skip to content

Commit

Permalink
[Closes #4584] Allow admins to decide if the Topic Category should be…
Browse files Browse the repository at this point in the history
… mandatory or not
  • Loading branch information
afabiani committed Jun 27, 2019
1 parent 765ab23 commit 96f9f00
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 29 deletions.
11 changes: 7 additions & 4 deletions geonode/base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from autocomplete_light.contrib.taggit_field import TaggitField, TaggitWidget

from django import forms
from django.conf import settings
from django.core import validators
from django.forms import models
from django.forms.fields import ChoiceField
Expand Down Expand Up @@ -200,10 +201,12 @@ def render_option_value(
label)

def render_options(self, selected_choices):

# Normalize to strings.
def _region_id_from_choice(choice):
if isinstance(choice, int):
return choice
if isinstance(choice, int) or \
(isinstance(choice, basestring) and choice.isdigit()):
return int(choice)
else:
return choice.id

Expand Down Expand Up @@ -274,8 +277,8 @@ class CategoryForm(forms.Form):
def clean(self):
cleaned_data = self.data
ccf_data = cleaned_data.get("category_choice_field")

if not ccf_data:
category_mandatory = getattr(settings, 'TOPICCATEGORY_MANDATORY', False)
if category_mandatory and not ccf_data:
msg = _("Category is required.")
self._errors = self.error_class([msg])

Expand Down
4 changes: 4 additions & 0 deletions geonode/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def resource_urls(request):
settings,
'ADMIN_MODERATE_UPLOADS',
False),
TOPICCATEGORY_MANDATORY=getattr(
settings,
'TOPICCATEGORY_MANDATORY',
False),
GROUP_MANDATORY_RESOURCES=getattr(
settings,
'GROUP_MANDATORY_RESOURCES',
Expand Down
12 changes: 9 additions & 3 deletions geonode/documents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ def document_metadata(
instance=document,
prefix="resource")
category_form = CategoryForm(request.POST, prefix="category_choice_field", initial=int(
request.POST["category_choice_field"]) if "category_choice_field" in request.POST else None)
request.POST["category_choice_field"]) if "category_choice_field" in request.POST and
request.POST["category_choice_field"] else None)
else:
document_form = DocumentForm(instance=document, prefix="resource")
category_form = CategoryForm(
Expand All @@ -376,8 +377,12 @@ def document_metadata(
new_author = document_form.cleaned_data['metadata_author']
new_keywords = document_form.cleaned_data['keywords']
new_regions = document_form.cleaned_data['regions']
new_category = TopicCategory.objects.get(
id=category_form.cleaned_data['category_choice_field'])

new_category = None
if category_form and 'category_choice_field' in category_form.cleaned_data and\
category_form.cleaned_data['category_choice_field']:
new_category = TopicCategory.objects.get(
id=int(category_form.cleaned_data['category_choice_field']))

if new_poc is None:
if poc is None:
Expand Down Expand Up @@ -495,6 +500,7 @@ def document_metadata(
"author_form": author_form,
"category_form": category_form,
"metadata_author_groups": metadata_author_groups,
"TOPICCATEGORY_MANDATORY": getattr(settings, 'TOPICCATEGORY_MANDATORY', False),
"GROUP_MANDATORY_RESOURCES": getattr(settings, 'GROUP_MANDATORY_RESOURCES', False),
})

Expand Down
21 changes: 12 additions & 9 deletions geonode/layers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,8 @@ def layer_metadata(
prefix="layer_attribute_set",
queryset=Attribute.objects.order_by('display_order'))
category_form = CategoryForm(request.POST, prefix="category_choice_field", initial=int(
request.POST["category_choice_field"]) if "category_choice_field" in request.POST else None)
request.POST["category_choice_field"]) if "category_choice_field" in request.POST and
request.POST["category_choice_field"] else None)
tkeywords_form = TKeywordForm(
request.POST,
prefix="tkeywords")
Expand Down Expand Up @@ -1044,8 +1045,11 @@ def layer_metadata(
if author_form.has_changed and author_form.is_valid():
new_author = author_form.save()

new_category = TopicCategory.objects.get(
id=category_form.cleaned_data['category_choice_field'])
new_category = None
if category_form and 'category_choice_field' in category_form.cleaned_data and\
category_form.cleaned_data['category_choice_field']:
new_category = TopicCategory.objects.get(
id=int(category_form.cleaned_data['category_choice_field']))

for form in attribute_form.cleaned_data:
la = Attribute.objects.get(id=int(form['id'].id))
Expand Down Expand Up @@ -1078,10 +1082,9 @@ def layer_metadata(
if up_sessions.count() > 0 and up_sessions[0].user != the_layer.owner:
up_sessions.update(user=the_layer.owner)

if new_category is not None:
Layer.objects.filter(id=the_layer.id).update(
category=new_category
)
Layer.objects.filter(id=the_layer.id).update(
category=new_category
)

if not ajax:
return HttpResponseRedirect(
Expand Down Expand Up @@ -1196,8 +1199,8 @@ def layer_metadata(
'FREETEXT_KEYWORDS_READONLY',
False),
"metadata_author_groups": metadata_author_groups,
"GROUP_MANDATORY_RESOURCES":
getattr(settings, 'GROUP_MANDATORY_RESOURCES', False),
"TOPICCATEGORY_MANDATORY": getattr(settings, 'TOPICCATEGORY_MANDATORY', False),
"GROUP_MANDATORY_RESOURCES": getattr(settings, 'GROUP_MANDATORY_RESOURCES', False),
})


Expand Down
12 changes: 9 additions & 3 deletions geonode/maps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ def map_metadata(
if request.method == "POST":
map_form = MapForm(request.POST, instance=map_obj, prefix="resource")
category_form = CategoryForm(request.POST, prefix="category_choice_field", initial=int(
request.POST["category_choice_field"]) if "category_choice_field" in request.POST else None)
request.POST["category_choice_field"]) if "category_choice_field" in request.POST and
request.POST["category_choice_field"] else None)
else:
map_form = MapForm(instance=map_obj, prefix="resource")
category_form = CategoryForm(
Expand All @@ -213,8 +214,12 @@ def map_metadata(
new_regions = map_form.cleaned_data['regions']
new_title = strip_tags(map_form.cleaned_data['title'])
new_abstract = strip_tags(map_form.cleaned_data['abstract'])
new_category = TopicCategory.objects.get(
id=category_form.cleaned_data['category_choice_field'])

new_category = None
if category_form and 'category_choice_field' in category_form.cleaned_data and\
category_form.cleaned_data['category_choice_field']:
new_category = TopicCategory.objects.get(
id=int(category_form.cleaned_data['category_choice_field']))

if new_poc is None:
if poc is None:
Expand Down Expand Up @@ -334,6 +339,7 @@ def map_metadata(
"preview": getattr(settings, 'GEONODE_CLIENT_LAYER_PREVIEW_LIBRARY', 'geoext'),
"crs": getattr(settings, 'DEFAULT_MAP_CRS', 'EPSG:3857'),
"metadata_author_groups": metadata_author_groups,
"TOPICCATEGORY_MANDATORY": getattr(settings, 'TOPICCATEGORY_MANDATORY', False),
"GROUP_MANDATORY_RESOURCES": getattr(settings, 'GROUP_MANDATORY_RESOURCES', False),
})

Expand Down
4 changes: 4 additions & 0 deletions geonode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,10 @@
# absolutely need it set to True this variable
MODIFY_TOPICCATEGORY = ast.literal_eval(os.getenv('MODIFY_TOPICCATEGORY', 'True'))

# If this option is enabled, Topic Categories will become strictly Mandatory on
# Metadata Wizard
TOPICCATEGORY_MANDATORY = ast.literal_eval(os.environ.get('TOPICCATEGORY_MANDATORY', 'False'))

MISSING_THUMBNAIL = os.getenv(
'MISSING_THUMBNAIL', 'geonode/img/missing_thumb.png'
)
Expand Down
46 changes: 36 additions & 10 deletions geonode/templates/metadata_form_js.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
var metadata_update_done = false;
var metadata_preview = false;
var is_advanced = (metadata_uri.match(/metadata_advanced/i) != null);
var is_category_mandatory = {% if TOPICCATEGORY_MANDATORY %}true{% else %}false{% endif %};
var is_group_mandatory = {% if GROUP_MANDATORY_RESOURCES %}true{% else %}false{% endif %};

var updateWizard = function() {
Expand Down Expand Up @@ -417,8 +418,13 @@
$('#btn_next_dwn').click(function(){getNextTab()});

$('#btn_upd_md_up').click(function(){
if((!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())) {
if
( is_category_mandatory &&
(
(!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())
)
) {
$('#category_mandatoryDialog').modal();
} else if
( is_group_mandatory &&
Expand All @@ -434,8 +440,13 @@
});

$('#btn_upd_md_dwn').click(function(){
if((!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())) {
if
( is_category_mandatory &&
(
(!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())
)
) {
$('#category_mandatoryDialog').modal();
} else if
( is_group_mandatory &&
Expand All @@ -451,8 +462,13 @@
});

$('#btn_upd_md_done').click(function(){
if((!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())) {
if
( is_category_mandatory &&
(
(!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())
)
) {
$('#category_mandatoryDialog').modal();
} else if
( is_group_mandatory &&
Expand All @@ -471,8 +487,13 @@
});

$('#preview_tab').on('show.bs.tab', function(){
if((!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())) {
if
( is_category_mandatory &&
(
(!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())
)
) {
$('#category_mandatoryDialog').modal();
} else if
( is_group_mandatory &&
Expand Down Expand Up @@ -514,8 +535,13 @@
});

$('#metadata_update').submit(function(e) {
if((!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())) {
if
( is_category_mandatory &&
(
(!is_advanced && $('#category_form').hasClass("input-empty")) ||
(is_advanced && !$('#category_form input:checked').val())
)
) {
$('#category_mandatoryDialog').modal();
} else if
( is_group_mandatory &&
Expand Down

0 comments on commit 96f9f00

Please sign in to comment.