async def commit(self): logger.debug("~> Commiting") for key, item in self.store.items(): logger.debug("~~> Key: %r", key) Model = item.__model__ # Create if key not in self.origin and item._created is not None: logger.debug(f"<~~ Created {item.__class__.__name__}") result = await Model.create(**item.serialize()) yield (f"Created {key}", result) # Hard delete elif item._created is None: if key not in self.origin: logger.debug("<~~ Skipped") continue else: logger.debug(f"<~~ Deleted {item.__class__.__name__}") result = await Model.delete.where( db.and_( Model._id == item._id, Model._etag == self.origin[key]._etag, )).gino.status() yield (f"Deleted {key}", result) # Update or soft delete elif item._etag != self.origin[key]._etag: logger.debug(f"<~~ Updated {item.__class__.__name__}") result = (await Model.update.values(**item.serialize()).where( db.and_(Model._id == item._id, Model._etag == self.origin[key]._etag) ).gino.status()) yield (f"Updated {key}", result) # elif isinstance(item, Resource) and item is not self.origin[key]: # logger.debug(f"<~~ Updated {item.__class__.__name__} Resource") # Model = item.__model__ # result = ( # await Model.update.values(**item.serialize()) # .where(Model._id == item._id) # .gino.status() # ) # yield (f"Updated {key} Resource", result) else: logger.debug("<~~ Skiped") logger.debug("<~ Done") self.store.clear() self.origin.clear()
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 save(self, user_id): """ allow a user to save a thread. if we have savered already (and they are clicking again), this means that they are trying to unsave the thread, return status of the star for that user """ already_saved = self.has_saved(user_id) save_status = None if not already_saved: # star up the thread db.engine.execute(thread_saves.insert(), user_id=user_id, thread_id=self.id) self.saves = self.saves + 1 save_status = True else: # unstar the thread db.engine.execute( thread_saves.delete( db.and_(thread_saves.c.user_id == user_id, thread_saves.c.thread_id == self.id))) self.saves = self.saves - 1 vote_status = False db.session.commit() # for the vote count return save_status
def has_downloaded(self, user_id): """ Has the user downloaded the PDF? """ rs = Publication_Download.query.filter( db.and_(Publication_Download.user_id == user_id, Publication_Download.publication_id == self.id)) return True if rs.first() else False
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 vote(self, user_id): """ Add a vote from user id to a comment. """ already_voted = self.has_voted(user_id) vote_status = None if not already_voted: # vote up the thread db.engine.execute(comment_upvotes.insert(), user_id=user_id, comment_id=self.id) self.votes = self.votes + 1 vote_status = True else: # unvote the thread db.engine.execute( comment_upvotes.delete( db.and_(comment_upvotes.c.user_id == user_id, comment_upvotes.c.comment_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): select_votes = comment_upvotes.select( db.and_(comment_upvotes.c.user_id == user_id, comment_upvotes.c.comment_id == self.id)) rs = db.engine.execute(select_votes) return False if rs.rowcount == 0 else True