Exemplo n.º 1
0
def greetings():
    if request.method == 'OPTIONS':
        return jsonify({'Methods': ['GET', 'POST']})

    elif request.method == 'POST':
        # create a new greeting
        if request.content_type != 'application/json':
            return jsonify({
                'Error':
                'Updates must be JSON documents (content-type: application/json)'
            }), 415

        try:
            greeting = Greeting.create_from_api(request.json)

        except APIInvalidData as e:
            return jsonify({'Error': e.message}), 400

        except APIAlreadyExists as e:
            return jsonify({'Error': e.message, 'GreetingId': e.id}), 409

        return jsonify({'GreetingId': greeting.id}), 201

    elif request.method == 'GET':
        # fetch all greetings
        return Greeting.all_to_json()
Exemplo n.º 2
0
def db(request):

    greeting = Greeting()
    greeting.save()

    greetings = Greeting.objects.all()

    return render(request, 'db.html', {'greetings': greetings})
Exemplo n.º 3
0
def greeting(greeting_id):
    greeting = Greeting.by_id(greeting_id)
    if not greeting:
        return jsonify({
            'Error':
            'Greeting with id {id} not found'.format(id=greeting_id)
        }), 404

    if request.method == 'OPTIONS':
        return jsonify({'Methods': ['GET', 'PUT']})

    elif request.method == 'GET':
        # fetch a single greeting
        return greeting.to_json()

    elif request.method == 'PUT':
        # update a single greeting
        if request.content_type != 'application/json':
            return jsonify({
                'Error':
                'Updates must be JSON documents (content-type: application/json)'
            }), 415

        if greeting.update_from_api(request.json):
            status = 'updated'
        else:
            status = 'unmodified'

        return greeting.to_json(status=status)

    elif request.method == 'DELETE':
        # delete a single greeting
        greeting.delete()
        return greeting.to_json(status='deleted')
Exemplo n.º 4
0
def db(request):
    greeting = Greeting()
    greeting.save()
    greetings = Greeting.objects.all()
    return render(request, "db.html", {"greetings": greetings})
    def create_greeting(self, request):
        g = Greeting(content=request.message)
        g.put()

        return GreetingResponse(greetingId=str(g.key()))