Skip to content

Commit

Permalink
Task: #39: bugfix PA facet, adding custom resource_count
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Mar 15, 2021
1 parent 232a2db commit bbc6c13
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
18 changes: 4 additions & 14 deletions rndt/templates/search/_pa_filter.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@
<nav class="filter" ng-cloak>
<h4><a href="#" class="toggle toggle-nav"><i class="fa fa-chevron-right"></i>{% trans "Public Administration" %}</a></h4>
<ul class="nav closed" id="pub_ammin">
{% if facet_type == 'layers' %}
{% verbatim %}
<li ng-repeat="pa in pubblica_amministrazione">
<a data-value="{{ pa.ipa }}" data-filter="uuid__startswith" title="{{ pa.name }}"
ng-click="multiple_choice_listener($event)" class="{{pa.active}}">{{ pa.name | limitTo: 25 }}{{ pa.name.length > 25 ? '...' : ''}}
</a>
<li ng-repeat="pa in pubblica_amministrazione" ng-if="pa.count > 0">
<a data-value="{{ pa.ipa }}" data-filter="uuid__startswith" ng-click="multiple_choice_listener($event)" class="{{pa.active}}">{{ pa.name | limitTo: 25 }}{{ pa.name.length > 25 ? '...' : ''}}
<span class="badge pull-right">{{ pa.count }}</span>
</a>
</li>
{% endverbatim %}
{% else %}
{% verbatim %}
<li ng-repeat="pa in pubblica_amministrazione">
<a data-value="{{ pa.ipa }}" data-filter="uuid__startswith" title="{{ pa.name }}"
ng-click="multiple_choice_listener($event)" class="{{pa.active}}">{{ pa.name | limitTo: 25 }}{{ pa.name.length > 25 ? '...' : ''}}
</a>
</li>
{% endverbatim %}
{% endif %}
</ul>
</nav>
35 changes: 32 additions & 3 deletions rndt/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import json
import time

from django.core.serializers.json import DjangoJSONEncoder
from geonode.api.api import TypeFilteredResource
from geonode.api.authorization import ApiLockdownAuthorization
from geonode.layers.models import Layer
from tastypie.constants import ALL
from tastypie.resources import ModelResource
from tastypie.serializers import Serializer

from rndt.models import PubblicaAmministrazione


class PubblicaAmministrazioneResource(ModelResource):
class PACountJSONSerializer(Serializer):

def get_resources_counts(self, options):
counts = dict()
for pa in PubblicaAmministrazione.objects.all():
layer_with_ipa = Layer.objects.filter(uuid__startswith=pa.ipa).count()
counts[pa.id] = layer_with_ipa
return counts

def to_json(self, data, options=None):
options = options or {}
data = self.to_simple(data, options)
counts = self.get_resources_counts(options)
if 'objects' in data:
for item in data['objects']:
item['count'] = counts.get(item['id'], 0)
# Add in the current time.
data['requested_time'] = time.time()

return json.dumps(data, cls=DjangoJSONEncoder, sort_keys=True)
class PubblicaAmministrazioneResource(TypeFilteredResource):
"""PA api"""

def serialize(self, request, data, format, options=None):
if options is None:
options = {}
options['count_type'] = 'pubblica_amministrazione'
options['count_type'] = 'id'

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

Expand All @@ -22,3 +49,5 @@ class Meta:
'ipa': ALL,
'name': ALL
}
serializer = PACountJSONSerializer()
authorization = ApiLockdownAuthorization()

0 comments on commit bbc6c13

Please sign in to comment.