Ejemplo n.º 1
0
 def __init__(self):
     self.create_parser = RequestParser()
     self.create_parser.add_argument(
         'title', type=str, required=True, location='json', help='Missing title')
     self.create_parser.add_argument(
         'rating', type=int, required=True, location='json', help='Missing rating')
     self.create_parser.add_argument(
         'content', type=str, required=True, location='json', help='Missing content')
     self.rating_repository = RatingRepository()
     self.plan_repository = PlanRepository()
     super(RatingResource, self).__init__()
Ejemplo n.º 2
0
class RatingResource(Resource):
    def __init__(self):
        self.create_parser = RequestParser()
        self.create_parser.add_argument(
            'title', type=str, required=True, location='json', help='Missing title')
        self.create_parser.add_argument(
            'rating', type=int, required=True, location='json', help='Missing rating')
        self.create_parser.add_argument(
            'content', type=str, required=True, location='json', help='Missing content')
        self.rating_repository = RatingRepository()
        self.plan_repository = PlanRepository()
        super(RatingResource, self).__init__()

    @authenticate
    def post(self, plan_id, **kwargs):
        user = kwargs['user']
        args = self.create_parser.parse_args()
        plan = self.plan_repository.find(plan_id)

        if not plan.public:
            abort(404, message="No plan with this id was found")

        rating = Rating(
            title=args['title'],
            rating=args['rating'],
            content=args['content'],
            user=user,
            plan=plan
        )

        result = self.rating_repository.add_or_update(rating)
        if not result.success():
            on_error(error_message="Could not save rating", result=result)

        self.rating_repository.save_changes()
        return rating

    @authenticate
    def get(self, plan_id, **kwargs):
        ratings = self.rating_repository.get(plan_id=plan_id)
        return ratings