Exemplo n.º 1
0
    def post_favourites(user_id):
        # Get the user or return 404 not found
        user = User.query.get_or_404(user_id)
        # Get the data
        data = request.get_json()

        # Check that there is data
        if not data:
            abort(400)

        # Check that the data has the correct fields
        if not (('movie_tv_id' in data and isinstance(data['movie_tv_id'], int)
                 and data['movie_tv_id']) or
                ('media_type' in data and isinstance(data['media_type'], str)
                 and data['media_type'])):
            abort(400)

        print(data)
        try:
            favourite = Favourite(data['movie_tv_id'], data['media_type'])
            user.favourites.append(favourite)
            favourite.insert()
            return jsonify({
                "success": True,
                "status_code": 200,
                "favourite": favourite.format()
            })
        except Exception as e:
            print('Error ', e)
            abort(422)