[ckan-changes] [okfn/ckan] 0b932d: [1821] Refactored facet_div() using new snippet, f...

GitHub noreply at github.com
Wed Apr 18 17:25:51 UTC 2012


  Branch: refs/heads/feature-1821-multilingual-extension
  Home:   https://github.com/okfn/ckan
  Commit: 0b932dfcce971cc9cc9f9eeeda1cd4ffe371ddad
      https://github.com/okfn/ckan/commit/0b932dfcce971cc9cc9f9eeeda1cd4ffe371ddad
  Author: Ian Murray <ian.murray at okfn.org>
  Date:   2012-04-18 (Wed, 18 Apr 2012)

  Changed paths:
    M ckan/templates/facets.html

  Log Message:
  -----------
  [1821] Refactored facet_div() using new snippet, facet_li()


diff --git a/ckan/templates/facets.html b/ckan/templates/facets.html
index 05c19b5..ed57bc2 100644
--- a/ckan/templates/facets.html
+++ b/ckan/templates/facets.html
@@ -34,19 +34,49 @@
     <div py:if="if_empty is not None or h.new_facet_items(name, limit)" class="facet-box">
         <h2>${h.facet_title(title)}</h2>
         <ul class="facet-options">
-            <li py:for="facet_item in h.new_facet_items(name, limit)"
-                py:if="not (name, facet_item.name) in c.fields">
-                <a href="${c.drill_down_url(**{name: facet_item.name})}">
-                    ${label_function(facet_item)}
-                </a>
-                ${count_label(facet_item['count'])}
-            </li>
+            ${facet_li(name, limit=limit, label_function=label_function, if_empty=None, count_label=count_label)}
         </ul>
         <p py:if="not h.new_facet_items(name, limit)">${if_empty}</p>
     </div>
 </py:def>
 
 <!--
+Generate <li>s for facet items.  The generated tags are not wrapped by any
+other tag, ie - it's up to the caller to wrap them in something suitable.
+
+name
+  The field name identifying the facet field, eg. "tags"
+
+label_function
+  Renders the human-readable label for each facet value.
+  If defined, this should be a callable that accepts a `facet_item`.
+  eg. lambda facet_item: facet_item.display_name.upper()
+  By default it displays the facet item's display name, which should
+  usually be good enough
+
+if_empty
+  A string, which if defined, and the list of possible facet items is empty,
+  is displayed in a <li> tag in lieu of an empty list.
+
+count_label
+  A callable which accepts an integer, and returns a string.  This controls
+  how a facet-item's count is displayed.
+  
+-->
+<py:def function="facet_li(name, limit=5, label_function=lambda item: item.display_name, if_empty=None, count_label=lambda c: ' (%d)'%c)">
+
+    <li py:if="if_empty and not h.new_facet_items(name, limit)">${if_empty}</li>
+
+    <li py:for="facet_item in h.new_facet_items(name, limit)"
+        py:if="not (name, facet_item.name) in c.fields">
+        <a href="${c.drill_down_url(**{name: facet_item.name})}">
+            ${label_function(facet_item)}
+        </a>
+        ${count_label(facet_item['count'])}
+    </li>
+</py:def>
+
+<!--
 DEPRECATED.  Provided only for backward compatibility with existing plugins.
              Use `facet_div` instead.
 


================================================================
  Commit: a9d67eb88314ca0ded30f11416dfd7be31487e1c
      https://github.com/okfn/ckan/commit/a9d67eb88314ca0ded30f11416dfd7be31487e1c
  Author: Ian Murray <ian.murray at okfn.org>
  Date:   2012-04-18 (Wed, 18 Apr 2012)

  Changed paths:
    M ckan/templates/facets.html

  Log Message:
  -----------
  [1821] Deprecated facet_list_items


diff --git a/ckan/templates/facets.html b/ckan/templates/facets.html
index ed57bc2..e7badc5 100644
--- a/ckan/templates/facets.html
+++ b/ckan/templates/facets.html
@@ -131,6 +131,9 @@
 </py:def>
 
 <!--
+DEPRECATED.  Provided only for backward compatibility with existing plugins.
+             Use `facet_li` instead.
+
 Construct a possibly empty list of <li> elements containing links to further
 search results
 
@@ -151,11 +154,15 @@
 -->
 <py:def function="facet_list_items(code, limit=5, label=lambda n: n, if_empty=None)">
         
-        <li py:if="if_empty and not h.new_facet_items(code, limit)">${if_empty}</li>
-        <li py:for="facet_item in h.new_facet_items(code, limit)"
-            py:if="not (code, facet_item.name) in c.fields">
-            <a href="${c.drill_down_url(**{code: facet_item.name})}">${label(facet_item.name)}</a> (facet_item['count'])
-        </li>
+    <?python
+      import logging
+      logging.getLogger('ckan.templates.facets').warning("Deprecated function:ckan/templates/facets.html:facet_list_items()")
+    ?>
+    <li py:if="if_empty and not h.new_facet_items(code, limit)">${if_empty}</li>
+    <li py:for="facet_item in h.new_facet_items(code, limit)"
+        py:if="not (code, facet_item.name) in c.fields">
+        <a href="${c.drill_down_url(**{code: facet_item.name})}">${label(facet_item.name)}</a> (facet_item['count'])
+    </li>
 </py:def>
 
 <py:def function="field_list()">


================================================================
  Commit: 4180a6a61d201e4cdb7691dc1b0bb6a24e1ecf45
      https://github.com/okfn/ckan/commit/4180a6a61d201e4cdb7691dc1b0bb6a24e1ecf45
  Author: Ian Murray <ian.murray at okfn.org>
  Date:   2012-04-18 (Wed, 18 Apr 2012)

  Changed paths:
    M ckan/templates/facets.html

  Log Message:
  -----------
  [1821] Removed superfluous check in facet_li()


diff --git a/ckan/templates/facets.html b/ckan/templates/facets.html
index e7badc5..ca969e8 100644
--- a/ckan/templates/facets.html
+++ b/ckan/templates/facets.html
@@ -67,8 +67,7 @@
 
     <li py:if="if_empty and not h.new_facet_items(name, limit)">${if_empty}</li>
 
-    <li py:for="facet_item in h.new_facet_items(name, limit)"
-        py:if="not (name, facet_item.name) in c.fields">
+    <li py:for="facet_item in h.new_facet_items(name, limit)">
         <a href="${c.drill_down_url(**{name: facet_item.name})}">
             ${label_function(facet_item)}
         </a>


================================================================
  Commit: 38a5af5b87a62714da94b0326bb7dcf57f1b48e4
      https://github.com/okfn/ckan/commit/38a5af5b87a62714da94b0326bb7dcf57f1b48e4
  Author: Ian Murray <ian.murray at okfn.org>
  Date:   2012-04-18 (Wed, 18 Apr 2012)

  Changed paths:
    M ckan/controllers/group.py
    M ckan/controllers/package.py
    M ckan/lib/helpers.py
    M ckan/lib/helpers_clean.py
    M ckan/logic/action/get.py
    M ckan/templates/facets.html
    M ckan/templates/group/read.html
    M ckan/tests/lib/test_dictization_schema.py
    M ckanext/multilingual/plugin.py
    M ckanext/multilingual/tests/test_multilingual_plugin.py

  Log Message:
  -----------
  Merge remote-tracking branch 'origin/feature-1821-multilingual-extension' into feature-1821-multilingual-extension

Conflicts:
	ckan/templates/facets.html


diff --git a/ckan/controllers/group.py b/ckan/controllers/group.py
index 87c89cf..24b1d14 100644
--- a/ckan/controllers/group.py
+++ b/ckan/controllers/group.py
@@ -178,7 +178,7 @@ def pager_url(q=None, page=None):
                 items_per_page=limit
             )
             c.facets = query['facets']
-            c.new_facets = query['new_facets']
+            c.search_facets = query['search_facets']
             c.page.items = query['results']
         except SearchError, se:
             log.error('Group search error: %r', se.args)
diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py
index 63adb13..c3826d9 100644
--- a/ckan/controllers/package.py
+++ b/ckan/controllers/package.py
@@ -185,7 +185,7 @@ def pager_url(q=None, page=None):
                 items_per_page=limit
             )
             c.facets = query['facets']
-            c.new_facets = query['new_facets']
+            c.search_facets = query['search_facets']
             c.page.items = query['results']
         except SearchError, se:
             log.error('Dataset search error: %r', se.args)
diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py
index fd26fe7..8d79600 100644
--- a/ckan/lib/helpers.py
+++ b/ckan/lib/helpers.py
@@ -337,22 +337,39 @@ def _subnav_named_route(text, routename, **kwargs):
 def default_group_type():
     return str( config.get('ckan.default.group_type', 'group') )
 
-def new_facet_items(name, limit=10):
-    if not c.new_facets or \
-       not c.new_facets.get(name) or \
-       not c.new_facets.get(name).get('items'):
+def unselected_facet_items(facet, limit=10):
+    '''Return the list of unselected facet items for the given facet, sorted
+    by count.
+
+    Returns the list of unselected facet contraints or facet items (e.g. tag
+    names like "russian" or "tolstoy") for the given search facet (e.g.
+    "tags"), sorted by facet item count (i.e. the number of search results that
+    match each facet item).
+
+    Reads the complete list of facet items for the given facet from
+    c.search_facets, and filters out the facet items that the user has already
+    selected.
+
+    Arguments:
+    facet -- the name of the facet to filter.
+    limit -- the max. number of facet items to return.
+
+    '''
+    if not c.search_facets or \
+       not c.search_facets.get(facet) or \
+       not c.search_facets.get(facet).get('items'):
         return []
     facets = []
-    for facet_item in c.new_facets.get(name)['items']:
+    for facet_item in c.search_facets.get(facet)['items']:
         if not len(facet_item['name'].strip()):
             continue
-        if not (name, facet_item['name']) in request.params.items():
+        if not (facet, facet_item['name']) in request.params.items():
             facets.append(facet_item)
     return sorted(facets, key=lambda item: item['count'], reverse=True)[:limit]
 
 def facet_items(*args, **kwargs):
     """
-    DEPRECATED: Use the new facet data structure, and `new_facet_items()`
+    DEPRECATED: Use the new facet data structure, and `unselected_facet_items()`
     """
     _log.warning('Deprecated function: ckan.lib.helpers:facet_items().  Will be removed in v1.8')
     # facet_items() used to need c passing as the first arg
diff --git a/ckan/lib/helpers_clean.py b/ckan/lib/helpers_clean.py
index cc42060..8cef519 100644
--- a/ckan/lib/helpers_clean.py
+++ b/ckan/lib/helpers_clean.py
@@ -28,7 +28,7 @@
            subnav_link,
            subnav_named_route,
            default_group_type,
-           new_facet_items,
+           unselected_facet_items,
            facet_items,
            facet_title,
          #  am_authorized, # depreciated
diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py
index 3760a2a..93e4120 100644
--- a/ckan/logic/action/get.py
+++ b/ckan/logic/action/get.py
@@ -758,7 +758,7 @@ def package_search(context, data_dict):
                 new_facet_dict['display_name'] = key_
             new_facet_dict['count'] = value_
             restructured_facets[key]['items'].append(new_facet_dict)
-    search_results['new_facets'] = restructured_facets
+    search_results['search_facets'] = restructured_facets
 
     # check if some extension needs to modify the search results
     for item in plugins.PluginImplementations(plugins.IPackageController):
@@ -766,9 +766,9 @@ def package_search(context, data_dict):
 
     # After extensions have had a chance to modify the facets, sort them by
     # display name.
-    for facet in search_results['new_facets']:
-        search_results['new_facets'][facet]['items'] = sorted(
-                search_results['new_facets'][facet]['items'],
+    for facet in search_results['search_facets']:
+        search_results['search_facets'][facet]['items'] = sorted(
+                search_results['search_facets'][facet]['items'],
                 key=lambda facet: facet['display_name'], reverse=True)
 
     return search_results
diff --git a/ckan/templates/facets.html b/ckan/templates/facets.html
index ca969e8..f8c104b 100644
--- a/ckan/templates/facets.html
+++ b/ckan/templates/facets.html
@@ -30,13 +30,13 @@
   how a facet-item's count is displayed.
   
 -->
-<py:def function="facet_div(name, title, limit=5, label_function=lambda item: item.display_name, if_empty=None, count_label=lambda c: ' (%d)'%c)">
-    <div py:if="if_empty is not None or h.new_facet_items(name, limit)" class="facet-box">
+<py:def function="facet_div(name, title, limit=10, label_function=lambda item: item.display_name, if_empty=None, count_label=lambda c: ' (%d)'%c)">
+    <div py:if="if_empty is not None or h.unselected_facet_items(name, limit)" class="facet-box">
         <h2>${h.facet_title(title)}</h2>
         <ul class="facet-options">
             ${facet_li(name, limit=limit, label_function=label_function, if_empty=None, count_label=count_label)}
         </ul>
-        <p py:if="not h.new_facet_items(name, limit)">${if_empty}</p>
+        <p py:if="not h.unselected_facet_items(name, limit)">${if_empty}</p>
     </div>
 </py:def>
 
@@ -65,9 +65,9 @@
 -->
 <py:def function="facet_li(name, limit=5, label_function=lambda item: item.display_name, if_empty=None, count_label=lambda c: ' (%d)'%c)">
 
-    <li py:if="if_empty and not h.new_facet_items(name, limit)">${if_empty}</li>
+    <li py:if="if_empty and not h.unselected_facet_items(name, limit)">${if_empty}</li>
 
-    <li py:for="facet_item in h.new_facet_items(name, limit)">
+    <li py:for="facet_item in h.unselected_facet_items(name, limit)">
         <a href="${c.drill_down_url(**{name: facet_item.name})}">
             ${label_function(facet_item)}
         </a>
@@ -115,17 +115,17 @@
       import logging
       logging.getLogger('ckan.templates.facets').warning("Deprecated function: ckan/templates/facets.html:facet_sidebar()")
     ?>
-    <div py:if="if_empty is not None or h.new_facet_items(code, limit)" class="facet-box">
+    <div py:if="if_empty is not None or h.unselected_facet_items(code, limit)" class="facet-box">
         <h2>${title(code)}</h2>
         <ul class="facet-options">
-            <li py:for="facet_item in h.new_facet_items(code, limit)"
+            <li py:for="facet_item in h.unselected_facet_items(code, limit)"
                 py:if="not (code, facet_item.name) in c.fields">
                   <a href="${c.drill_down_url(**{code: facet_item.name})}">
                   <span py:if="'format' in code.lower()">${h.icon(h.format_icon(facet_item.name))}</span>
                     ${label(facet_item.name)}</a>${count_label(facet_item['count'])}
             </li>
         </ul>
-        <p py:if="not h.new_facet_items(code, limit)">${if_empty}</p>
+        <p py:if="not h.unselected_facet_items(code, limit)">${if_empty}</p>
     </div>
 </py:def>
 
@@ -157,9 +157,8 @@
       import logging
       logging.getLogger('ckan.templates.facets').warning("Deprecated function:ckan/templates/facets.html:facet_list_items()")
     ?>
-    <li py:if="if_empty and not h.new_facet_items(code, limit)">${if_empty}</li>
-    <li py:for="facet_item in h.new_facet_items(code, limit)"
-        py:if="not (code, facet_item.name) in c.fields">
+    <li py:if="if_empty and not h.unselected_facet_items(code, limit)">${if_empty}</li>
+    <li py:for="facet_item in h.unselected_facet_items(code, limit)">
         <a href="${c.drill_down_url(**{code: facet_item.name})}">${label(facet_item.name)}</a> (facet_item['count'])
     </li>
 </py:def>
diff --git a/ckan/templates/group/read.html b/ckan/templates/group/read.html
index f08cf99..c2c9270 100644
--- a/ckan/templates/group/read.html
+++ b/ckan/templates/group/read.html
@@ -5,8 +5,8 @@
   
   <xi:include href="../facets.html" />
 
-  <py:def function="page_title">${c.group.display_name}</py:def>
-  <py:def function="page_heading">${c.group.display_name}</py:def>
+  <py:def function="page_title">${c.group_dict.display_name}</py:def>
+  <py:def function="page_heading">${c.group_dict.display_name}</py:def>
   <py:if test="c.group.image_url">
     <py:def function="page_logo">${c.group.image_url}</py:def>
   </py:if>
diff --git a/ckan/tests/lib/test_dictization_schema.py b/ckan/tests/lib/test_dictization_schema.py
index ca7c018..a004b1e 100644
--- a/ckan/tests/lib/test_dictization_schema.py
+++ b/ckan/tests/lib/test_dictization_schema.py
@@ -94,12 +94,9 @@ def test_1_package_schema(self):
                                                 'hash': u'def456',
                                                 'size_extra': u'345',
                                                 'url': u'http://www.annakarenina.com/index.json'}],
-                                 'tags': [{'name': u'Flexible \u30a1',
-                                           'display_name': u'Flexible \u30a1'},
-                                          {'name': u'russian',
-                                           'display_name': u'russian'},
-                                          {'name': u'tolstoy',
-                                           'display_name': u'tolstoy'}],
+                                 'tags': [{'name': u'Flexible \u30a1'},
+                                          {'name': u'russian'},
+                                          {'name': u'tolstoy'}],
                                  'title': u'A Novel By Tolstoy',
                                  'url': u'http://www.annakarenina.com',
                                  'version': u'0.7a'}, pformat(converted_data)
diff --git a/ckanext/multilingual/plugin.py b/ckanext/multilingual/plugin.py
index e8521bd..a4696bd 100644
--- a/ckanext/multilingual/plugin.py
+++ b/ckanext/multilingual/plugin.py
@@ -173,7 +173,7 @@ def before_search(self, search_params):
     def after_search(self, search_results, search_params):
 
         # Translate the unselected search facets.
-        facets = search_results.get('new_facets')
+        facets = search_results.get('search_facets')
         if not facets:
             return search_results
 
@@ -255,7 +255,8 @@ class MultilingualGroup(SingletonPlugin):
     implements(IGroupController, inherit=True)
 
     def before_view(self, data_dict):
-        return translate_data_dict(data_dict)
+        translated_data_dict = translate_data_dict(data_dict)
+        return translated_data_dict
 
 class MultilingualTag(SingletonPlugin):
     '''The MultilingualTag plugin translates tag names on tag read pages and
diff --git a/ckanext/multilingual/tests/test_multilingual_plugin.py b/ckanext/multilingual/tests/test_multilingual_plugin.py
index fb16ebe..9a09cc3 100644
--- a/ckanext/multilingual/tests/test_multilingual_plugin.py
+++ b/ckanext/multilingual/tests/test_multilingual_plugin.py
@@ -45,7 +45,7 @@ def teardown(cls):
         ckan.model.repo.rebuild_db()
         ckan.lib.search.clear()
 
-    def test_dataset_view_translation(self):
+    def test_dataset_read_translation(self):
         '''Test the translation of dataset view pages by the
         multilingual_dataset plugin.
 
@@ -91,7 +91,7 @@ def test_dataset_view_translation(self):
             nose.tools.assert_raises(IndexError, response.mustcontain,
                     'this should not be rendered')
 
-    def test_tag_view_translation(self):
+    def test_tag_read_translation(self):
         '''Test the translation of tag view pages by the multilingual_tag
         plugin.
 
@@ -121,7 +121,7 @@ def test_tag_view_translation(self):
                 nose.tools.assert_raises(IndexError, response.mustcontain,
                         'this should not be rendered')
 
-    def test_user_view_translation(self):
+    def test_user_read_translation(self):
         '''Test the translation of datasets on user view pages by the
         multilingual_dataset plugin.
 
@@ -151,31 +151,7 @@ def test_user_view_translation(self):
                 nose.tools.assert_raises(IndexError, response.mustcontain,
                         'this should not be rendered')
 
-    def test_dataset_search_results_translation(self):
-        for (lang_code, translations) in (
-                ('de', ckan.lib.create_test_data.german_translations),
-                ('fr', ckan.lib.create_test_data.french_translations),
-                ('en', ckan.lib.create_test_data.english_translations),
-                ('pl', {})):
-            offset = '/%s/dataset' % lang_code
-            response = self.app.get(offset, status=200)
-            for term in ('Index of the novel', 'russian', 'tolstoy',
-                    "Dave's books", "Roger's books", 'plain text'):
-                if term in translations:
-                    response.mustcontain(translations[term])
-                elif term in ckan.lib.create_test_data.english_translations:
-                    response.mustcontain(
-                        ckan.lib.create_test_data.english_translations[term])
-                else:
-                    response.mustcontain(term)
-            for tag_name in ('123', '456', '789', 'russian', 'tolstoy'):
-                response.mustcontain('/%s/dataset?tags=%s' % (lang_code, tag_name))
-            for group_name in ('david', 'roger'):
-                response.mustcontain('/%s/dataset?groups=%s' % (lang_code, group_name))
-            nose.tools.assert_raises(IndexError, response.mustcontain,
-                    'this should not be rendered')
-
-    def test_group_search_results_translation(self):
+    def test_group_read_translation(self):
         for (lang_code, translations) in (
                 ('de', ckan.lib.create_test_data.german_translations),
                 ('fr', ckan.lib.create_test_data.french_translations),
@@ -211,7 +187,31 @@ def test_group_search_results_translation(self):
             nose.tools.assert_raises(IndexError, response.mustcontain,
                     'this should not be rendered')
 
-    def test_group_list_translation(self):
+    def test_dataset_index_translation(self):
+        for (lang_code, translations) in (
+                ('de', ckan.lib.create_test_data.german_translations),
+                ('fr', ckan.lib.create_test_data.french_translations),
+                ('en', ckan.lib.create_test_data.english_translations),
+                ('pl', {})):
+            offset = '/%s/dataset' % lang_code
+            response = self.app.get(offset, status=200)
+            for term in ('Index of the novel', 'russian', 'tolstoy',
+                    "Dave's books", "Roger's books", 'plain text'):
+                if term in translations:
+                    response.mustcontain(translations[term])
+                elif term in ckan.lib.create_test_data.english_translations:
+                    response.mustcontain(
+                        ckan.lib.create_test_data.english_translations[term])
+                else:
+                    response.mustcontain(term)
+            for tag_name in ('123', '456', '789', 'russian', 'tolstoy'):
+                response.mustcontain('/%s/dataset?tags=%s' % (lang_code, tag_name))
+            for group_name in ('david', 'roger'):
+                response.mustcontain('/%s/dataset?groups=%s' % (lang_code, group_name))
+            nose.tools.assert_raises(IndexError, response.mustcontain,
+                    'this should not be rendered')
+
+    def test_group_index_translation(self):
         for (lang_code, translations) in (
                 ('de', ckan.lib.create_test_data.german_translations),
                 ('fr', ckan.lib.create_test_data.french_translations),
@@ -233,10 +233,12 @@ def test_group_list_translation(self):
                         ckan.lib.create_test_data.english_translations[term])
                 else:
                     response.mustcontain(term)
+            for group_name in ('david', 'roger'):
+                response.mustcontain('/%s/group/%s' % (lang_code, group_name))
             nose.tools.assert_raises(IndexError, response.mustcontain,
                     'this should not be rendered')
 
-    def test_tag_list_translation(self):
+    def test_tag_index_translation(self):
         for (lang_code, translations) in (
                 ('de', ckan.lib.create_test_data.german_translations),
                 ('fr', ckan.lib.create_test_data.french_translations),
@@ -259,6 +261,7 @@ def test_tag_list_translation(self):
                         ckan.lib.create_test_data.english_translations[term])
                 else:
                     response.mustcontain(term)
+                response.mustcontain('/%s/tag/%s' % (lang_code, term))
             nose.tools.assert_raises(IndexError, response.mustcontain,
                     'this should not be rendered')
 


================================================================
Compare: https://github.com/okfn/ckan/compare/13ae448...38a5af5


More information about the ckan-changes mailing list