[ckan-changes] [okfn/ckan] 95373e: [#2304] Don't show Follow button on dataset pages ...

GitHub noreply at github.com
Wed Apr 25 14:55:32 UTC 2012


  Branch: refs/heads/feature-2304-follow
  Home:   https://github.com/okfn/ckan
  Commit: 95373e587eb6f7f11676b4cb1542def8279c2552
      https://github.com/okfn/ckan/commit/95373e587eb6f7f11676b4cb1542def8279c2552
  Author: Sean Hammond <seanhammond at lavabit.com>
  Date:   2012-04-25 (Wed, 25 Apr 2012)

  Changed paths:
    M ckan/controllers/package.py
    M ckan/templates/package/layout.html

  Log Message:
  -----------
  [#2304] Don't show Follow button on dataset pages if not authorised to follow

e.g. if not logged in


diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py
index d43668f..3b8b249 100644
--- a/ckan/controllers/package.py
+++ b/ckan/controllers/package.py
@@ -100,6 +100,31 @@ def _guess_package_type(self, expecting_name=False):
 
         return pt
 
+    def _setup_follow_button(self, context):
+        '''Setup some template context variables used for the Follow button.'''
+
+        # If the user is logged in set the am_following variable.
+        userid = context.get('user')
+        if not userid:
+            return
+        userobj = model.User.get(userid)
+        if not userobj:
+            return
+        c.pkg_dict['am_following'] = get_action('am_following')(context,
+                {'id': c.pkg.id})
+
+        # If the user is authorized set the authorized_to_follow variable.
+        try:
+            data_dict = {
+                    'follower_id': userobj.id,
+                    'follower_type': 'user',
+                    'object_id': c.pkg.id,
+                    'object_type': 'dataset',
+                    }
+            check_access('follower_create', context, data_dict)
+            c.authorized_to_follow = True
+        except NotAuthorized:
+            pass
 
     authorizer = ckan.authz.Authorizer()
 
@@ -303,12 +328,9 @@ def read(self, id, format='html'):
                 get_action('package_activity_list_html')(context,
                     {'id': c.current_package_id})
 
-        # Add the package's number of followers to the context for templates.
         c.num_followers = get_action('follower_count')(context,
                 {'id':c.pkg.id})
-
-        c.am_following = get_action('am_following')(context,
-                {'id': c.pkg.id})
+        self._setup_follow_button(context)
 
         PackageSaver().render_package(c.pkg_dict, context)
 
@@ -373,9 +395,9 @@ def history(self, id):
         except NotFound:
             abort(404, _('Dataset not found'))
 
-        # Add the package's number of followers to the context for templates.
         c.num_followers = get_action('follower_count')(
                 context, {'id':c.pkg.id})
+        self._setup_follow_button(context)
 
         format = request.params.get('format', '')
         if format == 'atom':
@@ -495,9 +517,9 @@ def edit(self, id, data=None, errors=None, error_summary=None):
         else:
             c.form = render(self._package_form(package_type=package_type), extra_vars=vars)
 
-        # Add the package's number of followers to the context for templates.
         c.num_followers = get_action('follower_count')(context,
                 {'id':c.pkg.id})
+        self._setup_follow_button(context)
 
         if (c.action == u'editresources'):
           return render('package/editresources.html')
@@ -674,9 +696,9 @@ def authz(self, id):
         roles = self._handle_update_of_authz(pkg)
         self._prepare_authz_info_for_render(roles)
 
-        # Add the package's number of followers to the context for templates.
         c.num_followers = get_action('follower_count')(context,
                 {'id':c.pkg.id})
+        self._setup_follow_button(context)
 
         return render('package/authz.html')
 
@@ -774,6 +796,7 @@ def followers(self, id=None):
             c.followers = get_action('follower_list')(context,
                     {'id': c.pkg_dict['id']})
             c.num_followers = len(c.followers)
+            self._setup_follow_button(context)
         except NotFound:
             abort(404, _('Dataset not found'))
         except NotAuthorized:
diff --git a/ckan/templates/package/layout.html b/ckan/templates/package/layout.html
index e86342d..fb1f2b5 100644
--- a/ckan/templates/package/layout.html
+++ b/ckan/templates/package/layout.html
@@ -51,10 +51,12 @@
       <li class="${'active' if c.action=='authz' else ''}" py:if="h.check_access('package_edit_permissions',{'id':c.pkg.id})">
         ${h.subnav_link(h.icon('lock') + _('Authorization'), controller='package', action='authz', id=c.pkg.name)}
       </li>
-      <py:choose test="c.am_following">
-        <li py:when="True"><button package_id="${c.pkg.id}" class="btn dataset-unfollow">Unfollow</button></li>
-        <li py:otherwise=""><button package_id="${c.pkg.id}" class="btn dataset-follow">Follow</button></li>
-      </py:choose>
+      <li py:if="c.authorized_to_follow">
+        <py:choose test="c.pkg_dict.am_following">
+          <button py:when="True" package_id="${c.pkg.id}" class="btn dataset-unfollow">Unfollow</button>
+          <button py:otherwise="" package_id="${c.pkg.id}" class="btn dataset-follow">Follow</button>
+        </py:choose>
+      </li>
     </ul>
   </py:match>
   


================================================================
  Commit: b5fdb756ce5c49b029ce69b32277a3270fa73c89
      https://github.com/okfn/ckan/commit/b5fdb756ce5c49b029ce69b32277a3270fa73c89
  Author: Sean Hammond <seanhammond at lavabit.com>
  Date:   2012-04-25 (Wed, 25 Apr 2012)

  Changed paths:
    M ckan/public/scripts/application.js
    M ckan/templates/package/layout.html
    M ckan/templates/user/layout.html

  Log Message:
  -----------
  [#2304] Make Follow/Unfollow buttons toggle when clicked

e.g. if you click on a Follow button then (after the ajax request
returns successfully) javascript turns the button into an Unfollow
button without refreshing the page. (Previously the button wouldn't
change to Unfollow until you refreshed the page.)


diff --git a/ckan/public/scripts/application.js b/ckan/public/scripts/application.js
index c7bd375..f8e9faf 100644
--- a/ckan/public/scripts/application.js
+++ b/ckan/public/scripts/application.js
@@ -98,9 +98,7 @@ CKAN.Utils = CKAN.Utils || {};
     // This only needs to happen on dataset pages, but it doesn't seem to do
     // any harm to call it anyway.
     CKAN.Utils.setupDatasetFollowButton();
-    CKAN.Utils.setupDatasetUnfollowButton();
     CKAN.Utils.setupUserFollowButton();
-    CKAN.Utils.setupUserUnfollowButton();
 
     var isGroupEdit = $('body.group.edit').length > 0;
     if (isGroupEdit) {
@@ -1250,69 +1248,97 @@ CKAN.Utils = function($, my) {
   };
 
   my.setupUserFollowButton = function() {
-    $('button.user-follow').click(function(e) {
-      $.ajax({
-        contentType: 'application/json',
-        url: '/api/action/follower_create',
-        data: JSON.stringify({
-               object_id: this.attributes.userid.nodeValue,
-               object_type: 'user',
-        }),
-        dataType: 'json',
-        processData: false,
-        type: 'POST',
-      });
-      return false;
-    });
-  };
-
-  my.setupUserUnfollowButton = function() {
-    $('button.user-unfollow').click(function(e) {
-      $.ajax({
-        contentType: 'application/json',
-        url: '/api/action/follower_delete',
-        data: JSON.stringify({
-               id: this.attributes.userid.nodeValue,
-        }),
-        dataType: 'json',
-        processData: false,
-        type: 'POST',
-      });
-      return false;
-    });
+    $('#user_follow_button').unbind();
+    if ($('#user_follow_button').attr('state') == 'follow')
+    {
+        $('#user_follow_button').html('Follow');
+        $('#user_follow_button').click(function(e) {
+            $.ajax({
+                contentType: 'application/json',
+                url: '/api/action/follower_create',
+                data: JSON.stringify({
+                    object_id: this.attributes.userid.nodeValue,
+                    object_type: 'user',
+                }),
+                dataType: 'json',
+                processData: false,
+                type: 'POST',
+                success: function(data) {
+                    $('#user_follow_button').attr('state', 'unfollow');
+                    my.setupUserFollowButton();
+                },
+            });
+            return false;
+        });
+    }
+    else
+    {
+        $('#user_follow_button').html('Unfollow');
+        $('#user_follow_button').click(function(e) {
+            $.ajax({
+                contentType: 'application/json',
+                url: '/api/action/follower_delete',
+                data: JSON.stringify({
+                    id: this.attributes.userid.nodeValue,
+                }),
+                dataType: 'json',
+                processData: false,
+                type: 'POST',
+                success: function(data) {
+                    $('#user_follow_button').attr('state', 'follow');
+                    my.setupUserFollowButton();
+                },
+            });
+            return false;
+        });
+    }
   };
 
   my.setupDatasetFollowButton = function() {
-    $('button.dataset-follow').click(function(e) {
-      $.ajax({
-        contentType: 'application/json',
-        url: '/api/action/follower_create',
-        data: JSON.stringify({
-               object_id: this.attributes.package_id.nodeValue,
-               object_type: 'dataset',
-        }),
-        dataType: 'json',
-        processData: false,
-        type: 'POST',
-      });
-      return false;
-    });
-  };
-
-  my.setupDatasetUnfollowButton = function() {
-    $('button.dataset-unfollow').click(function(e) {
-      $.ajax({
-        contentType: 'application/json',
-        url: '/api/action/follower_delete',
-        data: JSON.stringify({
-               id: this.attributes.package_id.nodeValue,
-        }),
-        dataType: 'json',
-        processData: false,
-        type: 'POST',
-      });
-      return false;
-    });
+    $('#dataset_follow_button').unbind();
+    if ($('#dataset_follow_button').attr('state') == 'follow')
+    {
+        $('#dataset_follow_button').html('Follow');
+        $('#dataset_follow_button').click(function(e) {
+            $.ajax({
+                contentType: 'application/json',
+                url: '/api/action/follower_create',
+                data: JSON.stringify({
+                    object_id: this.attributes.package_id.nodeValue,
+                    object_type: 'dataset',
+                }),
+                dataType: 'json',
+                processData: false,
+                type: 'POST',
+                success: function(data) {
+                    $('#dataset_follow_button').attr('state', 'unfollow');
+                    my.setupDatasetFollowButton();
+                },
+            });
+            return false;
+        });
+    }
+    else
+    {
+        $('#dataset_follow_button').html('Unfollow');
+        $('#dataset_follow_button').click(function(e) {
+            $.ajax({
+                contentType: 'application/json',
+                url: '/api/action/follower_delete',
+                data: JSON.stringify({
+                    id: this.attributes.package_id.nodeValue,
+                }),
+                dataType: 'json',
+                processData: false,
+                type: 'POST',
+                success: function(data) {
+                    $('#dataset_follow_button').attr('state', 'follow');
+                    my.setupDatasetFollowButton();
+                },
+            });
+            return false;
+        });
+    }
   };
 
   return my;
diff --git a/ckan/templates/package/layout.html b/ckan/templates/package/layout.html
index fb1f2b5..aa72282 100644
--- a/ckan/templates/package/layout.html
+++ b/ckan/templates/package/layout.html
@@ -53,8 +53,8 @@
       </li>
       <li py:if="c.authorized_to_follow">
         <py:choose test="c.pkg_dict.am_following">
-          <button py:when="True" package_id="${c.pkg.id}" class="btn dataset-unfollow">Unfollow</button>
-          <button py:otherwise="" package_id="${c.pkg.id}" class="btn dataset-follow">Follow</button>
+          <button py:when="True" package_id="${c.pkg.id}" state="unfollow" id="dataset_follow_button" class="btn">Unfollow</button>
+          <button py:otherwise="" package_id="${c.pkg.id}" state="follow" id="dataset_follow_button" class="btn">Follow</button>
         </py:choose>
       </li>
     </ul>
diff --git a/ckan/templates/user/layout.html b/ckan/templates/user/layout.html
index f52056c..17d6f80 100644
--- a/ckan/templates/user/layout.html
+++ b/ckan/templates/user/layout.html
@@ -24,8 +24,8 @@
           </li>
           <li py:if="c.authorized_to_follow">
             <py:choose test="c.user_dict.am_following">
-              <button py:when="True" userid="${c.user_dict.id}" class="btn user-unfollow">Unfollow</button>
-              <button py:otherwise="" userid="${c.user_dict.id}" class="btn user-follow">Follow</button>
+              <button py:when="True" userid="${c.user_dict.id}" id="user_follow_button" state="unfollow" class="btn">Unfollow</button>
+              <button py:otherwise="" userid="${c.user_dict.id}" id="user_follow_button" state="follow" class="btn">Follow</button>
             </py:choose>
           </li>
         </py:if>


================================================================
Compare: https://github.com/okfn/ckan/compare/8244875...b5fdb75


More information about the ckan-changes mailing list