From f52ddd1008fca7b23bd27e280ab154ef44a97db4 Mon Sep 17 00:00:00 2001 From: Sanj Date: Wed, 9 Nov 2011 21:04:53 +0530 Subject: [PATCH] printing --- printaform/formaprint/views.py | 36 +++++++++++++++++++++++++++++ printaform/templates/edit_item.html | 19 ++++++++++++++- printaform/urls.py | 3 +++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/printaform/formaprint/views.py b/printaform/formaprint/views.py index 17e143a..18e16b4 100644 --- a/printaform/formaprint/views.py +++ b/printaform/formaprint/views.py @@ -11,6 +11,15 @@ except: import simplejson as json from django.contrib.auth.decorators import login_required from ox.django.shortcuts import render_to_json_response +from copy import deepcopy +from os.path import join +from settings import MEDIA_ROOT +import os + +def index(request): + items = Item.objects.all() #FIXME: check perms, etc + context = RequestContext(request, {'items': items}) + return render_to_response("index.html", context) def new_item(request): if request.POST: @@ -71,8 +80,35 @@ def save_item(request): return render_to_json_response('ok') +@login_required +def save_item_as(request): + original_item_id = request.POST['item_id'] + original_item = get_object_or_404(Item, pk=original_item_id) + new_item = deepcopy(original_item) + new_item.id = None + new_item.title = request.POST['new_item_title'] + new_item.user = request.user + new_item.save() + return HttpResponse("/edit_item/?id=%d" % new_item.id) + + def render_item(request): id = request.GET.get("id", 0) item = get_object_or_404(Item, pk=id) return HttpResponse(item.render()) + +def print_item(request): + id = request.GET.get("id", 0) + item = get_object_or_404(Item, pk=id) + pdf_path = join(MEDIA_ROOT, "pdfs") + pdf_name = "%s.pdf" % id + pdf_output_path = join(pdf_path, pdf_name) + url = "http://localhost:8000/render_item/?id=%s" % id + cmd = "wkhtmltopdf %s %s" % (url, pdf_output_path,) + print cmd + os.system(cmd) + short_url = "/static/pdfs/%s.pdf" % id + return HttpResponseRedirect(short_url) + + diff --git a/printaform/templates/edit_item.html b/printaform/templates/edit_item.html index 113358f..7e86932 100644 --- a/printaform/templates/edit_item.html +++ b/printaform/templates/edit_item.html @@ -12,12 +12,29 @@ $(function() { }); }); + $('#saveAs').click(function(e) { + e.preventDefault(); + var title = prompt("Give new item a title:"); + var postData = $('#editForm').serializeArray(); + postData.push({ + 'name': 'new_item_title', + 'value': title + }); + $.post("/save_item_as/", postData, function(response) { + location.href = response; + }); + }); + }); +

+ Title: {{ item.title }}
+ Type: {{ item.typ.name }} +

{% csrf_token %} @@ -35,7 +52,7 @@ $(function() { {% endfor %}

- + View Get PDF

diff --git a/printaform/urls.py b/printaform/urls.py index 75b179d..3d9a745 100644 --- a/printaform/urls.py +++ b/printaform/urls.py @@ -8,10 +8,13 @@ admin.autodiscover() urlpatterns = patterns('', # Example: # (r'^printaform/', include('printaform.foo.urls')), + (r'^$', 'formaprint.views.index'), (r'^new_item/$', 'formaprint.views.new_item'), (r'^edit_item/$', 'formaprint.views.edit_item'), (r'^save_item/$', 'formaprint.views.save_item'), + (r'^save_item_as/$', 'formaprint.views.save_item_as'), (r'^render_item/$', 'formaprint.views.render_item'), + (r'^print_item/$', 'formaprint.views.print_item'), # Uncomment the admin/doc line below to enable admin documentation: # (r'^admin/doc/', include('django.contrib.admindocs.urls')),