|
@@ -1,10 +1,22 @@
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
from django.core.urlresolvers import reverse
|
|
|
+from django.http import HttpResponseBadRequest
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
|
|
from .models import Document, Service, ServiceUse, CostUse, GoodUse
|
|
|
|
|
|
|
|
|
+CONTENT_TYPES = {
|
|
|
+ 'html': 'text/html; charset=utf-8',
|
|
|
+ 'txt': 'text/plain; charset=utf-8',
|
|
|
+}
|
|
|
+
|
|
|
+def format_to_content_type(_format):
|
|
|
+ if _format not in ('txt', 'html'):
|
|
|
+ raise ValueError('Unknown format {}'.format(_format))
|
|
|
+ else:
|
|
|
+ return '{}; charset=utf-8'.format(CONTENT_TYPES[_format])
|
|
|
+
|
|
|
@login_required
|
|
|
def list_documents(request):
|
|
|
breadcrumbs = (
|
|
@@ -42,6 +54,11 @@ def detail_document(request, pk):
|
|
|
|
|
|
@login_required
|
|
|
def compare_document(request, pk, other_pk):
|
|
|
+ _format = request.GET.get('format', 'html')
|
|
|
+
|
|
|
+ if _format not in ('html', 'txt'):
|
|
|
+ return HttpResponseBadRequest('Wrong format : {}'.format(_format))
|
|
|
+
|
|
|
doc = Document.objects.get(pk=pk)
|
|
|
other_doc = Document.objects.get(pk=other_pk)
|
|
|
|
|
@@ -57,7 +74,13 @@ def compare_document(request, pk, other_pk):
|
|
|
'other_documents': Document.objects.exclude(pk=doc.pk),
|
|
|
'deltas': doc.compare(other_doc),
|
|
|
}
|
|
|
- return render(request, 'costs/document_compare.html', context)
|
|
|
+ response = render(
|
|
|
+ request,
|
|
|
+ 'costs/document_compare.{}'.format(_format),
|
|
|
+ context,
|
|
|
+ content_type=format_to_content_type(_format),
|
|
|
+ )
|
|
|
+ return response
|
|
|
|
|
|
|
|
|
@login_required
|