# Path: riv/shortcuts.py
# def render_to_rest(data):
#     return RestResponse(content=data)
# 
# def render_form_error_to_rest(form):
#     return RestResponse(form=form)


from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, Http404
from django.template import RequestContext
from django.forms.models import modelformset_factory
from forms import PollForm, VoteForm
from models import Poll, Choice
from riv.shortcuts import render_to_rest, render_form_error_to_rest

def poll_delete(request, id):
    p = get_object_or_404(Poll, pk=id)
    p.delete()
    if request.is_rest():
        return HttpResponse(status=204)
    return render_to_response('deleted.html')

@login_required
def results(request, id):
    poll = get_object_or_404(Poll, pk=id)
    if request.is_rest():
        result = {'choice': []}
        for choice in poll.choice_set.all():
            result['choice'].append({'name': choice.choice, 'votes': choice.votes})
        return render_to_rest(result)
    return render_to_response('results.html', {
        'poll': poll,
        }, context_instance=RequestContext(request)
    )
    
def vote(request):
    if request.method == 'POST':
        form = VoteForm(request.POST)
        if form.is_valid():
            choice = form.cleaned_data.get('choice')
            choice.votes += 1
            choice.save()
            return render_to_rest({'poll': choice.poll.id, 'choice': choice.id})
        else:

# Path: riv/shortcuts.py
# def render_to_rest(data):
#     return RestResponse(content=data)
# 
# def render_form_error_to_rest(form):
#     return RestResponse(form=form)


# Path: tests/polls/views.py
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, Http404
from django.template import RequestContext
from django.forms.models import modelformset_factory
from forms import PollForm, VoteForm
from models import Poll, Choice
from riv.shortcuts import render_to_rest, render_form_error_to_rest


def poll_delete(request, id):
    p = get_object_or_404(Poll, pk=id)
    p.delete()
    if request.is_rest():
        return HttpResponse(status=204)
    return render_to_response('deleted.html')

@login_required
def results(request, id):
    poll = get_object_or_404(Poll, pk=id)
    if request.is_rest():
        result = {'choice': []}
        for choice in poll.choice_set.all():
            result['choice'].append({'name': choice.choice, 'votes': choice.votes})
        return render_to_rest(result)
    return render_to_response('results.html', {
        'poll': poll,
        }, context_instance=RequestContext(request)
    )
    
def vote(request):
    if request.method == 'POST':
        form = VoteForm(request.POST)
        if form.is_valid():
            choice = form.cleaned_data.get('choice')
            choice.votes += 1
            choice.save()
            return render_to_rest({'poll': choice.poll.id, 'choice': choice.id})
        else:

            return render_form_error_to_rest(form)