Commits

Simone Marzola committed 8c5406f

refactored back_to_referer function to dinamically display flash message

  • Participants
  • Parent commits 3c94e68

Comments (0)

Files changed (1)

File tgcomments/controllers/root.py

 from tgext.datahelpers.validators import SQLAEntityConverter
 from tgext.datahelpers.utils import fail_with
 
-def back_to_referer(*args, **kw):
-    if not kw.get('success'):
-        flash(_('Failed to post comment'), 'error')
+def back_to_referer(message=None, status='ok'):
+    if message:
+        flash(_(message), status)
     if request.referer is not None:
         raise redirect(request.referer)
     raise redirect(request.host_url)
         entity_type = getattr(app_model, kw['entity_type'], None)
         entity_id = kw['entity_id']
         if entity_type is None or entity_id is None:
-            return back_to_referer()
+            return back_to_referer('Failed to post comment', status='error')
 
         if issubclass(entity_type, FakeCommentEntity):
             entity = make_fake_comment_entity(entity_type, entity_id)
         else:
             entity = DBSession.query(entity_type).get(entity_id)
             if not entity:
-                return back_to_referer()
+                return back_to_referer('Failed to post comment', status='error')
 
         if not request.identity:
             try:
                 user = {'name':String(not_empty=True).to_python(kw.get('author')),
                         'avatar':get_user_gravatar(Email(not_empty=True).to_python(kw.get('email')))}
             except Invalid:
-                return back_to_referer()
+                return back_to_referer('Failed to post comment', status='error')
         else:
             user = request.identity['user']
 
         c = Comment.add_comment(entity, user, kw['body'])
         notify_comment_on_facebook(request.referer, c)
-        flash(_('Comment Added'))
-        return back_to_referer(success=True)
+        return back_to_referer('Comment Added')
 
     @expose()
     @require(predicates.in_group('tgcmanager'))
               error_handler=fail_with(404))
     def delete(self, comment):
         DBSession.delete(comment)
-        flash(_('Comment Deleted'))
-        return back_to_referer(success=True)
+        return back_to_referer('Comment Deleted')
 
     @expose()
     @require(predicates.in_group('tgcmanager'))
     def hide(self, comment):
         comment.hidden = not comment.hidden
         if comment.hidden:
-            flash(_('Comment Hidden'))
-        else:
-            flash(_('Comment Displayed'))
-        return back_to_referer(success=True)
+            return back_to_referer('Comment Hidden')
+        return back_to_referer('Comment Displayed')
 
     @expose()
     @require(predicates.not_anonymous())
             DBSession.flush()
         except IntegrityError:
             transaction.doom()
-            flash(_('Already voted this comment'), 'warning')
-            return back_to_referer(success=True)
+            return back_to_referer('Already voted this comment', 'warning')
+        return back_to_referer('Thanks for your vote!')
 
-        flash(_('Thanks for your vote!'))
-        return back_to_referer(success=True)
-