def post_stats_do_comment(related_type, related_id, comment_id): # 需要同时更新被评论对象的数字和最后评论id def func(update, where): update['last_comment_id'] = comment_id post_stats_incr(PostStats.comment_count, related_id, 1, cb=func) # 如果被评论的是文章,需要更新板块数据 if related_type == POST_TYPES.TOPIC: t = Topic.get_by_pk(related_id) post_stats_incr(PostStats.comment_count, t.board_id, 1, cb=func)
def get(self, topic_id): topic = Topic.get_by_pk(topic_id) if not self.topic_check(topic): return self.render( "forum/topic_edit.html", nav="index", page_title=page_title("编辑主题 - %s" % topic.title, topic.board.title, "社区"), topic=topic, boards=Board.get_list(), )
def get_object(cls, related_type, related_id): from model.user import User from model.topic import Topic from model.wiki import WikiItem if related_type == OBJECT_TYPES.USER: u = User.get_by_pk(related_id) if u: return u elif related_type == OBJECT_TYPES.TOPIC: t = Topic.get_by_pk(related_id) if t: return t elif related_type == OBJECT_TYPES.WIKI: w = WikiItem.get_by_pk(related_id) if w: return w
def post(self, topic_id): topic = Topic.get_by_pk(topic_id) if not self.topic_check(topic): return title = self.get_argument("title", "").strip() content = self.get_argument("content", "").strip() if title and config.TITLE_LENGTH_MIN <= len(title) <= config.TITLE_LENGTH_MAX: topic.edit({"title": title, "content": content}, self.current_user()) self.messages.success("编辑成功") self.redirect(url_for("topic", topic.id)) else: # 非标准提交,不用过于客气 self.redirect(url_for("topic_new"))
def statistic_add_comment(related_type, related_id, comment_id): # 关于原子更新 # http://docs.peewee-orm.com/en/latest/peewee/querying.html#atomic-updates # s: Statistic = cls.get_by_pk(related_id) Statistic.update(last_comment_id=comment_id, comment_count=Statistic.comment_count + 1)\ .where(Statistic.id == related_id)\ .execute() Statistic24h.update(comment_count=Statistic24h.comment_count + 1)\ .where(Statistic24h.id == related_id)\ .execute() if related_type == POST_TYPES.TOPIC: t = Topic.get_by_pk(related_id) Statistic.update(last_comment_id=comment_id, comment_count=Statistic.comment_count + 1)\ .where(Statistic.id == t.board_id)\ .execute() Statistic24h.update(comment_count=Statistic24h.comment_count + 1)\ .where(Statistic24h.id == t.board_id)\ .execute()
def get(self, topic_id): topic = Topic.get_by_pk(topic_id) if topic: topic.view_count_inc() count, user_topics = Topic.get_list_by_user(topic.user) user_topics = user_topics.limit(10) follow = JsDict() if self.current_user(): follow.topic = Follow.exists(OBJECT_TYPES.TOPIC, topic_id, self.current_user()) follow.author = Follow.exists(OBJECT_TYPES.USER, topic.user.id, self.current_user()) self.render( "forum/topic.html", nav="index", page_title=page_title(topic.title, topic.board.title, "社区"), topic=topic, user_topics=user_topics, follow=follow, ) else: self.write_error(404)
def statistic_add_topic_click(topic_id, board_id=None): Statistic.update(click_count=Statistic.click_count + 1)\ .where(Statistic.id == topic_id)\ .execute() Statistic24h.update(click_count=Statistic24h.click_count + 1)\ .where(Statistic24h.id == topic_id)\ .execute() if not board_id: t = Topic.get_by_pk(topic_id) board_id = t.board_id Statistic.update(click_count=Statistic.click_count + 1)\ .where(Statistic.id == board_id)\ .execute() Statistic24h.update(click_count=Statistic24h.click_count + 1)\ .where(Statistic24h.id == board_id)\ .execute()
def get_post(cls, related_type, related_id): from model.user import User from model.topic import Topic from model.wiki import WikiItem if type(related_id) == POST_ID_GENERATOR: related_id = related_id.to_bin() if type(related_type) == str: related_type = int(related_type) if related_type == POST_TYPES.USER: u = User.get_by_pk(related_id) if u: return u elif related_type == POST_TYPES.TOPIC: t = Topic.get_by_pk(related_id) if t: return t elif related_type == POST_TYPES.WIKI: w = WikiItem.get_by_pk(related_id) if w: return w
def post_stats_add_topic_click(topic_id, board_id=None): if not board_id: t = Topic.get_by_pk(topic_id) board_id = t.board_id post_stats_incr(PostStats.click_count, topic_id) post_stats_incr(PostStats.click_count, board_id)