Commits

devilicecream  committed 8513257 Draft

Added likes (still broken)

  • Participants
  • Parent commits 333c00f

Comments (0)

Files changed (2)

File tgcomments/controllers/root.py

         else:
             flash(_('Comment Displayed'))
         return back_to_referer(success=True)
+
+    @expose()
+    @validate({'comment':SQLAEntityConverter(Comment)},
+        error_handler=fail_with(404))
+    def like(self, comment):
+        if request.identity['user'] in comment.have_liked:
+            flash(_('You have already liked or unliked this comment'))
+            return back_to_referer(success=True)
+        comment.likes += 1
+        comment.have_liked.append(request.identity['user'])
+        flash(_('You liked the comment'))
+        return back_to_referer(success=True)
+
+    @expose()
+    @validate({'comment':SQLAEntityConverter(Comment)},
+        error_handler=fail_with(404))
+    def unlike(self, comment):
+        if request.identity['user'] in comment.have_liked:
+            flash(_('You have already liked or unliked this comment'))
+            return back_to_referer(success=True)
+        comment.unlikes += 1
+        comment.have_liked.append(request.identity['user'])
+        flash(_('You unliked the comment'))
+        return back_to_referer(success=True)

File tgcomments/model/models.py

 
 from sqlalchemy import Table, ForeignKey, Column
 from sqlalchemy.types import Unicode, Integer, DateTime, UnicodeText, Boolean
-from sqlalchemy.orm import backref, relation
+from sqlalchemy.orm import backref, relation, relationship
 
 from tgcomments.model import DeclarativeBase, DBSession
 from tgcomments.lib import get_user_avatar
 
 from datetime import datetime
 
+
 class Comment(DeclarativeBase):
     __tablename__ = 'tgcomments_comments'
     __table_args__ = (Index('idx_commented_entity', "entity_type", "entity_id"), )
     body = Column(UnicodeText, nullable=False)
     created_at = Column(DateTime, default=datetime.now, nullable=False)
     hidden = Column(Boolean, default=False, nullable=False)
+    likes = Column(Integer, default=0, nullable=False)
+    unlikes = Column(Integer, default=0, nullable=False)
+
 
     user_id = Column(Integer, ForeignKey(primary_key(app_model.User)), nullable=True)
-    user = relation(app_model.User, backref=backref('comments'))
+    user = relation(app_model.User, backref=backref('comments'), primaryjoin = user_id==app_model.User.user_id)
 
     author_name = Column(Unicode(2048), nullable=False)
     author_pic = Column(Unicode(2048), nullable=True)
 
         DBSession.add(c)
         return c
-
-