class RatingEP(Resource): def __init__(self): self.rating_request = RatingRequest() self.rating_schema = RatingSchema() super(RatingEP, self).__init__() @Auth.authentication_required() def post(self): args = self.rating_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'] rating = post_object.ratings.filter(uid=uid) logger.debug(rating.count()) if rating.count() == 0: rating = post_object.ratings.create(uid=uid, pid=args.data['pid'], rating=args.data['rating']) else: rating.update(rating=args.data['rating']) rating.save() rating = rating[0] post_object.save() response_my_rating = self.rating_schema.dump(rating) response_post_rating = post_object.get_rating() return jsonify({"my_rating": response_my_rating.data, "post_rating": response_post_rating}) @Auth.authentication_required() def get(self): args = self.rating_request.load(request.args) 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'] rating = post_object.ratings.filter(uid=uid) logger.debug(rating.count()) if rating.count() == 0: rating = -1 else: rating = rating[0].rating return jsonify({'rating': rating})
def __init__(self): self.rating_request = RatingRequest() self.rating_schema = RatingSchema() super(RatingEP, self).__init__()