This commit is contained in:
Sanj 2011-11-09 21:04:53 +05:30
parent 84becd7537
commit f52ddd1008
3 changed files with 57 additions and 1 deletions

View File

@ -11,6 +11,15 @@ except:
import simplejson as json import simplejson as json
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from ox.django.shortcuts import render_to_json_response 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): def new_item(request):
if request.POST: if request.POST:
@ -71,8 +80,35 @@ def save_item(request):
return render_to_json_response('ok') 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): def render_item(request):
id = request.GET.get("id", 0) id = request.GET.get("id", 0)
item = get_object_or_404(Item, pk=id) item = get_object_or_404(Item, pk=id)
return HttpResponse(item.render()) 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)

View File

@ -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;
});
});
}); });
</script> </script>
</head> </head>
<body> <body>
<p>
Title: {{ item.title }} <br />
Type: {{ item.typ.name }}
</p>
<form action="." method="POST" id="editForm"> <form action="." method="POST" id="editForm">
{% csrf_token %} {% csrf_token %}
<input name="item_id" type="hidden" value="{{ item.id }}" /> <input name="item_id" type="hidden" value="{{ item.id }}" />
@ -35,7 +52,7 @@ $(function() {
{% endfor %} {% endfor %}
<p> <p>
<input type="submit" /> <input type="submit" value="Save" /> <button id="saveAs">Make Copy</button> <a href="/render_item/?id={{ item.id }}" target="_blank">View</a> <a href="/print_item/?id={{ item.id }}" target="_blank">Get PDF</a>
</p> </p>
</form> </form>
</body> </body>

View File

@ -8,10 +8,13 @@ admin.autodiscover()
urlpatterns = patterns('', urlpatterns = patterns('',
# Example: # Example:
# (r'^printaform/', include('printaform.foo.urls')), # (r'^printaform/', include('printaform.foo.urls')),
(r'^$', 'formaprint.views.index'),
(r'^new_item/$', 'formaprint.views.new_item'), (r'^new_item/$', 'formaprint.views.new_item'),
(r'^edit_item/$', 'formaprint.views.edit_item'), (r'^edit_item/$', 'formaprint.views.edit_item'),
(r'^save_item/$', 'formaprint.views.save_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'^render_item/$', 'formaprint.views.render_item'),
(r'^print_item/$', 'formaprint.views.print_item'),
# Uncomment the admin/doc line below to enable admin documentation: # Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')), # (r'^admin/doc/', include('django.contrib.admindocs.urls')),