Alessandro Molina avatar Alessandro Molina committed 0a5e04d

Add function to hide comments instead of deleting them

Comments (0)

Files changed (4)

 
 setup(
     name='tgapp-tgcomments',
-    version='0.0.1',
+    version='0.0.2',
     description='TurboGears2 pluggable application for comments to entities with facebook sharing',
     long_description=README,
     author='Alessandro Molina',

tgcomments/controllers/root.py

 
         c = Comment.add_comment(entity, user, kw['body'])
         notify_comment_on_facebook(request.referer, c)
-        flash('Comment Added')
+        flash(_('Comment Added'))
         return back_to_referer(success=True)
 
     @expose()
               error_handler=fail_with(404))
     def delete(self, comment):
         DBSession.delete(comment)
-        flash('Comment Deleted')
+        flash(_('Comment Deleted'))
         return back_to_referer(success=True)
+
+    @expose()
+    @require(predicates.in_group('tgcmanager'))
+    @validate({'comment':SQLAEntityConverter(Comment)},
+              error_handler=fail_with(404))
+    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)

tgcomments/model/models.py

 import tg
 
 from sqlalchemy import Table, ForeignKey, Column
-from sqlalchemy.types import Unicode, Integer, DateTime, UnicodeText
+from sqlalchemy.types import Unicode, Integer, DateTime, UnicodeText, Boolean
 from sqlalchemy.orm import backref, relation
 
 from tgcomments.model import DeclarativeBase, DBSession
 
     body = Column(UnicodeText, nullable=False)
     created_at = Column(DateTime, default=datetime.now, nullable=False)
+    hidden = Column(Boolean, default=False, nullable=False)
 
     user_id = Column(Integer, ForeignKey(primary_key(app_model.User)), nullable=True)
     user = relation(app_model.User, backref=backref('comments'))
         return Type.__name__, getattr(entity, type_primary_key.key)
 
     @classmethod
-    def comments_for(cls, entity):
+    def comments_for(cls, entity, hidden='auto'):
         entity_type, entity_id = cls.get_entity_descriptor(entity)
 
-        return DBSession.query(cls).filter_by(entity_type=entity_type)\
-                                   .filter_by(entity_id=entity_id)\
-                                   .order_by(cls.created_at.desc()).all()
+        comments = DBSession.query(cls).filter_by(entity_type=entity_type)\
+                                       .filter_by(entity_id=entity_id)
+
+        if not (hidden==True or \
+                (hidden=='auto' and tg.request.identity and 'tgcmanager' in tg.request.identity['groups'])):
+            comments = comments.filter_by(hidden==False)
+
+        return comments.order_by(cls.created_at.desc()).all()
 
     @classmethod
     def add_comment(cls, entity, user, body):

tgcomments/templates/comments_partial.html

         </form>
     </div>
     <div class="tgcomments_comments">
-        <div class="tgcomments_comment" py:for="comment in comments">
+        <div py:for="comment in comments" class="tgcomments_comment ${comment.hidden and 'tgcomments_hidden' or ''}">
             <div class="tgcomments_comment_head">
                 <div class="tgcomments_comment_avatar">
                     <img style="height:32px;" src="${comment.author_pic}"/>
                 </div>
-                <div class="tgcomments_comment_author">
-                    ${comment.author_name}
+                <div class="tgcomments_comment_author ${}">
+                    <span>${comment.author_name}</span>
+                    <a py:if="request.identity and 'tgcmanager' in request.identity['groups']"
+                       href="${h.plug_url('tgcomments', '/hide/%s'%comment.uid)}">
+                        <img src="${tg.url('/_pluggable/tgcomments/images/hide.gif')}"/>
+                    </a>
                     <a py:if="request.identity and 'tgcmanager' in request.identity['groups']"
                        href="${h.plug_url('tgcomments', '/delete/%s'%comment.uid)}"
                        onclick="return confirm('Permanently delete comment of ${comment.author_name}');">
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.