def archive(self): """ Archive the link Link get archived to save some memory in redis/db by disallowing voting and commenting on old links Upon archiving of the link all votes get deleted and only the final score is kept Same thing happens with the votes on comments of this link - votes get deleted and only final score is kept """ # delete link votes from DB LinkVote.where("link_id", self.id).delete() # delete cached link votes link_upvotes_key, link_downvotes_key = LinkVote.set_keys(self.id) cache.delete(link_upvotes_key) cache.delete(link_downvotes_key) # delete comment votes for comment in Comment.where("link_id", self.id): # delete cached comment votes comment_upvotes_key, comment_downvotes_key = CommentVote.set_keys( comment.id ) cache.delete(comment_upvotes_key) cache.delete(comment_downvotes_key) # delete comment votes from DB CommentVote.where("comment_id", comment.id).delete() # update self with self.get_read_modify_write_lock(): self.archived = True self.update_with_cache()
def create_tables(): User.create_table() Feed.create_table() Link.create_table() Ban.create_table() Comment.create_table() FeedAdmin.create_table() Report.create_table() create_subscriptions_table() DisposableToken.create_table() CommentVote.create_table() LinkVote.create_table()
def create_tables(app): with app.app_context(): from news.models.disposable_token import DisposableToken DisposableToken.create_table() from news.models.feed import Feed Feed.create_table() from news.models.user import User User.create_table() from news.models.link import Link Link.create_table() from news.models.vote import LinkVote LinkVote.create_table() from news.models.comment import Comment Comment.create_table() from news.models.vote import CommentVote CommentVote.create_table() from news.models.subscriptions import create_subscriptions_table create_subscriptions_table() from news.models.feed_admin import FeedAdmin FeedAdmin.create_table() from news.models.report import Report Report.create_table() from news.models.link import SavedLink SavedLink.create_table() from news.models.ban import Ban Ban.create_table()
def login(self, remember_me: bool = False): """ Login the user Generate access token which gets saved in session, info about user is then saved to redis :param remember_me: """ token = DisposableToken.get() self._accessor_cache["session_token"] = token.id session_key = "us:{}".format(self.session_token) cache.set(session_key, self.id, ttl=0 if remember_me else 60 * 60 * 2, raw=True) login_user(self, remember=remember_me) LinkVote.by_user_and_vote_type(self.id, UPVOTE) LinkVote.by_user_and_vote_type(self.id, DOWNVOTE) CommentVote.by_user_and_vote_type(self.id, UPVOTE) CommentVote.by_user_and_vote_type(self.id, DOWNVOTE)
def votes(self): """ Get link votes :return: """ from news.models.vote import LinkVote return LinkVote.where("link_id", self.id).get()
def vote_by(self, user: "******") -> Optional["LinkVote"]: """ Get link vote by user :param user: user :return: vote """ from news.models.vote import LinkVote if user.is_anonymous: return None return LinkVote.by_link_and_user(self.id, user.id)
def do_vote(link, vote_str=None): """ Vote on link All kinds of votes are handled here (up, down, unvote) :param link: link :param vote_str: vote type :return: """ if (current_user.is_authenticated and Ban.by_user_and_feed(current_user, link.feed) is not None): abort(403) if link.archived: abort(405) vote = vote_type_from_string(vote_str) vote = LinkVote(user_id=current_user.id, link_id=link.id, vote_type=vote) vote.apply() return redirect(redirect_back(link.route))
def comment_downvotes(self): from news.models.vote import LinkVote if "cd" not in self._relations: self._relations["cd"] = LinkVote.downvotes_by_user(self) return self._relations["cd"]
def link_upvotes(self): from news.models.vote import LinkVote if "lu" not in self._relations: self._relations["lu"] = LinkVote.upvotes_by_user(self) return self._relations["lu"]