Skip to content

Commit

Permalink
Merge branch 'feature-2484-followers-as-helper'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hammond committed Jun 21, 2012
2 parents 84d674d + b9d5051 commit 1d4f02a
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 72 deletions.
29 changes: 0 additions & 29 deletions ckan/controllers/package.py
Expand Up @@ -94,14 +94,6 @@ 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.
if c.user:
c.pkg_dict['am_following'] = get_action('am_following_dataset')(
context, {'id': c.pkg.id})

authorizer = ckan.authz.Authorizer()

def search(self):
Expand Down Expand Up @@ -293,10 +285,6 @@ def read(self, id, format='html'):
get_action('package_activity_list_html')(context,
{'id': c.current_package_id})

c.num_followers = get_action('dataset_follower_count')(context,
{'id':c.pkg.id})
self._setup_follow_button(context)

PackageSaver().render_package(c.pkg_dict, context)

template = self._read_template( package_type )
Expand Down Expand Up @@ -360,10 +348,6 @@ def history(self, id):
except NotFound:
abort(404, _('Dataset not found'))

c.num_followers = get_action('dataset_follower_count')(
context, {'id':c.pkg.id})
self._setup_follow_button(context)

format = request.params.get('format', '')
if format == 'atom':
# Generate and return Atom 1.0 document.
Expand Down Expand Up @@ -486,10 +470,6 @@ 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)

c.num_followers = get_action('dataset_follower_count')(context,
{'id':c.pkg.id})
self._setup_follow_button(context)

if (c.action == u'editresources'):
return render('package/editresources.html')
else:
Expand Down Expand Up @@ -667,10 +647,6 @@ def authz(self, id):
roles = self._handle_update_of_authz(pkg)
self._prepare_authz_info_for_render(roles)

c.num_followers = get_action('dataset_follower_count')(context,
{'id':c.pkg.id})
self._setup_follow_button(context)

# c.related_count = len(pkg.related)

return render('package/authz.html')
Expand Down Expand Up @@ -759,9 +735,6 @@ def resource_read(self, id, resource_id):
qualified=True)

c.related_count = len(c.pkg.related)
c.num_followers = get_action('dataset_follower_count')(context,
{'id':c.pkg.id})
self._setup_follow_button(context)
return render('package/resource_read.html')

def resource_download(self, id, resource_id):
Expand Down Expand Up @@ -793,8 +766,6 @@ def followers(self, id=None):
c.pkg = context['package']
c.followers = get_action('dataset_follower_list')(context,
{'id': c.pkg_dict['id']})
c.num_followers = len(c.followers)
self._setup_follow_button(context)

c.related_count = len(c.pkg.related)
except NotFound:
Expand Down
7 changes: 0 additions & 7 deletions ckan/controllers/related.py
Expand Up @@ -34,12 +34,5 @@ def list(self, id):

c.related_count = len(c.pkg.related)

c.num_followers = logic.get_action('dataset_follower_count')(context,
{'id': c.pkg_dict['id']})
# If the user is logged in set the am_following variable.
if c.user:
c.pkg_dict['am_following'] = logic.get_action('am_following_dataset')(
context, {'id': c.pkg.id})

return base.render( "package/related_list.html")

19 changes: 0 additions & 19 deletions ckan/controllers/user.py
Expand Up @@ -46,22 +46,6 @@ def _db_to_edit_form_schema(self):
'''This is an interface to manipulate data from the database
into a format suitable for the form (optional)'''

def _setup_follow_button(self, context):
'''Setup some template context variables needed for the Follow/Unfollow
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.user_dict['am_following'] = get_action('am_following_user')(context,
{'id': c.user_dict['id']})

def _setup_template_variables(self, context, data_dict):
c.is_sysadmin = Authorizer().is_sysadmin(c.user)
try:
Expand All @@ -72,9 +56,6 @@ def _setup_template_variables(self, context, data_dict):
abort(401, _('Not authorized to see this page'))
c.user_dict = user_dict
c.is_myself = user_dict['name'] == c.user
c.num_followers = get_action('user_follower_count')(context,
{'id':c.user_dict['id']})
self._setup_follow_button(context)

## end hooks

Expand Down
54 changes: 54 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -819,6 +819,58 @@ def process_names(items):
items.append(item)
return items

# these are the types of objects that can be followed
_follow_objects = ['dataset', 'user']

def follow_button(obj_type, obj_id):
'''Return a follow button for the given object type and id.
If the user is not logged in return an empty string instead.
:param obj_type: the type of the object to be followed when the follow
button is clicked, e.g. 'user' or 'dataset'
:type obj_type: string
:param obj_id: the id of the object to be followed when the follow button
is clicked
:type obj_id: string
:returns: a follow button as an HTML snippet
:rtype: string
'''
import ckan.logic as logic
obj_type = obj_type.lower()
assert obj_type in _follow_objects
# If the user is logged in show the follow/unfollow button
if c.user:
context = {'model' : model, 'session':model.Session, 'user':c.user}
action = 'am_following_%s' % obj_type
following = logic.get_action(action)(context, {'id': obj_id})
return snippet('snippets/follow_button.html',
following=following,
obj_id=obj_id,
obj_type=obj_type)
return ''

def follow_count(obj_type, obj_id):
'''Return the number of followers of an object.
:param obj_type: the type of the object, e.g. 'user' or 'dataset'
:type obj_type: string
:param obj_id: the id of the object
:type obj_id: string
:returns: the number of followers of the object
:rtype: int
'''
import ckan.logic as logic
obj_type = obj_type.lower()
assert obj_type in _follow_objects
action = '%s_follower_count' % obj_type
context = {'model' : model, 'session':model.Session, 'user':c.user}
return logic.get_action(action)(context, {'id': obj_id})


# these are the functions that will end up in `h` template helpers
# if config option restrict_template_vars is true
Expand Down Expand Up @@ -873,6 +925,8 @@ def process_names(items):
'activity_div',
'lang_native_name',
'unselected_facet_items',
'follow_button',
'follow_count',
# imported into ckan.lib.helpers
'literal',
'link_to',
Expand Down
7 changes: 3 additions & 4 deletions ckan/public/scripts/application.js
Expand Up @@ -1424,16 +1424,15 @@ CKAN.Utils = function($, my) {
function followButtonClicked(event) {
var button = event.currentTarget;
if (button.id === 'user_follow_button') {
var object_id = button.getAttribute('data-user-id');
var object_type = 'user';
} else if (button.id === 'dataset_follow_button') {
var object_id = button.getAttribute('data-dataset-id');
var object_type = 'dataset';
}
else {
// This shouldn't happen.
return;
}
var object_id = button.getAttribute('data-obj-id');
if (button.getAttribute('data-state') === "follow") {
if (object_type == 'user') {
var url = '/api/action/follow_user';
Expand All @@ -1447,7 +1446,7 @@ CKAN.Utils = function($, my) {
id: object_id,
});
var nextState = 'unfollow';
var nextString = 'Unfollow';
var nextString = CKAN.Strings.unfollow;
} else if (button.getAttribute('data-state') === "unfollow") {
if (object_type == 'user') {
var url = '/api/action/unfollow_user';
Expand All @@ -1461,7 +1460,7 @@ CKAN.Utils = function($, my) {
id: object_id,
});
var nextState = 'follow';
var nextString = 'Follow';
var nextString = CKAN.Strings.follow;
}
else {
// This shouldn't happen.
Expand Down
4 changes: 3 additions & 1 deletion ckan/templates/js_strings.html
Expand Up @@ -67,7 +67,9 @@
youCanUseMarkdown = _('You can use %aMarkdown formatting%b here.'),
shouldADataStoreBeEnabled = _('Should a %aDataStore table and Data API%b be enabled for this resource?'),
datesAreInISO = _('Dates are in %aISO Format%b — eg. %c2012-12-25%d or %c2010-05-31T14:30%d.'),
dataFileUploaded = _('Data File (Uploaded)')
dataFileUploaded = _('Data File (Uploaded)'),
follow = _('Follow'),
unfollow = _('Unfollow'),
), indent=4)}

</script>
Expand Down
9 changes: 3 additions & 6 deletions ckan/templates/package/layout.html
Expand Up @@ -37,8 +37,8 @@
<li class="${'active' if c.action=='history' else ''}">${h.subnav_link(h.icon('page_stack') + _('History'), controller='package', action='history', id=c.pkg.name)}</li>
<li class="${'active' if c.action=='followers' else ''}" style="float:right;">
${h.subnav_link(
h.icon('authorization_group') + _('Followers ({num_followers})').format(num_followers=c.num_followers),
controller='package',
h.icon('authorization_group') + _('Followers ({num_followers})').format(num_followers=h.follow_count('dataset', c.pkg_dict.id)),
controller='package',
action='followers',
id=c.pkg.name)}
</li>
Expand All @@ -58,10 +58,7 @@
${h.subnav_link(h.icon('lock') + _('Authorization'), controller='package', action='authz', id=c.pkg.name)}
</li>
<li py:if="c.user" style="float:right;">
<py:choose test="c.pkg_dict.am_following">
<a py:when="True" id="dataset_follow_button" class="btn btn-mini" data-dataset-id="${c.pkg.id}" data-state="unfollow">Unfollow</a>
<a py:otherwise="" id="dataset_follow_button" class="btn btn-mini" data-dataset-id="${c.pkg.id}" data-state="follow">Follow</a>
</py:choose>
${h.follow_button('dataset', c.pkg_dict.id)}
</li>
</ul>
</py:match>
Expand Down
11 changes: 11 additions & 0 deletions ckan/templates/snippets/follow_button.html
@@ -0,0 +1,11 @@
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip=""
>
<py:choose test="following">
<a py:when="True" id="${obj_type}_follow_button" class="btn btn-mini" data-obj-id="${obj_id}" data-state="unfollow">Unfollow</a>
<a py:otherwise="" id="${obj_type}_follow_button" class="btn btn-mini" data-obj-id="${obj_id}" data-state="follow">Follow</a>
</py:choose>
</html>
9 changes: 3 additions & 6 deletions ckan/templates/user/layout.html
Expand Up @@ -13,7 +13,7 @@
<li><a href="${h.url_for('/user/logout')}">Log out</a></li>
<li style="float:right;" class="${'active' if c.action=='followers' else ''}">
${h.subnav_link(
h.icon('authorization_group') + _('My Followers ({num_followers})').format(num_followers=c.num_followers),
h.icon('authorization_group') + _('My Followers ({num_followers})').format(num_followers=h.follow_count('user', c.user_dict.id)),
controller='user',
action='followers',
id=c.user_dict.name)}
Expand All @@ -24,16 +24,13 @@
<li class="${'active' if c.action=='read' else ''}"><a href="${h.url_for(controller='user', action='read', id=c.user_dict.name)}">View Profile</a></li>
<li style="float:right;" class="${'active' if c.action=='followers' else ''}">
${h.subnav_link(
h.icon('authorization_group') + _('Followers ({num_followers})').format(num_followers=c.num_followers),
h.icon('authorization_group') + _('Followers ({num_followers})').format(num_followers=h.follow_count('user', c.user_dict.id)),
controller='user',
action='followers',
id=c.user_dict.name)}
</li>
<li py:if="c.user" style="float:right;">
<py:choose test="c.user_dict.am_following">
<a py:when="True" id="user_follow_button" class="btn btn-mini" data-user-id="${c.user_dict.id}" data-state="unfollow">Unfollow</a>
<a py:otherwise="" id="user_follow_button" class="btn btn-mini" data-user-id="${c.user_dict.id}" data-state="follow">Follow</a>
</py:choose>
${h.follow_button('user', c.user_dict.id)}
</li>
</py:if>
<py:if test="not c.id">
Expand Down

0 comments on commit 1d4f02a

Please sign in to comment.