def vote(self, user_id): """ allow a user to vote on a thread. if we have voted already (and they are clicking again), this means that they are trying to unvote the thread, return status of the vote for that user """ already_voted = self.has_voted(user_id) vote_status = None if not already_voted: # vote up the thread db.engine.execute( thread_upvotes.insert(), user_id = user_id, thread_id = self.id ) self.votes = self.votes + 1 vote_status = True else: # unvote the thread db.engine.execute( thread_upvotes.delete( db.and_( thread_upvotes.c.user_id == user_id, thread_upvotes.c.thread_id == self.id ) ) ) self.votes = self.votes - 1 vote_status = False db.session.commit() # for the vote count return vote_status
def vote(self, user_id): """ allow a user to vote on a thread. if we have voted already (and they are clicking again), this means that they are trying to unvote the thread, return status of the vote for that user """ already_voted = self.has_voted(user_id) vote_status = None if not already_voted: # vote up the thread db.engine.execute(thread_upvotes.insert(), user_id=user_id, thread_id=self.id) self.votes = self.votes + 1 vote_status = True else: # unvote the thread db.engine.execute( thread_upvotes.delete( db.and_(thread_upvotes.c.user_id == user_id, thread_upvotes.c.thread_id == self.id))) self.votes = self.votes - 1 vote_status = False db.session.commit() # for the vote count return vote_status
def has_voted(self, user_id): """ did the user vote already """ select_votes = thread_upvotes.select( db.and_(thread_upvotes.c.user_id == user_id, thread_upvotes.c.thread_id == self.id)) rs = db.engine.execute(select_votes) return False if rs.rowcount == 0 else True
def get_comment_karma(self): """ fetch the number of votes this user has had on his/her comments """ comment_ids = [c.id for c in self.comments] select = comment_upvotes.select( db.and_(comment_upvotes.c.comment_id.in_(comment_ids), comment_upvotes.c.user_id != self.id)) rs = db.engine.execute(select) return rs.rowcount
def has_voted(self, user_id): """ did the user vote already """ select_votes = thread_upvotes.select( db.and_( thread_upvotes.c.user_id == user_id, thread_upvotes.c.thread_id == self.id ) ) rs = db.engine.execute(select_votes) return False if rs.rowcount == 0 else True
def get_comment_karma(self): """ fetch the number of votes this user has had on his/her comments """ comment_ids = [c.id for c in self.comments] select = comment_upvotes.select(db.and_( comment_upvotes.c.comment_id.in_(comment_ids), comment_upvotes.c.user_id != self.id ) ) rs = db.engine.execute(select) return rs.rowcount
def get_thread_karma(self): """ fetch the number of votes this user has had on his/her threads 1.) Get id's of all threads by this user 2.) See how many of those threads also were upvoted but not by the person him/her self. """ thread_ids = [t.id for t in self.threads] select = thread_upvotes.select( db.and_(thread_upvotes.c.thread_id.in_(thread_ids), thread_upvotes.c.user_id != self.id)) rs = db.engine.execute(select) return rs.rowcount
def get_thread_karma(self): """ fetch the number of votes this user has had on his/her threads 1.) Get id's of all threads by this user 2.) See how many of those threads also were upvoted but not by the person him/her self. """ thread_ids = [t.id for t in self.threads] select = thread_upvotes.select(db.and_( thread_upvotes.c.thread_id.in_(thread_ids), thread_upvotes.c.user_id != self.id ) ) rs = db.engine.execute(select) return rs.rowcount