Skip to content

Commit

Permalink
GeoNode#2670: attach thesauri keywords to Resource. Add tkeywords API.
Browse files Browse the repository at this point in the history
  • Loading branch information
etj committed Oct 20, 2016
1 parent 7864609 commit 6a9aeaa
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
50 changes: 49 additions & 1 deletion geonode/api/api.py
Expand Up @@ -27,19 +27,22 @@
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from django.db.models import Count
from django.utils.translation import get_language

from avatar.templatetags.avatar_tags import avatar_url
from guardian.shortcuts import get_objects_for_user

from geonode.base.models import ResourceBase
from geonode.base.models import TopicCategory
from geonode.base.models import Region
from geonode.base.models import HierarchicalKeyword
from geonode.base.models import ThesaurusKeywordLabel

from geonode.layers.models import Layer
from geonode.maps.models import Map
from geonode.documents.models import Document
from geonode.groups.models import GroupProfile

from geonode.base.models import HierarchicalKeyword
from django.core.serializers.json import DjangoJSONEncoder
from tastypie.serializers import Serializer
from tastypie import fields
Expand Down Expand Up @@ -140,6 +143,51 @@ class Meta:
serializer = CountJSONSerializer()


class ThesaurusKeywordResource(TypeFilteredResource):
"""ThesaurusKeyword api"""

thesaurus_identifier = fields.CharField(null=False)
label_id = fields.CharField(null=False)

def build_filters(self, filters={}):
"""adds filtering by current language"""

orm_filters = super(ThesaurusKeywordResource, self).build_filters(filters)

orm_filters['lang'] = filters['lang'] if 'lang' in filters else get_language()

if 'thesaurus' in filters:
orm_filters['keyword__thesaurus__identifier'] = filters['thesaurus']

return orm_filters

def serialize(self, request, data, format, options={}):
options['count_type'] = 'tkeywords__id'

return super(ThesaurusKeywordResource, self).serialize(request, data, format, options)

def dehydrate_id(self, bundle):
return bundle.obj.keyword.id

def dehydrate_label_id(self, bundle):
return bundle.obj.id

def dehydrate_thesaurus_identifier(self, bundle):
return bundle.obj.keyword.thesaurus.identifier

class Meta:
queryset = ThesaurusKeywordLabel.objects.all().order_by('label')

resource_name = 'thesaurus/keywords'
allowed_methods = ['get']
filtering = {
'label': ALL,
'lang': ALL,
'thesaurus': ALL,
}
serializer = CountJSONSerializer()


class RegionResource(TypeFilteredResource):
"""Regions api"""

Expand Down
4 changes: 3 additions & 1 deletion geonode/api/urls.py
Expand Up @@ -21,7 +21,7 @@
from tastypie.api import Api

from .api import TagResource, TopicCategoryResource, ProfileResource, \
GroupResource, RegionResource, OwnersResource
GroupResource, RegionResource, OwnersResource, ThesaurusKeywordResource
from .resourcebase_api import LayerResource, MapResource, DocumentResource, \
ResourceBaseResource, FeaturedResourceBaseResource

Expand All @@ -38,3 +38,5 @@
api.register(GroupResource())
api.register(FeaturedResourceBaseResource())
api.register(OwnersResource())
api.register(ThesaurusKeywordResource())

19 changes: 19 additions & 0 deletions geonode/base/migrations/0009_resourcebase_tkeywords.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('base', '0008_add_thesaurus_keywords'),
]

operations = [
migrations.AddField(
model_name='resourcebase',
name='tkeywords',
field=models.ManyToManyField(help_text='formalised word(s) or phrase(s) from a fixed thesaurus used to describe the subject (space or comma-separated', to='base.ThesaurusKeyword'),
),
]
3 changes: 3 additions & 0 deletions geonode/base/models.py
Expand Up @@ -404,6 +404,8 @@ class ResourceBase(PolymorphicModel, PermissionLevelMixin, ItemBase):
'it is first produced')
keywords_help_text = _('commonly used word(s) or formalised word(s) or phrase(s) used to describe the subject '
'(space or comma-separated')
tkeywords_help_text = _('formalised word(s) or phrase(s) from a fixed thesaurus used to describe the subject '
'(space or comma-separated')
regions_help_text = _('keyword identifies a location')
restriction_code_type_help_text = _('limitation(s) placed upon the access or use of the data.')
constraints_other_help_text = _('other restrictions and legal prerequisites for accessing and using the resource or'
Expand Down Expand Up @@ -434,6 +436,7 @@ class ResourceBase(PolymorphicModel, PermissionLevelMixin, ItemBase):

keywords = TaggableManager(_('keywords'), through=TaggedContentItem, blank=True, help_text=keywords_help_text,
manager=_HierarchicalTagManager)
tkeywords = models.ManyToManyField(ThesaurusKeyword, help_text=tkeywords_help_text)
regions = models.ManyToManyField(Region, verbose_name=_('keywords region'), blank=True,
help_text=regions_help_text)

Expand Down

0 comments on commit 6a9aeaa

Please sign in to comment.