Example #1
0
class CommentEP(Resource):
    def __init__(self):
        self.comment_request = CommentRequest()
        self.comment_schema = CommentSchema(many=False)
        super(CommentEP, self).__init__()

    @Auth.authentication_required()
    def post(self):
        args = self.comment_request.load(request.get_json())
        if args.errors:
            raise exceptions.InvalidRequest(args.errors)
        try:
            post_object = Post.objects.get(pid=args.data['pid'])
        except Post.DoesNotExist:
            raise exceptions.PostDoesNotExist("Post Does Not Exist")
        uid = request.headers['UID']
        try:
            user_object = User.objects.get(uid=uid)
        except User.DoesNotExist:
            raise exceptions.UserDoesNotExist("User Does Not Exist")
        comment = post_object.comments.create(user=user_object, pid=args.data['pid'], comment=args.data['comment'])
        post_object.save()
        response_comment = self.comment_schema.dump(comment)
        return jsonify({"comment": response_comment.data})
Example #2
0
 def __init__(self):
     self.comment_request = CommentRequest()
     self.comment_schema = CommentSchema(many=False)
     super(CommentEP, self).__init__()