# 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_index(request):
    poll_list = Poll.objects.all().order_by('-pub_date')
    if request.is_rest():
        return render_to_rest(poll_list)
    return render_to_response('index.html',  {
        'poll_list': poll_list,
    })

def poll_detail(request, id):
    p = get_object_or_404(Poll, pk=id)
    if request.is_rest():
        return render_to_rest(p)
    return render_to_response('detail.html', {'poll': p})

def poll_create_or_update(request, id=None):
    if id:
        p = get_object_or_404(Poll, pk=id)
    if request.method == 'POST':
        if id:
            form = PollForm(request.POST, instance=p)
        else:
            form = PollForm(request.POST)
        if form.is_valid():
            obj = form.save()
            return render_to_rest(obj)
        else:
            if request.is_rest():

# 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_index(request):
    poll_list = Poll.objects.all().order_by('-pub_date')
    if request.is_rest():
        return render_to_rest(poll_list)
    return render_to_response('index.html',  {
        'poll_list': poll_list,
    })

def poll_detail(request, id):
    p = get_object_or_404(Poll, pk=id)
    if request.is_rest():
        return render_to_rest(p)
    return render_to_response('detail.html', {'poll': p})

def poll_create_or_update(request, id=None):
    if id:
        p = get_object_or_404(Poll, pk=id)
    if request.method == 'POST':
        if id:
            form = PollForm(request.POST, instance=p)
        else:
            form = PollForm(request.POST)
        if form.is_valid():
            obj = form.save()
            return render_to_rest(obj)
        else:
            if request.is_rest():

                return render_form_error_to_rest(form)