[ckan-changes] [okfn/ckan] ca8eb7: [#2304] Change follower_list to return user dicts

GitHub noreply at github.com
Mon Apr 23 17:20:27 UTC 2012


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

  Changed paths:
    M ckan/logic/action/get.py
    M ckan/tests/functional/api/test_follow.py

  Log Message:
  -----------
  [#2304] Change follower_list to return user dicts


diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py
index b59c4e3..8dc5153 100644
--- a/ckan/logic/action/get.py
+++ b/ckan/logic/action/get.py
@@ -1342,7 +1342,12 @@ def follower_list(context, data_dict):
     cursor = conn.execute(q)
     results = []
     for row in cursor:
-        results.append(table_dictize(row, context))
+        follower_id = row['follower_id']
+        assert row['follower_type'] == 'user', (
+                "Currently only users (and not other domain objects) are "
+                "supported as followers.")
+        user = model.User.get(follower_id)
+        results.append(model_dictize.user_dictize(user, context))
     return results
 
 def user_follower_list(context, data_dict):
diff --git a/ckan/tests/functional/api/test_follow.py b/ckan/tests/functional/api/test_follow.py
index 3edbfb2..f2144d4 100644
--- a/ckan/tests/functional/api/test_follow.py
+++ b/ckan/tests/functional/api/test_follow.py
@@ -73,12 +73,7 @@ def test_user_follow_user(self):
         followers = response['result']
         assert len(followers) == 1
         follower = followers[0]
-        assert follower['follower_id'] == self.annafan.id
-        assert follower['follower_type'] == 'user'
-        assert follower['followee_id'] == self.russianfan.id
-        assert follower['followee_type'] == 'user'
-        timestamp = datetime_from_string(follower['datetime'])
-        assert (timestamp >= before and timestamp <= after), str(timestamp)
+        assert follower['id'] == self.annafan.id
 
         # Check that the user's follower count has increased by 1.
         params = json.dumps({'id': self.russianfan.id})


================================================================
  Commit: 3d040bbd3d4f676ca60841fbfbac46b3ece5aac1
      https://github.com/okfn/ckan/commit/3d040bbd3d4f676ca60841fbfbac46b3ece5aac1
  Author: Sean Hammond <seanhammond at lavabit.com>
  Date:   2012-04-23 (Mon, 23 Apr 2012)

  Changed paths:
    M ckan/config/routing.py
    M ckan/controllers/user.py
    A ckan/templates/user/followers.html
    M ckan/templates/user/read.html

  Log Message:
  -----------
  [#2304] Add /user/{id}/followers page with list of user's followers


diff --git a/ckan/config/routing.py b/ckan/config/routing.py
index f3ae5b8..7a0df71 100644
--- a/ckan/config/routing.py
+++ b/ckan/config/routing.py
@@ -235,6 +235,7 @@ def make_map():
         m.connect('/user/edit', action='edit')
         # Note: openid users have slashes in their ids, so need the wildcard
         # in the route.
+        m.connect('/user/{id:.*}/followers', action='followers')
         m.connect('/user/edit/{id:.*}', action='edit')
         m.connect('/user/reset/{id:.*}', action='perform_reset')
         m.connect('/user/register', action='register')
diff --git a/ckan/controllers/user.py b/ckan/controllers/user.py
index 6801676..35e8ce2 100644
--- a/ckan/controllers/user.py
+++ b/ckan/controllers/user.py
@@ -14,7 +14,7 @@
 from ckan.logic import tuplize_dict, clean_dict, parse_params
 from ckan.logic.schema import user_new_form_schema, user_edit_form_schema
 from ckan.logic.action.get import user_activity_list_html
-from ckan.logic.action.get import user_follower_count
+from ckan.logic.action.get import user_follower_count, user_follower_list
 from ckan.lib.captcha import check_recaptcha, CaptchaError
 
 log = logging.getLogger(__name__)
@@ -407,3 +407,18 @@ def _get_form_password(self):
                 raise ValueError(_("The passwords you entered do not match."))
             return password1
 
+    def followers(self, id=None):
+        context = {'model': model, 'user': c.user or c.author,
+                'for_view': True}
+        data_dict = {'id':id, 'user_obj':c.userobj}
+
+        try:
+            user_dict = get_action('user_show')(context,data_dict)
+        except NotFound:
+            h.redirect_to(controller='user', action='login', id=None)
+        except NotAuthorized:
+            abort(401, _('Not authorized to see this page'))
+
+        c.user_dict = user_dict
+        c.followers = user_follower_list(context, {'id':c.user_dict['id']})
+        return render('user/followers.html')
diff --git a/ckan/templates/user/followers.html b/ckan/templates/user/followers.html
new file mode 100644
index 0000000..1540c7f
--- /dev/null
+++ b/ckan/templates/user/followers.html
@@ -0,0 +1,22 @@
+<html xmlns:py="http://genshi.edgewall.org/"
+  xmlns:i18n="http://genshi.edgewall.org/i18n"
+  xmlns:xi="http://www.w3.org/2001/XInclude"
+  py:strip="">
+  
+  <py:def function="page_title">${c.user} - Followers - User</py:def>
+  <py:def function="page_heading">
+      ${c.user_dict.display_name}'s Followers
+  </py:def>
+
+  <div py:match="content">
+    <ul class="userlist">
+     <li py:for="follower in c.followers" class="user">
+        <ul>
+          ${h.linked_user(follower['name'], maxlength=20)}
+        </ul>
+      </li>
+    </ul>
+  </div>
+
+  <xi:include href="layout.html" />
+</html>
diff --git a/ckan/templates/user/read.html b/ckan/templates/user/read.html
index 5722fad..dc94319 100644
--- a/ckan/templates/user/read.html
+++ b/ckan/templates/user/read.html
@@ -59,8 +59,11 @@
               <span>Edits</span>
             </li>
             <li>
-              <strong>${c.num_followers}</strong>
-              <span>Followers</span>
+              <a href="${h.url_for(controller='user', action='followers',
+                  id=c.user_dict.name)}">
+                <strong>${c.num_followers}</strong>
+                <span>Followers</span>
+              </a>
             </li>
           </ul>
         </div>


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

  Changed paths:
    M ckan/logic/action/create.py
    M ckan/public/scripts/application.js
    M ckan/templates/user/layout.html

  Log Message:
  -----------
  [#2304] Initial implementation of follow button


diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py
index 3b00428..57b27f2 100644
--- a/ckan/logic/action/create.py
+++ b/ckan/logic/action/create.py
@@ -486,6 +486,16 @@ def follower_create(context, follower_dict):
     schema = (context.get('schema')
             or ckan.logic.schema.default_create_follower_schema())
 
+    # If no follower_id is given in follower_dict, we use the logged-in user.
+    if not follower_dict.has_key('follower_id'):
+        if not context.has_key('user'):
+            raise logic.NotAuthorized
+        userobj = model.User.get(context['user'])
+        if not userobj:
+            raise logic.NotAuthorized
+        follower_dict['follower_id'] = userobj.id
+        follower_dict['follower_type'] = 'user'
+
     check_access('follower_create', context, follower_dict)
 
     data, errors = validate(follower_dict, schema, context)
diff --git a/ckan/public/scripts/application.js b/ckan/public/scripts/application.js
index 995e703..67e92f6 100644
--- a/ckan/public/scripts/application.js
+++ b/ckan/public/scripts/application.js
@@ -40,6 +40,11 @@ CKAN.Utils = CKAN.Utils || {};
       CKAN.Utils.setupNotesExtract();
     }
 
+    var isUserView = $('body.user.read').length > 0;
+    if (isUserView) {
+      CKAN.Utils.setupUserFollowButton();
+    }
+
     var isResourceView = $('body.package.resource_read').length > 0;
     if (isResourceView) {
       CKAN.DataPreview.loadPreviewDialog(preload_resource);
@@ -1237,6 +1242,25 @@ CKAN.Utils = function($, my) {
     });
     return count;
   };
+
+  my.setupUserFollowButton = function() {
+    var select = $('button.user-follow');
+    $('button.user-follow').click(function(e) {
+      $.ajax({
+        contentType: 'application/json',
+        url: '/api/action/follower_create',
+        data: JSON.stringify({
+               followee_id: this.attributes.userid.nodeValue,
+               followee_type: 'user',
+        }),
+        dataType: 'json',
+        processData: false,
+        type: 'POST',
+      });
+      return false;
+    });
+  };
+
   return my;
 }(jQuery, CKAN.Utils || {});
 
diff --git a/ckan/templates/user/layout.html b/ckan/templates/user/layout.html
index cf4d02c..4e15410 100644
--- a/ckan/templates/user/layout.html
+++ b/ckan/templates/user/layout.html
@@ -20,6 +20,12 @@
           <li class="${'active' if c.action=='login' else ''}"><a href="${h.url_for(controller='user', action='login')}">Login</a></li>
           <li class="${'active' if c.action=='register' else ''}"><a href="${h.url_for(controller='user', action='register')}">Register Account</a></li>
         </py:if>
+
+        <li>
+          <button userid="${c.user_dict.id}" class="btn user-follow">
+            Follow
+          </button>
+        </li>
       </py:otherwise>
     </ul>
   </py:match>


================================================================
Compare: https://github.com/okfn/ckan/compare/7c73209...ab75728


More information about the ckan-changes mailing list