Exemple #1
0
 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)
Exemple #2
0
    def follow(self, related_type, relate_id):
        related_type = int(related_type)

        # 1. 首先需要登录
        if not self.current_user():
            return -1, '请先登录后再做此操作'

        # 2. 父级类型必须存在
        if related_type not in OBJECT_TYPES.values():
            return -2, '不合法的提交'

        # 3. relate_id 存在检查
        obj = OBJECT_TYPES.get_object(related_type, relate_id)
        if not obj:
            return -3, '目标不存在'

        # 4. 关注存在检查
        if Follow.exists(related_type, relate_id, self.current_user()):
            return -4, '已经完成过这个操作'

        # 5. 存入数据库
        Follow.new(related_type, relate_id, self.current_user())
        return 0, '成功'