Exemplo n.º 1
0
def movies():
    # We can ask the current request what method it is. If it is a get
    # return json respond with list of all movies.
    if request.method == "GET":
        # We create a list of dictionaries for all movies. Here we are
        # using the list comprehensions feature in Python
        return Response(json.dumps([x.to_dict() for x in movies_db]), mimetype="application/json")
    # If it is not a get method, it must be a post method (where we only allow
    # post and get). We use json to change the request data, on json format, to
    # dictionary where we can fetch the details of the movie being posted. We
    # create a new Movie instance and add it to the movie store.
    else:
        data = json.loads(request.data)
        movie = Movie(data.get("name"), data.get("year"))
        movies_db.append(movie)
        return "Ok", 200
Exemplo n.º 2
0
def movies():
    # We can ask the current request what method it is. If it is a get
    # return json respond with list of all movies.
    if request.method == 'GET':
        # We create a list of dictionaries for all movies. Here we are
        # using the list comprehensions feature in Python
        return Response(json.dumps([x.to_dict() for x in movies_db]),
                        mimetype='application/json')
    # If it is not a get method, it must be a post method (where we only allow
    # post and get). We use json to change the request data, on json format, to
    # dictionary where we can fetch the details of the movie being posted. We
    # create a new Movie instance and add it to the movie store.
    else:
        data = json.loads(request.data)
        movie = Movie(data.get('name'), data.get('year'))
        movies_db.append(movie)
        return 'Ok', 200
Exemplo n.º 3
0
 def post(self):
     data = json.loads(request.data)
     movie = Movie(data.get('name'), data.get('year'))
     movies_db.append(movie)
     return 'ok', 200