Exemplo n.º 1
0
    def post(self):
        try:
            user_id = get_jwt_identity()
            body = request.get_json()
            search_all = Search.objects(owner=user_id).first()
            if search_all == None:
                search_all = Search(owner=user_id).save()
            Search.objects(owner=user_id).update_one(
                push__history_search=body["keyword"])
            users = json.loads(
                User.objects(username__icontains=body["keyword"]).to_json())
            posts = json.loads(
                Post.objects(described__icontains=body["keyword"]).to_json())
            for index, post in enumerate(posts):
                like = Like.objects.get(post=post["id"])
                is_liked = like.is_liked(user_id)
                posts[index]["is_liked"] = is_liked
            data = []

            for index, user in enumerate(users):
                if str(user["id"]) == str(user_id):
                    del users[index]
                items = {}
                items["username"] = user["username"]
                items["id"] = user["id"]
                friend = Friend.objects.get(owner=user["id"])
                if friend.is_friend(user_id):
                    items["is_friend"] = True
                else:
                    items["is_friend"] = False
                data.append(items)

            self.res["user"] = data
            self.res["posts"] = posts[::-1]
        except DoesNotExist:
            self.res = response.user_is_invalid
        except Exception:
            self.res = response.internal_server()
            raise Exception
        return jsonify(self.res)
Exemplo n.º 2
0
    def get(self, id):
        try:
            user_id = get_jwt_identity()
            like = Like.objects.get(post=id)
            for userEmbed in like.user_like:
                if str(userEmbed["user"]) == user_id:
                    self.check_user = True
                    like.update(pull__user_like=userEmbed)
            
            if not self.check_user:
                raise Exception

            post = Post.objects(id=id).first()
            if post.like < 0:
                raise Exception
            post.update(dec__like=1)
            self.res = resCon.like_convert(post)
        except DoesNotExist:
            self.res = response.post_is_not_exit()
        except Exception:
            self.res = response.internal_server()
        return jsonify(self.res)