[ckan-changes] [okfn/ckan] db36db: [2347] Added support for sorting by view count, or...

GitHub noreply at github.com
Wed May 2 14:08:13 UTC 2012


  Branch: refs/heads/feature-2347-related-dashboard
  Home:   https://github.com/okfn/ckan
  Commit: db36db63dd994cd1ea84bf294e8b756c0bd41bab
      https://github.com/okfn/ckan/commit/db36db63dd994cd1ea84bf294e8b756c0bd41bab
  Author: Ross Jones <rossdjones at gmail.com>
  Date:   2012-05-02 (Wed, 02 May 2012)

  Changed paths:
    M ckan/config/routing.py
    M ckan/controllers/related.py
    M ckan/logic/action/get.py
    M ckan/model/related.py
    M ckan/templates/_util.html
    M ckan/templates/related/dashboard.html

  Log Message:
  -----------
  [2347] Added support for sorting by view count, or creation date


diff --git a/ckan/config/routing.py b/ckan/config/routing.py
index 1ad271e..dee0f51 100644
--- a/ckan/config/routing.py
+++ b/ckan/config/routing.py
@@ -154,7 +154,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_read', '/apps/{id}', action='read')
         m.connect('related_dashboard', '/apps', action='dashboard')
 
     with SubMapper(map, controller='package') as m:
diff --git a/ckan/controllers/related.py b/ckan/controllers/related.py
index fb6effd..3519ad2 100644
--- a/ckan/controllers/related.py
+++ b/ckan/controllers/related.py
@@ -16,7 +16,8 @@ def dashboard(self):
                    'user': c.user or c.author, 'extras_as_string': True,
                    'for_view': True}
         data_dict = {
-            'type_filter': base.request.params.get('type', '')
+            'type_filter': base.request.params.get('type', ''),
+            'sort': base.request.params.get('sort', '')
         }
 
         params_nopage = [(k, v) for k,v in base.request.params.items()
@@ -54,6 +55,28 @@ def pager_url(q=None, page=None):
 
         return base.render( "related/dashboard.html")
 
+    def read(self, id):
+        context = {'model': model, 'session': model.Session,
+                   'user': c.user or c.author, 'extras_as_string': True,
+                   'for_view': True}
+        data_dict = {'id': id}
+
+        try:
+            logic.check_access('related_show', context, data_dict)
+        except logic.NotAuthorized:
+            abort(401, _('Not authorized to see this page'))
+
+        related = model.Session.query(model.Related).\
+                    filter(model.Related.id == id).first()
+        if not related:
+            abort(404, _('The requested related item was not found'))
+
+        related.view_count += 1
+        model.Session.add(related)
+        model.Session.commit()
+
+        base.redirect(related.url)
+
 
     def list(self, id):
         """ List all related items for a specific dataset """
diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py
index b0e315e..2227e4d 100644
--- a/ckan/logic/action/get.py
+++ b/ckan/logic/action/get.py
@@ -166,9 +166,22 @@ def related_list(context, data_dict=None):
     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)
+
+        filter_on_type = data_dict.get('type_filter', None)
+        if filter_on_type:
+            related_list = related_list.filter(model.Related.type == filter_on_type)
+
+        sort = data_dict.get('sort', None)
+        if sort:
+            sortables = {
+                'view_count_asc' : model.Related.view_count.asc,
+                'view_count_desc': model.Related.view_count.desc,
+                'created_asc' : model.Related.created.asc,
+                'created_desc': model.Related.created.desc,
+            }
+            s = sortables.get(sort, None)
+            if s:
+                related_list = related_list.order_by( s() )
     else:
         relateds = model.Related.get_for_dataset(dataset, status='active')
         related_items = (r.related for r in relateds)
diff --git a/ckan/model/related.py b/ckan/model/related.py
index df2d589..c7f093e 100644
--- a/ckan/model/related.py
+++ b/ckan/model/related.py
@@ -16,6 +16,7 @@
         meta.Column('url', meta.UnicodeText),
         meta.Column('created', meta.DateTime, default=datetime.datetime.now),
         meta.Column('owner_id', meta.UnicodeText),
+        meta.Column('view_count', meta.Integer, default=0)
         )
 
 related_dataset_table = meta.Table('related_dataset', meta.metadata,
diff --git a/ckan/templates/_util.html b/ckan/templates/_util.html
index 73f976f..1d75af8 100644
--- a/ckan/templates/_util.html
+++ b/ckan/templates/_util.html
@@ -129,15 +129,16 @@
     <li class="span3">
       <div class="thumbnail">
         <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">
+        <a href="${h.url_for('related_read', id=related.id)}" 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" />
         </a>
+        <span style="display:none;">${related.view_count}</span>
         <div class="caption">
           <h5 class="heading" title="${related.title}">${h.markdown_extract(related.title, extract_length=30)}</h5>
           <div class="description" data-truncate="60" py:if="related.description">${h.markdown_extract(related.description, extract_length=1000)}</div>
           <i class="empty" py:if="not related.description">No description for this item</i>
-          <p class="read-more"><a href="${related.url}">View this related item</a></p>
+          <p class="read-more"><a href="${h.url_for('related_read', id=related.id)}">View this related item</a></p>
         </div>
       </div>
     </li>
diff --git a/ckan/templates/related/dashboard.html b/ckan/templates/related/dashboard.html
index fc519b7..029b741 100644
--- a/ckan/templates/related/dashboard.html
+++ b/ckan/templates/related/dashboard.html
@@ -25,7 +25,7 @@ <h4 py:if="c.page.item_count">Showing items <strong>${c.page.first_item} - ${c.p
     <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">
+        <form action="" method="get" class="form-inline">
             <input type='hidden' name='page' value='1'/>
 
             <label for="type">Filter by type</label>
@@ -36,6 +36,19 @@ <h4 py:if="not c.page.item_count"><strong>${c.page.item_count}</strong> related
                 <option value="visualization"  py:attrs="{'selected': 'selected' if c.filters.get('type') == 'visualization' else None}">Visualization</option>
             </select>
 
+            <label for="sort">Sort by</label>
+            <select name="sort">
+                <option value="">Default</option>
+                <option value="view_count_desc" py:attrs="{'selected': 'selected' if c.filters.get('sort') == 'view_count_desc' else None}">Most viewed</option>
+                <option value="view_count_asc" py:attrs="{'selected': 'selected' if c.filters.get('sort') == 'view_count_asc' else None}">Least viewed</option>
+
+                <option value="created_desc" py:attrs="{'selected': 'selected' if c.filters.get('sort') == 'created_desc' else None}">Newest</option>
+                <option value="created_asc" py:attrs="{'selected': 'selected' if c.filters.get('sort') == 'created_asc' else None}">Oldest</option>
+
+            </select>
+
+
+
             <button class="btn btn-primary pull-right">Apply</button>
         </form>
     </div>


================================================================



More information about the ckan-changes mailing list