Skip to content

Commit

Permalink
[#2941] Add /user/follow/NAME page
Browse files Browse the repository at this point in the history
To follow a user without javascript
  • Loading branch information
Sean Hammond committed Oct 4, 2012
1 parent 4c0a22e commit 05ea51c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions ckan/config/routing.py
Expand Up @@ -257,6 +257,7 @@ def make_map():
# Note: openid users have slashes in their ids, so need the wildcard
# in the route.
m.connect('/user/dashboard', action='dashboard')
m.connect('/user/follow/{id}', action='follow')
m.connect('/user/followers/{id:.*}', action='followers')
m.connect('/user/edit/{id:.*}', action='edit')
m.connect('/user/reset/{id:.*}', action='perform_reset')
Expand Down
17 changes: 17 additions & 0 deletions ckan/controllers/user.py
Expand Up @@ -481,3 +481,20 @@ def dashboard(self, id=None):
data_dict = {'id': id, 'user_obj': c.userobj}
self._setup_template_variables(context, data_dict)
return render('user/dashboard.html')

def follow(self, id):
'''Start following this user.'''
context = {'model': model,
'session': model.Session,
'user': c.user or c.author}
data_dict = {'id': id}
try:
get_action('follow_user')(context, data_dict)
h.flash_success(_("You are now following {0}").format(id))
except ValidationError as e:
error_message = (e.extra_msg or e.message or e.error_summary
or e.error_dict)
h.flash_error(error_message)
except NotAuthorized as e:
h.flash_error(e.extra_msg)
h.redirect_to(controller='user', action='read', id=id)
28 changes: 15 additions & 13 deletions ckan/logic/action/create.py
Expand Up @@ -893,51 +893,53 @@ def follow_user(context, data_dict):
:rtype: dictionary
'''
if not context.has_key('user'):
raise logic.NotAuthorized
if 'user' not in context:
raise logic.NotAuthorized(_("You must be logged in to follow users"))

model = context['model']
session = context['session']

userobj = model.User.get(context['user'])
if not userobj:
raise logic.NotAuthorized
raise logic.NotAuthorized(_("You must be logged in to follow users"))

schema = (context.get('schema')
or ckan.logic.schema.default_follow_user_schema())

data_dict, errors = _validate(data_dict, schema, context)
validated_data_dict, errors = _validate(data_dict, schema, context)

if errors:
model.Session.rollback()
raise ValidationError(errors)

# Don't let a user follow herself.
if userobj.id == data_dict['id']:
if userobj.id == validated_data_dict['id']:
message = _('You cannot follow yourself')
raise ValidationError({'message': message})
raise ValidationError({'message': message}, error_summary=message)

# Don't let a user follow someone she is already following.
if model.UserFollowingUser.get(userobj.id, data_dict['id']) is not None:
if model.UserFollowingUser.is_following(userobj.id,
validated_data_dict['id']):
message = _(
'You are already following {id}').format(id=data_dict['id'])
raise ValidationError({'message': message})
'You are already following {0}').format(data_dict['id'])
raise ValidationError({'message': message}, error_summary=message)

follower = model_save.user_following_user_dict_save(data_dict, context)
follower = model_save.user_following_user_dict_save(validated_data_dict,
context)

activity_dict = {
'user_id': userobj.id,
'object_id': data_dict['id'],
'object_id': validated_data_dict['id'],
'activity_type': 'follow user',
}
activity_dict['data'] = {
'user': ckan.lib.dictization.table_dictize(
model.User.get(data_dict['id']), context),
model.User.get(validated_data_dict['id']), context),
}
activity_create_context = {
'model': model,
'user': userobj,
'defer_commit':True,
'defer_commit': True,
'session': session
}
logic.get_action('activity_create')(activity_create_context,
Expand Down
4 changes: 3 additions & 1 deletion ckan/logic/schema.py
Expand Up @@ -40,6 +40,7 @@
activity_type_exists,
tag_not_in_vocabulary,
url_validator)
from ckan.logic.converters import (convert_user_name_or_id_to_id,)
from formencode.validators import OneOf
import ckan.model

Expand Down Expand Up @@ -418,7 +419,8 @@ def default_create_activity_schema():
return schema

def default_follow_user_schema():
schema = {'id': [not_missing, not_empty, unicode, user_id_or_name_exists]}
schema = {'id': [not_missing, not_empty, unicode,
convert_user_name_or_id_to_id]}
return schema

def default_follow_dataset_schema():
Expand Down

0 comments on commit 05ea51c

Please sign in to comment.