Beispiel #1
0
 def post(self, *args, **kwargs):
     json_data = get_cleaned_json_data(self, ['opt', 'data'])
     data = json_data['data']
     opt = json_data['opt']
     # 支持某个回复
     if opt == 'support-postreply':
         try:
             postreply = PostReply.get(PostReply.id == data['postreply'])
         except:
             self.write(json_result(1, '请输入正确的回复'))
             return
         postreply.up_like()
         self.write(json_result(0, 'success'))
     # 收藏该主题
     elif opt == 'collect-post':
         try:
             post = Post.get(Post.id == data['post'])
         except:
             self.write(json_result(1, '请输入正确的Post'))
             return
         CollectPost.create(post=post, user=self.current_user)
         self.write(json_result(0, 'success'))
     # 取消收藏该主题
     elif opt == 'cancle-collect-post':
         try:
             post = Post.get(Post.id == data['post'])
         except:
             self.write(json_result(1, 'CollectPost不正确'))
             return
         collectpost = CollectPost.get(post=post, user=self.current_user)
         collectpost.delete_instance()
         self.write(json_result(0, 'success'))
     else:
         self.write(json_result(1, 'opt不支持'))
Beispiel #2
0
    def post(self, *args, **kwargs):
        json_data = get_cleaned_json_data(self, ['opt', 'data'])
        data = json_data['data']
        opt = json_data['opt']

        # 关注用户
        if opt == 'get-post-list':
            data = []
            posts = Post.select()
            for p in posts:
                tp = obj2dict(p, ['id', 'title'])
                data.append(tp)
            self.write(json_result(0, data))
Beispiel #3
0
    def post(self, *args, **kwargs):
        json_data = get_cleaned_json_data(self, ['opt', 'data'])
        data = json_data['data']
        opt = json_data['opt']

        # 关注用户
        if opt == 'get-post-list':
            data = []
            posts = Post.select()
            for p in posts:
                tp = obj2dict(p, ['id', 'title'])
                data.append(tp)
            self.write(json_result(0, data))
Beispiel #4
0
 def post(self, *args, **kwargs):
     json_data = get_cleaned_json_data(self, ['opt', 'data'])
     data = json_data['data']
     opt = json_data['opt']
     # 获取文章详情
     if opt == 'get-post':
         try:
             post = BlogPost.get(BlogPost.id == int(data['post']),
                                 BlogPost.is_del == False)
         except:
             self.write(json_result(1, '不存在该post'))
             return
         else:
             self.write(
                 json_result(
                     0, {
                         'title': post.title,
                         'content': post.content,
                         'labels': BlogPostLabel.get_post_label(post),
                         'category': post.category.name
                     }))
             return
     # 更新文章
     elif opt == 'update-post':
         try:
             post = BlogPost.get(BlogPost.id == int(data['post']),
                                 BlogPost.is_del == False)
         except:
             self.write(json_result(1, '不存在该post'))
             return
         else:
             cate = BlogPostCategory.get_by_name(data['category'])
             post.category = cate
             post.title = data['title']
             post.content = data['content']
             post.save()
             BlogPostLabel.update_post_label(data['labels'], post)
             self.write(json_result(0, 'success'))
             return
     # 创建文章
     elif opt == 'create-post':
         cate = BlogPostCategory.get_by_name(data['category'])
         post = BlogPost.create(title=data['title'],
                                category=cate,
                                content=data['content'])
         BlogPostLabel.add_post_label(data['labels'], post)
         self.write(json_result(0, 'success'))
         return
     else:
         self.write(json_result(1, 'opt不支持'))
Beispiel #5
0
 def post(self, *args, **kwargs):
     json_data = get_cleaned_json_data(self, ['opt', 'data'])
     data = json_data['data']
     opt = json_data['opt']
     # 关注用户
     if opt == 'follow-user':
         try:
             user = User.get(User.id == data['user'])
         except:
             self.write(json_result(1, '没有该用户'))
             return
         Follower.create(user=user, follower=self.current_user)
         self.write(json_result(0, 'success'))
     # 取关用户
     elif opt == 'unfollow-user':
         try:
             user = User.get(User.id == data['user'])
         except:
             self.write(json_result(1, '没有该用户'))
             return
         try:
             f = Follower.get(Follower.user == user,
                              Follower.follower == self.current_user)
         except:
             self.write(json_result(1, '还没有关注他'))
             return
         f.delete_instance()
         self.write(json_result(0, 'success'))
     # 更新头像
     elif opt == 'update-avatar':
         import base64
         avatar = base64.b64decode(data['avatar'])
         user = self.current_user
         avatar_file_name = user.username + '.png'
         avatar_file = open(config.avatar_upload_path + avatar_file_name,
                            'wb')
         avatar_file.write(avatar)
         user.avatar = avatar_file_name
         user.save()
         self.write(json_result(0, 'success'))
     # 更新社区主题
     elif opt == 'update-theme':
         user = self.current_user
         user.theme = data['theme']
         user.save()
         self.write(json_result(0, 'success'))
     else:
         self.write(json_result(1, 'opt不支持'))
Beispiel #6
0
 def post(self, *args, **kwargs):
     json_data = get_cleaned_json_data(self, ['opt', 'data'])
     data = json_data['data']
     opt = json_data['opt']
     # 获取文章详情
     if opt == 'get-post':
         try:
             post = BlogPost.get(BlogPost.id == int(data['post']), BlogPost.is_del == False)
         except:
             self.write(json_result(1, '不存在该post'))
             return
         else:
             self.write(json_result(0, {'title': post.title,
                            'content': post.content,
                            'labels': BlogPostLabel.get_post_label(post),
                            'category': post.category.name}))
             return
     # 更新文章
     elif opt == 'update-post':
         try:
             post = BlogPost.get(BlogPost.id == int(data['post']), BlogPost.is_del == False)
         except:
             self.write(json_result(1, '不存在该post'))
             return
         else:
             cate = BlogPostCategory.get_by_name(data['category'])
             post.category = cate
             post.title = data['title']
             post.content = data['content']
             post.save()
             BlogPostLabel.update_post_label(data['labels'], post)
             self.write(json_result(0, 'success'))
             return
     # 创建文章
     elif opt == 'create-post':
         cate = BlogPostCategory.get_by_name(data['category'])
         post = BlogPost.create(title=data['title'],
                                category=cate,
                                content=data['content'])
         BlogPostLabel.add_post_label(data['labels'], post)
         self.write(json_result(0, 'success'))
         return
     else:
         self.write(json_result(1, 'opt不支持'))
Beispiel #7
0
    def post(self, *args, **kwargs):
        json_data = get_cleaned_json_data(self, ['opt', 'data'])
        data = json_data['data']
        opt = json_data['opt']

        # 关注用户
        if opt == 'follow-user':
            try:
                user = User.get(User.id == data['user'])
            except:
                self.write(json_result(1, '没有该用户'))
                return
            Follower.create(user=user, follower=self.current_user)
            self.write(json_result(0, 'success'))

        # 取关用户
        elif opt == 'unfollow-user':
            try:
                user = User.get(User.id == data['user'])
            except:
                self.write(json_result(1, '没有该用户'))
                return
            try:
                f = Follower.get(Follower.user == user, Follower.follower == self.current_user)
            except:
                self.write(json_result(1, '还没有关注他'))
                return
            f.delete_instance()
            self.write(json_result(0, 'success'))

        # 更新头像
        elif opt == 'update-avatar':
            import base64
            avatar = base64.b64decode(data['avatar'])
            user = self.current_user
            avatar_file_name = user.username + '.png'
            avatar_file = open(config.avatar_upload_path + avatar_file_name, 'wb')
            avatar_file.write(avatar)
            user.avatar = avatar_file_name
            user.save()
            self.write(json_result(0, 'success'))

        # 更新社区主题
        elif opt == 'update-theme':
            user = self.current_user
            user.theme = data['theme']
            user.save()
            self.write(json_result(0, 'success'))

        # 获取聊天记录
        elif opt == 'realtime-chat':
            user = self.current_user
            other_id = data['other']
            other = User.get(User.id == other_id)
            result = ChatMessage.get_recent_chat_message(user, other)
            self.write(json_result(0, result))

        # 发送消息
        elif opt == 'chat-to' :
            user = self.current_user
            other_id = data['other']
            other = User.get(User.id == other_id)
            content = data['content']
            ChatMessage.create(me=user, other=other, content=content)
            self.write(json_result(0, 'success'))
        else:
            self.write(json_result(1, 'opt不支持'))