from django.core.urlresolvers import reverse_lazy
from django.views import generic
from django.shortcuts import redirect, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from . import forms
from . import models
from custommixins import mixins


class OrderView(generic.View):
    template_name = 'orders/order_create.html'

    def get(self, request):
        qs = models.Product.objects.none()
        formset = forms.ProductFormSet(queryset=qs, prefix='formset')
        order_form = forms.OrderForm(prefix='order_form')
        return render(request, self.template_name, {'formset': formset, 'order_form': order_form})

    def post(self, request):
        formset = forms.ProductFormSet(request.POST, prefix='formset')
        order_form = forms.OrderForm(request.POST, prefix='order_form')
        if formset.is_valid():
            order = order_form.save()
            for form in formset.forms:
                product = form.save(commit=False)
                order.products.add(product)
                order.save()
            return HttpResponseRedirect(reverse('order_details', args=(order.id,)))
        else:
            return render(request, self.template_name, {'formset': formset, 'order_form': order_form})


class OrderDetails(generic.DetailView):
    model = models.Order
    template_name_suffix = '_details'


class OrderList(mixins.LoginRequiredMixin, mixins.AdminRequiredMixin, generic.ListView):
    model = models.Order


class OrderEdit(generic.View):
    template_name = 'orders/order_edit.html'

    def get(self, request, pk):
        order = models.Order.objects.get(pk=pk)

        formset = forms.ProductFormSet(queryset=order.products.all(), prefix='formset')

        order_form = forms.OrderForm(prefix='order_form', instance=order)
        return render(request, self.template_name, {'formset': formset, 'order_form': order_form})

    def post(self, request, pk):
        order = models.Order.objects.get(pk=pk)
        formset = forms.ProductFormSet(request.POST, prefix='formset')
        order_form = forms.OrderForm(request.POST, prefix='order_form')
        if formset.is_valid():
            order = order_form.save()
            for form in formset.forms:
                product = form.save(commit=False)
                order.products.add(product)
                order.save()
            return HttpResponseRedirect(reverse('order_details', args=(order.id,)))
        else:
            return render(request, self.template_name, {'formset': formset, 'order_form': order_form})

[((588, 675), 'django.shortcuts.render', 'render', (['request', 'self.template_name', "{'formset': formset, 'order_form': order_form}"], {}), "(request, self.template_name, {'formset': formset, 'order_form':\n    order_form})\n", (594, 675), False, 'from django.shortcuts import redirect, render\n'), ((1836, 1923), 'django.shortcuts.render', 'render', (['request', 'self.template_name', "{'formset': formset, 'order_form': order_form}"], {}), "(request, self.template_name, {'formset': formset, 'order_form':\n    order_form})\n", (1842, 1923), False, 'from django.shortcuts import redirect, render\n'), ((1193, 1280), 'django.shortcuts.render', 'render', (['request', 'self.template_name', "{'formset': formset, 'order_form': order_form}"], {}), "(request, self.template_name, {'formset': formset, 'order_form':\n    order_form})\n", (1199, 1280), False, 'from django.shortcuts import redirect, render\n'), ((2493, 2580), 'django.shortcuts.render', 'render', (['request', 'self.template_name', "{'formset': formset, 'order_form': order_form}"], {}), "(request, self.template_name, {'formset': formset, 'order_form':\n    order_form})\n", (2499, 2580), False, 'from django.shortcuts import redirect, render\n'), ((1116, 1158), 'django.core.urlresolvers.reverse', 'reverse', (['"""order_details"""'], {'args': '(order.id,)'}), "('order_details', args=(order.id,))\n", (1123, 1158), False, 'from django.core.urlresolvers import reverse\n'), ((2416, 2458), 'django.core.urlresolvers.reverse', 'reverse', (['"""order_details"""'], {'args': '(order.id,)'}), "('order_details', args=(order.id,))\n", (2423, 2458), False, 'from django.core.urlresolvers import reverse\n')]