Exemple #1
0
def new(request):
    if request.method == 'GET':
        return HttpResponseRedirect(reverse('polls.views.index'))
    elif request.method == 'POST':
        response_data = {}
        try:
            json_data = json.loads(request.body)
            p = Poll()
            p.title = json_data["title"]
            p.put()
            for choice in json_data["choices"]:
                c = Choice(parent=p.key)
                c.poll = p.key
                c.choice = choice["choice"]
                c.votes = 0
                c.put()
            response_data['result'] = 'success'
            response_data['id'] = p.key.integer_id()
            response_data['title'] = json_data["title"]
            return HttpResponse(json.dumps(response_data), content_type="application/json")
        except:
            response_data['result'] = 'failed'
            response_data['message'] = 'You messed up'
            return HttpResponse(json.dumps(response_data), content_type="application/json")