Skip to content

Commit

Permalink
[#2304] Initial implementation of follow button
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hammond committed Apr 23, 2012
1 parent 3d040bb commit ab75728
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ckan/logic/action/create.py
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions ckan/public/scripts/application.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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 || {});

Expand Down
6 changes: 6 additions & 0 deletions ckan/templates/user/layout.html
Expand Up @@ -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>
Expand Down

0 comments on commit ab75728

Please sign in to comment.