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 check(feed, *args, **kwargs): from news.models.ban import Ban if (current_user.is_authenticated and Ban.by_user_and_feed(current_user, feed) is not None): abort(403) return func(feed, *args, **kwargs)
def subscribe(self, feed: "Feed"): """ Subscribe user to given feed :param feed: feed to subscribe :return: """ if self.feed_subs >= MAX_SUBSCRIPTIONS_FREE: return False # if user is banned from feed he can't subscribe if Ban.by_user_and_feed(self, feed) is not None: return False # save subscription db.table("feeds_users").insert(user_id=self.id, feed_id=feed.id) key = "subs:{}".format(self.id) ids = cache.get(key) or [] ids.append(feed.id) cache.set(key, ids) self.incr("feed_subs", 1) self.update_with_cache() # TODO DO IN QUEUE feed.incr("subscribers_count", 1) return True
def GET_feed_bans(feed): """ Get bans on given feed :param feed: feed :return: """ bans = Ban.where("feed_id", feed.id).get() return render_template("feed_bans.html", feed=feed, bans=bans, active_tab="bans")
def save_link(link): """ Save link to saved links :param link_slug: link slug :return: """ if Ban.by_user_and_feed(current_user, link.feed) is not None: abort(403) # save the link saved_link = SavedLink(user_id=current_user.id, link_id=link.id) saved_link.commit() return redirect(redirect_back(link.route))
def post_ban_user(feed, username): user = User.by_username(username) if user is None: abort(404) ban_form = BanForm() ban_form.fill(user) if ban_form.validate(): expire = datetime.utcnow() + timedelta(seconds=ban_form.get_duration()) ban = Ban( reason=ban_form.reason.data, user_id=ban_form.user_id.data, feed_id=feed.id, until=expire, ) ban.apply() return redirect(feed.route + "/bans") return render_template("ban_user.html", feed=feed, form=ban_form, user=user)
def ban_user(feed, username): user = User.by_username(username) if user is None: abort(404) if Ban.by_user_and_feed(user, feed) is not None: flash("This user is already baned", "info") return redirect(redirect_back(feed.route + "/admin/reports")) ban_form = BanForm() ban_form.fill(user) return render_template("ban_user.html", feed=feed, form=ban_form, user=user)
def link_report(link): """ Report given link for breaking the rules :param feed: feed :param link_slug: link slug :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) return render_template("report.html", thing=link, feed=link.feed, report_form=ReportForm())
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 get_link(link, link_slug=""): """ Default view page for link :param link_slug: link slug :return: """ if (current_user.is_authenticated and Ban.by_user_and_feed(current_user, link.feed) is not None): abort(403) # Currently supports only one type of sorting for comments sorted_comments = SortedComments(link.id).get_full_tree() return render_template( "link.html", link=link, feed=link.feed, comment_form=CommentForm(), comments=sorted_comments, )
def comment_link(link): """ Handle comment on link :param feed: :param link_slug: :return: """ if Ban.by_user_and_feed(current_user, link.feed) is not None: abort(403) if link.archived: abort(405) comment_form = CommentForm() if comment_form.validate(): comment = comment_form.result() comment.user_id = current_user.id comment.link_id = link.id comment.commit() return redirect(link.route)
def post_link_report(link): """ Handle the link report Redirect to link on successful report Show the form if there are some mistakes :param feed: feed :param link_slug: link slug :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) report_form = ReportForm() if report_form.validate(): report = Report( reason=report_form.reason.data, comment=report_form.comment.data, user_id=current_user.id, feed_id=link.feed.id, ) link.report(report) flash("Thanks for your feedback!") return redirect( link.route ) # todo safe users origin through the form to redirect him back return render_template("report.html", thing=link, feed=link.feed, report_form=report_form)
def is_baned_from(self, feed: "Feed") -> bool: return Ban.by_user_and_feed(self, feed) is not None