Commits

Alessandro Molina committed b46fafa

Add compound index on commented entity and delete button

Comments (0)

Files changed (5)

 
 install_requires=[
     "TurboGears2 >= 2.1.4",
-    "tgext.pluggable"
+    "tgext.pluggable",
+    'tgext.datahelpers'
 ]
 
 here = os.path.abspath(os.path.dirname(__file__))

tgcomments/bootstrap.py

 
 def bootstrap(command, conf, vars):
     print 'Bootstrapping tgcomments...'
+
+    g = app_model.Group(group_name='tgcomments_manager', display_name='TGComments manager')
+    model.DBSession.add(g)
+    model.DBSession.flush()
+
+    u1 = model.DBSession.query(app_model.User).filter_by(user_name='manager').first()
+    if u1:
+        g.users.append(u1)
+    model.DBSession.flush()

tgcomments/controllers/root.py

 from tgcomments.model import DBSession, Comment
 from tgcomments.lib import get_user_gravatar, notify_comment_on_facebook, make_fake_comment_entity, FakeCommentEntity
 
+try:
+    from tg import predicates
+except ImportError:
+    from repoze.what import predicates
+
 from tgext.pluggable import app_model
 
 from formencode.validators import Email, String, Invalid
+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('Comment Added')
         return back_to_referer(success=True)
 
+    @expose()
+    @require(predicates.in_group('tgcomments_manager'))
+    @validate({'comment':SQLAEntityConverter(Comment)},
+              error_handler=fail_with(404))
+    def delete(self, comment):
+        DBSession.delete(comment)
+        flash('Comment Deleted')
+        return back_to_referer(success=True)

tgcomments/model/models.py

+from sqlalchemy.schema import Index
 import tg
 
 from sqlalchemy import Table, ForeignKey, Column
 
 class Comment(DeclarativeBase):
     __tablename__ = 'tgcomments_comments'
+    __table_args__ = (Index('idx_commented_entity', "entity_type", "entity_id"), )
 
     uid = Column(Integer, autoincrement=True, primary_key=True)
 

tgcomments/templates/comments_partial.html

                 </div>
                 <div class="tgcomments_comment_author">
                     ${comment.author_name}
+                    <a py:if="request.identity and 'tgcomments_manager' in request.identity['groups']"
+                       href="${h.plug_url('tgcomments', '/delete/%s'%comment.uid)}"
+                       onclick="return confirm('Permanently delete comment of ${comment.author_name}');">
+                        <img src="${tg.url('/_pluggable/tgcomments/images/trash.gif')}"/>
+                    </a>
                 </div>
             </div>
             <div class="tgcomments_comment_body">${comment.body}</div>
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.