#########################################################################
#
# Copyright (C) 2016 OSGeo
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from django import forms
from django.contrib import admin
from django.conf import settings
from django.shortcuts import redirect, render
from django.urls import path
from dal import autocomplete
from taggit.forms import TagField
from django.core.management import call_command
from slugify import slugify
from django.contrib import messages
from treebeard.admin import TreeAdmin
from treebeard.forms import movenodeform_factory
from modeltranslation.admin import TabbedTranslationAdmin
from geonode.base.models import (
TopicCategory,
SpatialRepresentationType,
Region,
RestrictionCodeType,
ContactRole,
Link,
License,
HierarchicalKeyword,
MenuPlaceholder,
Menu,
MenuItem,
Configuration,
Thesaurus,
ThesaurusLabel,
ThesaurusKeyword,
ThesaurusKeywordLabel,
)
from geonode.base.forms import BatchEditForm, ThesaurusImportForm, UserAndGroupPermissionsForm
from geonode.base.widgets import TaggitSelect2Custom
metadata_batch_edit.short_description = "Metadata batch edit"
[docs]
def set_user_and_group_dataset_permission(modeladmin, request, queryset):
ids = ",".join(str(element.pk) for element in queryset)
resource = queryset[0].__class__.__name__.lower()
model_mapper = {"profile": "people", "groupprofile": "groups"}
form = UserAndGroupPermissionsForm(
{
"permission_type": "view",
"mode": "set",
"ids": ids,
}
)
return render(
request, "base/user_and_group_permissions.html", context={"form": form, "model": model_mapper[resource]}
)
set_user_and_group_dataset_permission.short_description = "Set layer permissions"
[docs]
class LicenseAdmin(TabbedTranslationAdmin):
[docs]
list_display = ("id", "name")
[docs]
list_display_links = ("name",)
[docs]
class TopicCategoryAdmin(TabbedTranslationAdmin):
[docs]
list_display_links = ("identifier",)
[docs]
list_display = ("identifier", "description", "gn_description", "fa_class", "is_choice")
if settings.MODIFY_TOPICCATEGORY is False:
[docs]
exclude = (
"identifier",
"description",
)
[docs]
def has_add_permission(self, request):
# the records are from the standard TC 211 list, so no way to add
if settings.MODIFY_TOPICCATEGORY:
return True
else:
return False
[docs]
def has_delete_permission(self, request, obj=None):
# the records are from the standard TC 211 list, so no way to remove
if settings.MODIFY_TOPICCATEGORY:
return True
else:
return False
[docs]
class RegionAdmin(TabbedTranslationAdmin):
[docs]
list_display_links = ("name",)
[docs]
list_display = ("code", "name", "parent")
[docs]
search_fields = (
"code",
"name",
)
[docs]
class SpatialRepresentationTypeAdmin(TabbedTranslationAdmin):
[docs]
model = SpatialRepresentationType
[docs]
list_display_links = ("identifier",)
[docs]
list_display = ("identifier", "description", "gn_description", "is_choice")
[docs]
def has_add_permission(self, request):
# the records are from the standard TC 211 list, so no way to add
return False
[docs]
def has_delete_permission(self, request, obj=None):
# the records are from the standard TC 211 list, so no way to remove
return False
[docs]
class RestrictionCodeTypeAdmin(TabbedTranslationAdmin):
[docs]
model = RestrictionCodeType
[docs]
list_display_links = ("identifier",)
[docs]
list_display = ("identifier", "description", "gn_description", "is_choice")
[docs]
def has_add_permission(self, request):
# the records are from the standard TC 211 list, so no way to add
return False
[docs]
def has_delete_permission(self, request, obj=None):
# the records are from the standard TC 211 list, so no way to remove
return False
[docs]
class LinkAdmin(admin.ModelAdmin):
[docs]
list_display_links = ("id",)
[docs]
list_display = ("id", "resource", "extension", "link_type", "name", "mime")
[docs]
list_filter = ("resource", "extension", "link_type", "mime")
[docs]
search_fields = (
"name",
"resource__title",
)
[docs]
class HierarchicalKeywordAdmin(TreeAdmin):
[docs]
search_fields = ("name",)
[docs]
class ConfigurationAdmin(admin.ModelAdmin):
[docs]
def has_delete_permission(self, request, obj=None):
# Disable delete action of Singleton model, since "delete selected objects" uses QuerysSet.delete()
# instead of Model.delete()
return False
[docs]
class ThesaurusAdmin(admin.ModelAdmin):
[docs]
change_list_template = "admin/thesauri/change_list.html"
[docs]
list_display = ("id", "identifier")
[docs]
list_display_links = ("id", "identifier")
[docs]
ordering = ("identifier",)
[docs]
def get_urls(self):
urls = super().get_urls()
my_urls = [path("importrdf/", self.import_rdf, name="base_thesaurus_importrdf")]
return my_urls + urls
[docs]
def import_rdf(self, request):
if request.method == "POST":
try:
rdf_file = request.FILES["rdf_file"]
name = slugify(rdf_file.name)
call_command("load_thesaurus", file=rdf_file, name=name)
self.message_user(request, "Your RDF file has been imported", messages.SUCCESS)
return redirect("..")
except Exception as e:
self.message_user(request, e.args[0], messages.ERROR)
return redirect("..")
form = ThesaurusImportForm()
payload = {"form": form}
return render(request, "admin/thesauri/upload_form.html", payload)
[docs]
class ThesaurusLabelAdmin(admin.ModelAdmin):
[docs]
list_display = ("thesaurus_id", "lang", "label")
[docs]
list_display_links = ("label",)
[docs]
ordering = ("thesaurus__identifier", "lang")
[docs]
def thesaurus_id(self, obj):
return obj.thesaurus.identifier
thesaurus_id.short_description = "Thesaurus"
thesaurus_id.admin_order_field = "thesaurus__identifier"
[docs]
class ThesaurusKeywordAdmin(admin.ModelAdmin):
[docs]
model = ThesaurusKeyword
[docs]
list_display = (
"thesaurus_id",
"about",
"alt_label",
)
[docs]
list_display_links = (
"about",
"alt_label",
)
[docs]
ordering = (
"thesaurus__identifier",
"alt_label",
)
[docs]
list_filter = ("thesaurus_id",)
[docs]
def thesaurus_id(self, obj):
return obj.thesaurus.identifier
thesaurus_id.short_description = "Thesaurus"
thesaurus_id.admin_order_field = "thesaurus__identifier"
[docs]
class ThesaurusKeywordLabelAdmin(admin.ModelAdmin):
[docs]
model = ThesaurusKeywordLabel
[docs]
list_display = ("thesaurus_id", "keyword_id", "lang", "label")
[docs]
list_display_links = ("lang", "label")
[docs]
ordering = ("keyword__thesaurus__identifier", "keyword__alt_label", "lang")
[docs]
list_filter = ("keyword__thesaurus__identifier", "keyword_id", "lang")
[docs]
def thesaurus_id(self, obj):
return obj.keyword.thesaurus.identifier
thesaurus_id.short_description = "Thesaurus"
thesaurus_id.admin_order_field = "keyword__thesaurus__identifier"
[docs]
def keyword_id(self, obj):
return obj.keyword.alt_label
keyword_id.short_description = "Keyword"
keyword_id.admin_order_field = "keyword__alt_label"
admin.site.register(TopicCategory, TopicCategoryAdmin)
admin.site.register(Region, RegionAdmin)
admin.site.register(SpatialRepresentationType, SpatialRepresentationTypeAdmin)
admin.site.register(RestrictionCodeType, RestrictionCodeTypeAdmin)
admin.site.register(ContactRole, ContactRoleAdmin)
admin.site.register(Link, LinkAdmin)
admin.site.register(License, LicenseAdmin)
admin.site.register(HierarchicalKeyword, HierarchicalKeywordAdmin)
admin.site.register(MenuPlaceholder, MenuPlaceholderAdmin)
admin.site.register(Menu, MenuAdmin)
admin.site.register(MenuItem, MenuItemAdmin)
admin.site.register(Configuration, ConfigurationAdmin)
admin.site.register(Thesaurus, ThesaurusAdmin)
admin.site.register(ThesaurusLabel, ThesaurusLabelAdmin)
admin.site.register(ThesaurusKeyword, ThesaurusKeywordAdmin)
admin.site.register(ThesaurusKeywordLabel, ThesaurusKeywordLabelAdmin)