1 from django.shortcuts import render_to_response, get_object_or_404
2 from django.contrib.auth.decorators import login_required, permission_required
3 from django.http import HttpResponse
4 from django.forms.models import inlineformset_factory
5 from django.template import RequestContext
6 from django.views.generic.create_update import get_model_and_form_class, apply_extra_context, redirect, update_object, lookup_object, delete_object
7 from django.views.generic import UpdateView, DeleteView, CreateView, DetailView
8 from django.utils.decorators import method_decorator
9 from django.contrib import messages
11 from extra_views import CreateWithInlinesView, UpdateWithInlinesView, InlineFormSet
12 from extra_views.generic import GenericInlineFormSet
14 from invoice.models import Client, Invoice, InvoiceLine, status_text
15 from invoice.forms import ClientForm, InvoiceForm, InvoiceUpdateForm
19 clients = Client.objects.all()
20 new_invoices = Invoice.objects.filter(status = 'N')
21 sent_invoices = Invoice.objects.filter(status = 'S')
22 return render_to_response('invoice/index.html', locals())
24 # ------------------- CLIENT ---------------
26 class DetailClient(DetailView):
28 @method_decorator(login_required)
29 def dispatch(self, *args, **kwargs):
30 return super(DetailView, self).dispatch(*args, **kwargs)
32 class CreateClient(CreateView):
34 form_class = ClientForm
37 @method_decorator(login_required)
38 def dispatch(self, *args, **kwargs):
39 return super(CreateClient, self).dispatch(*args, **kwargs)
41 class UpdateClient(UpdateView):
43 form_class = ClientForm
46 @method_decorator(login_required)
47 def dispatch(self, *args, **kwargs):
48 return super(UpdateClient, self).dispatch(*args, **kwargs)
50 class DeleteClient(DeleteView):
52 form_class = ClientForm
55 @method_decorator(login_required)
56 def dispatch(self, *args, **kwargs):
57 return super(DeleteClient, self).dispatch(*args, **kwargs)
59 # ------------------- INVOICE ---------------
61 class InvoiceLineInline(InlineFormSet):
64 class DetailInvoice(DetailView):
67 def get_context_data(self, *args, **kwargs):
68 context_data = super(DetailInvoice, self).get_context_data(*args, **kwargs)
70 if self.object.due_days < 0 and self.object.status != 'P':
71 context_data.update({'alert_error': "This invoice is overdue" })
72 elif self.object.due_days < 3 and self.object.status != 'P':
73 context_data.update({'alert_warning': "This invoice is soon due"})
75 if self.object.status == 'N':
76 context_data.update({'nextstate': "S",
77 'nextstate_str': "sent"})
78 elif self.object.status == 'S':
79 context_data.update({'nextstate': "P",
80 'nextstate_str': "paid"})
86 @method_decorator(login_required)
87 def dispatch(self, *args, **kwargs):
88 return super(DetailInvoice, self).dispatch(*args, **kwargs)
90 class DetailInvoicePrint(DetailInvoice):
91 template_name = 'invoice/invoice_detail_print.html'
93 class CreateInvoice(CreateWithInlinesView):
95 inlines = [InvoiceLineInline]
96 form_class = InvoiceForm
99 @method_decorator(login_required)
100 def dispatch(self, *args, **kwargs):
101 return super(CreateInvoice, self).dispatch(*args, **kwargs)
103 class UpdateInvoice(UpdateView):
105 form_class = InvoiceUpdateForm
107 @method_decorator(login_required)
108 def dispatch(self, *args, **kwargs):
109 return super(UpdateInvoice, self).dispatch(*args, **kwargs)
111 def form_valid(self, form):
112 messages.info(self.request, 'Status set to %s.' % (status_text(form.data['status']), ))
113 return super(UpdateInvoice, self).form_valid(form)
116 # def get_initial(self):
117 # initial = super(CreateInvoice, self).get_initial().copy()
118 # if self.request.path