Skip to content

Commit

Permalink
[2347] Implemented related dashboard with filter on type and tidied u…
Browse files Browse the repository at this point in the history
…p some of the related templates
  • Loading branch information
rossjones committed May 2, 2012
1 parent 96e486d commit 0b30897
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 15 deletions.
1 change: 1 addition & 0 deletions ckan/config/routing.py
Expand Up @@ -155,6 +155,7 @@ def make_map():
with SubMapper(map, controller='related') as m:
m.connect('related_list', '/dataset/{id}/related', action='list')
m.connect('related_read', '/dataset/{id}/related/{related_id}', action='read')
m.connect('related_dashboard', '/apps', action='dashboard')

with SubMapper(map, controller='package') as m:
m.connect('/dataset', action='search')
Expand Down
50 changes: 48 additions & 2 deletions ckan/controllers/related.py
Expand Up @@ -4,13 +4,59 @@
import ckan.logic as logic
import ckan.lib.base as base
import ckan.lib.helpers as h
import urllib

c = base.c

class RelatedController(base.BaseController):

def list(self, id):
def dashboard(self):
""" List all related items regardless of dataset """
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'extras_as_string': True,
'for_view': True}
data_dict = {
'type_filter': base.request.params.get('type', '')
}

params_nopage = [(k, v) for k,v in base.request.params.items()
if k != 'page']
try:
page = int(base.request.params.get('page', 1))
except ValueError, e:
abort(400, ('"page" parameter must be an integer'))

# Update ordering in the context
query = logic.get_action('related_list')(context,data_dict)

def search_url(params):
url = h.url_for(controller='related', action='dashboard')
params = [(k, v.encode('utf-8')
if isinstance(v, basestring) else str(v))
for k, v in params]
return url + u'?' + urllib.urlencode(params)

def pager_url(q=None, page=None):
params = list(params_nopage)
params.append(('page', page))
return search_url(params)


c.page = h.Page(
collection=query.all(),
page=page,
url=pager_url,
item_count=query.count(),
items_per_page=8
)

c.filters = dict(params_nopage)

return base.render( "related/dashboard.html")


def list(self, id):
""" List all related items for a specific dataset """
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'extras_as_string': True,
'for_view': True}
Expand All @@ -32,5 +78,5 @@ def list(self, id):

c.related_count = len(c.pkg.related)

return base.render( "package/related_list.html")
return base.render( "related/related_list.html")

23 changes: 15 additions & 8 deletions ckan/logic/action/get.py
Expand Up @@ -138,8 +138,8 @@ def related_show(context, data_dict=None):

def related_list(context, data_dict=None):
"""
List the related items for a specific package which should be
mentioned in the data_dict
List the related items which may be for a specific package which
should be mentioned in the data_dict
context:
model - The CKAN model module
Expand All @@ -150,6 +150,9 @@ def related_list(context, data_dict=None):
id - The ID of the dataset to which we want to list related items
or
dataset - The dataset (package) model
If neither value is in the data_dict then all related items will
be returned, and the ordering requested will be applied.
"""
model = context['model']
session = context['session']
Expand All @@ -158,14 +161,18 @@ def related_list(context, data_dict=None):
if not dataset:
dataset = model.Package.get(data_dict.get('id'))

if not dataset:
raise NotFound

check_access('related_show',context, data_dict)

relateds = model.Related.get_for_dataset(dataset, status='active')
related_items = (r.related for r in relateds)
related_list = model_dictize.related_list_dictize( related_items, context)
related_list = []
if not dataset:
related_list = model.Session.query(model.Related)
tfilter = data_dict.get('type_filter', None)
if tfilter:
related_list = related_list.filter(model.Related.type == tfilter)
else:
relateds = model.Related.get_for_dataset(dataset, status='active')
related_items = (r.related for r in relateds)
related_list = model_dictize.related_list_dictize( related_items, context)
return related_list


Expand Down
4 changes: 2 additions & 2 deletions ckan/templates/_util.html
Expand Up @@ -125,10 +125,10 @@
</py:def>


<py:def function="related_summary(related)">
<py:def function="related_summary(related, allow_delete)">
<li class="span3">
<div class="thumbnail">
<button py:if="c.user and (c.userobj.id == related.owner_id or h.check_access('package_update',{'id':c.pkg.id}))" class="close" onclick="related_delete('${related.id}');">×</button>
<button py:if="allow_delete and c.user and (c.userobj.id == related.owner_id or h.check_access('package_update',{'id':c.pkg.id}))" class="close" onclick="related_delete('${related.id}');">×</button>
<a href="${related.url}" class="image">
<img src="${related.image_url}" width="210" py:if="related.image_url" />
<img src="/images/photo-placeholder.png" width="210" py:if="not related.image_url" />
Expand Down
File renamed without changes.
56 changes: 56 additions & 0 deletions ckan/templates/related/dashboard.html
@@ -0,0 +1,56 @@
<html xmlns:py="http://genshi.edgewall.org/"
xmlns:i18n="http://genshi.edgewall.org/i18n"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:dc="http://purl.org/dc/terms/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dcat="http://www.w3.org/ns/dcat#"
typeof="dcat:Dataset"
about=""
py:strip="">

<py:def function="body_class">no-sidebar</py:def>
<xi:include href="add-related.html" />

<py:def function="page_title">Apps</py:def>

<py:def function="page_heading" property="dc:title">
Applications
</py:def>

<div py:match="content">
<h4 py:if="c.page.item_count">Showing items <strong>${c.page.first_item} - ${c.page.last_item}</strong> of <strong>${c.page.item_count}</strong> related items found</h4>
<h4 py:if="not c.page.item_count"><strong>${c.page.item_count}</strong> related items found</h4>

<div class="well">
<form action="" method="get" class="form-horizontal">
<input type='hidden' name='page' value='1'/>

<label for="type">Filter by type</label>
<select name="type">
<option value="">All</option>
<option value="app" py:attrs="{'selected': 'selected' if c.filters.get('type') == 'app' else None}">Application</option>
<option value="idea" py:attrs="{'selected': 'selected' if c.filters.get('type') == 'idea' else None}">Idea</option>
<option value="visualization" py:attrs="{'selected': 'selected' if c.filters.get('type') == 'visualization' else None}">Visualization</option>
</select>

<button class="btn btn-primary pull-right">Apply</button>
</form>
</div>

<ul class="related-items thumbnails">
<py:for each="related in c.page.items">
${related_summary(related,False)}
</py:for>
</ul>

${c.page.pager()}

<span class="insert-comment-thread"></span>
</div>

<xi:include href="../layout.html" />
</html>

Expand Up @@ -12,7 +12,7 @@
py:strip="">

<py:def function="body_class">no-sidebar</py:def>
<xi:include href="../_snippet/add-related.html" />
<xi:include href="add-related.html" />

<py:def function="page_title">${c.pkg_dict.get('title', c.pkg_dict['name'])}
- Related</py:def>
Expand All @@ -31,7 +31,7 @@ <h3>Related items <a class="btn btn-small btn-primary pull-right" data-toggle="m

<ul class="related-items thumbnails">
<py:for each="related in c.pkg.related">
${related_summary(related)}
${related_summary(related, True)}
</py:for>
</ul>
</div>
Expand Down Expand Up @@ -70,6 +70,6 @@ <h3>Related items <a class="btn btn-small btn-primary pull-right" data-toggle="m
href="${h.url(controller='package', action='history', id=c.pkg.name, format='atom', days=7)}" />
</py:def>

<xi:include href="layout.html" />
<xi:include href="../package/layout.html" />
</html>

0 comments on commit 0b30897

Please sign in to comment.