Skip to content

Commit

Permalink
[#2941] Add /user/unfollow/NAME page
Browse files Browse the repository at this point in the history
 To unfollow a user without javascript
  • Loading branch information
Sean Hammond committed Oct 4, 2012
1 parent ae937e0 commit a663b5c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions ckan/config/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def make_map():
# in the route.
m.connect('/user/dashboard', action='dashboard')
m.connect('/user/follow/{id}', action='follow')
m.connect('/user/unfollow/{id}', action='unfollow')
m.connect('/user/followers/{id:.*}', action='followers')
m.connect('/user/edit/{id:.*}', action='edit')
m.connect('/user/reset/{id:.*}', action='perform_reset')
Expand Down
18 changes: 18 additions & 0 deletions ckan/controllers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,21 @@ def follow(self, id):
except NotAuthorized as e:
h.flash_error(e.extra_msg)
h.redirect_to(controller='user', action='read', id=id)

def unfollow(self, id):
'''Stop following this user.'''
context = {'model': model,
'session': model.Session,
'user': c.user or c.author}
data_dict = {'id': id}
try:
get_action('unfollow_user')(context, data_dict)
h.flash_success(_("You are no longer following {0}").format(id))
except (NotFound, NotAuthorized) as e:
error_message = e.extra_msg or e.message
h.flash_error(error_message)
except ValidationError as e:
error_message = (e.error_summary or e.message or e.extra_msg
or e.error_dict)
h.flash_error(error_message)
h.redirect_to(controller='user', action='read', id=id)
30 changes: 14 additions & 16 deletions ckan/logic/action/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,23 +311,28 @@ def package_relationship_delete_rest(context, data_dict):

package_relationship_delete(context, data_dict)

def _unfollow(context, data_dict, FollowerClass):
def _unfollow(context, data_dict, schema, FollowerClass):
validated_data_dict, errors = validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

model = context['model']

if not context.has_key('user'):
raise ckan.logic.NotAuthorized
raise ckan.logic.NotAuthorized(
_("You must be logged in to unfollow something."))
userobj = model.User.get(context['user'])
if not userobj:
raise ckan.logic.NotAuthorized
raise ckan.logic.NotAuthorized(
_("You must be logged in to unfollow something."))
follower_id = userobj.id

object_id = data_dict.get('id')
object_id = validated_data_dict.get('id')

follower_obj = FollowerClass.get(follower_id, object_id)
if follower_obj is None:
raise NotFound(
_('Could not find follower {follower} -> {object}').format(
follower=follower_id, object=object_id))
_('You are not following {0}.').format(data_dict.get('id')))

follower_obj.delete()
model.repo.commit()
Expand All @@ -341,11 +346,7 @@ def unfollow_user(context, data_dict):
'''
schema = context.get('schema') or (
ckan.logic.schema.default_follow_user_schema())
data_dict, errors = validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

_unfollow(context, data_dict, context['model'].UserFollowingUser)
_unfollow(context, data_dict, schema, context['model'].UserFollowingUser)

def unfollow_dataset(context, data_dict):
'''Stop following a dataset.
Expand All @@ -356,8 +357,5 @@ def unfollow_dataset(context, data_dict):
'''
schema = context.get('schema') or (
ckan.logic.schema.default_follow_dataset_schema())
data_dict, errors = validate(data_dict, schema, context)
if errors:
raise ValidationError(errors)

_unfollow(context, data_dict, context['model'].UserFollowingDataset)
_unfollow(context, data_dict, schema,
context['model'].UserFollowingDataset)

0 comments on commit a663b5c

Please sign in to comment.