From 5ba01b62ea9d350639fe532654f1a43ca59ebca5 Mon Sep 17 00:00:00 2001 From: yomguy Date: Thu, 20 Dec 2012 11:05:44 +0100 Subject: [PATCH] add forms templates, first testimonial pdf view (no media) --- teleforma/templates/forms/form_detail.html | 17 +++ teleforma/templates/forms/form_sent.html | 17 +++ .../templates/forms/includes/built_form.html | 14 +++ teleforma/urls.py | 5 + teleforma/views/pro.py | 115 +++++++++++------- 5 files changed, 126 insertions(+), 42 deletions(-) create mode 100644 teleforma/templates/forms/form_detail.html create mode 100644 teleforma/templates/forms/form_sent.html create mode 100644 teleforma/templates/forms/includes/built_form.html diff --git a/teleforma/templates/forms/form_detail.html b/teleforma/templates/forms/form_detail.html new file mode 100644 index 00000000..3a160dfe --- /dev/null +++ b/teleforma/templates/forms/form_detail.html @@ -0,0 +1,17 @@ + +{% load forms_builder_tags %} + + + {{ form.title }} + + + + {% render_built_form form %} + diff --git a/teleforma/templates/forms/form_sent.html b/teleforma/templates/forms/form_sent.html new file mode 100644 index 00000000..847cadfd --- /dev/null +++ b/teleforma/templates/forms/form_sent.html @@ -0,0 +1,17 @@ + + + + {{ form.title }} + + + +

{{ form.title }}

+ {% if form.response %} +

{{ form.response }}

+ {% endif %} + diff --git a/teleforma/templates/forms/includes/built_form.html b/teleforma/templates/forms/includes/built_form.html new file mode 100644 index 00000000..1a36c608 --- /dev/null +++ b/teleforma/templates/forms/includes/built_form.html @@ -0,0 +1,14 @@ + {% if form.intro %} +

{{ form.intro }}

+ {% endif %} + +
+ {% csrf_token %} + {{ form_for_form.as_p }} + + + +
diff --git a/teleforma/urls.py b/teleforma/urls.py index 8c001f81..3ee3632c 100644 --- a/teleforma/urls.py +++ b/teleforma/urls.py @@ -47,6 +47,7 @@ user_export = UsersXLSExport() profile_view = ProfileView() document = DocumentView() media = MediaView() +testimonial = TestimonialView() urlpatterns = patterns('', # url(r'^$', HomeView.as_view(), name='teleforma-home'), @@ -103,6 +104,10 @@ urlpatterns = patterns('', url(r'^forms/', include('forms_builder.forms.urls')), url(r'^desk/seminars/(?P.*)/form/$', evaluation_form_detail, name="teleforma-seminar-form"), + # Testimonial + url(r'^desk/seminars/(?P.*)/testimonial/$', testimonial.download, + name="teleforma-seminar-testimonial"), + # Postman url(r'^messages/', include('postman.urls')), diff --git a/teleforma/views/pro.py b/teleforma/views/pro.py index e4a12804..81da0816 100644 --- a/teleforma/views/pro.py +++ b/teleforma/views/pro.py @@ -380,6 +380,7 @@ def evaluation_form_detail(request, pk, template='teleforma/evaluation_form.html else: entry = form_for_form.save() form_valid.send(sender=request, form=form_for_form, entry=entry) + messages.info(request, _("You have successfully sumitted your evaluation")) return redirect('teleforma-seminar-detail', seminar.id) context['seminar'] = seminar @@ -393,56 +394,86 @@ def evaluation_form_detail(request, pk, template='teleforma/evaluation_form.html # Testimonials -def fetch_resources(uri, rel): - """ - Callback to allow xhtml2pdf/reportlab to retrieve Images,Stylesheets, etc. - `uri` is the href attribute from the html link element. - `rel` gives a relative path, but it's not used here. +import StringIO - """ - if uri.startswith(settings.MEDIA_URL): - path = os.path.join(settings.MEDIA_ROOT, - uri.replace(settings.MEDIA_URL, "")) - elif uri.startswith(settings.STATIC_URL): - path = os.path.join(settings.STATIC_ROOT, - uri.replace(settings.STATIC_URL, "")) - else: - path = os.path.join(settings.STATIC_ROOT, - uri.replace(settings.STATIC_URL, "")) +from xhtml2pdf import pisa - if not os.path.isfile(path): - path = os.path.join(settings.MEDIA_ROOT, - uri.replace(settings.MEDIA_URL, "")) +from django.conf import settings +from django.http import HttpResponse +from django.shortcuts import render +from django.template.loader import get_template +from django.template.context import Context +from django.utils.html import escape +from django.views.generic.detail import SingleObjectMixin - if not os.path.isfile(path): - raise UnsupportedMediaPathException( - 'media urls must start with %s or %s' % ( - settings.MEDIA_ROOT, settings.STATIC_ROOT)) - return path +class TestimonialView(DetailView): + + model = Seminar + template_name = 'teleforma/seminar_detail.html' + mimetype = 'application/pdf' + extension = 'pdf' + + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(TestimonialView, self).dispatch(*args, **kwargs) + + def get_context_data(self, **kwargs): + context = super(TestimonialView, self).get_context_data(**kwargs) + context['seminar'] = self.get_object() + return context + + def download(self, request, pk): + """Function to render html template into a pdf file""" + + seminar = Seminar.objects.get(id=pk) + template = get_template(self.template_name) + + context = Context({'seminar': seminar, 'STATIC_URL': settings.STATIC_ROOT }) + html = template.render(context) + result = StringIO.StringIO() + + pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), + dest=result, + encoding='UTF-8', + link_callback=self.fetch_resources) + if not pdf.err: + response = HttpResponse(result.getvalue(), mimetype=self.mimetype) + return response + + return HttpResponse('We had some errors
%s
' % escape(html)) + + + -def render_to_pdf(template_src, context_dict): - """Function to render html template into a pdf file""" - template = get_template(template_src) - context = Context(context_dict) - html = template.render(context) - result = StringIO.StringIO() + def fetch_resources(self, uri, rel): + """ + Callback to allow xhtml2pdf/reportlab to retrieve Images,Stylesheets, etc. + `uri` is the href attribute from the html link element. + `rel` gives a relative path, but it's not used here. - pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), - dest=result, - encoding='UTF-8', - link_callback=fetch_resources) - if not pdf.err: - response = HttpResponse(result.getvalue(), - mimetype='application/pdf') + """ + if uri.startswith(settings.MEDIA_URL): + path = os.path.join(settings.MEDIA_ROOT, + uri.replace(settings.MEDIA_URL, "")) + elif uri.startswith(settings.STATIC_URL): + path = os.path.join(settings.STATIC_ROOT, + uri.replace(settings.STATIC_URL, "")) + else: + path = os.path.join(settings.STATIC_ROOT, + uri.replace(settings.STATIC_URL, "")) - return response + if not os.path.isfile(path): + path = os.path.join(settings.MEDIA_ROOT, + uri.replace(settings.MEDIA_URL, "")) + + if not os.path.isfile(path): + raise UnsupportedMediaPathException( + 'media urls must start with %s or %s' % ( + settings.MEDIA_ROOT, settings.STATIC_ROOT)) + + return path - return HttpResponse('We had some errors
%s
' % escape(html)) -def download_pdf(request): - """Build briefing packages format and export as HTML and PDF.""" - response = HttpResponse(content_type='application/pdf') - return generate_pdf('app/test.html', file_object=response) \ No newline at end of file -- 2.39.5