From: Guillaume Pellerin Date: Wed, 3 Feb 2016 22:39:03 +0000 (+0100) Subject: add complete registration with PDF download X-Git-Tag: 1.1~146 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=13cbf4a2818cc214ff043f10564375d0050ae9fe;p=teleforma.git add complete registration with PDF download --- diff --git a/teleforma/forms.py b/teleforma/forms.py index 1f90cdd6..8dae5586 100644 --- a/teleforma/forms.py +++ b/teleforma/forms.py @@ -64,4 +64,4 @@ class StudentInline(InlineFormSet): model = Student can_delete = False fields = ['level', 'iej', 'period', 'training', 'platform_only', 'procedure', - 'written_speciality', 'oral_speciality', 'oral_1', 'oral_2'] + 'written_speciality', 'oral_speciality', 'oral_1', 'oral_2', 'promo_code'] diff --git a/teleforma/models/crfpa.py b/teleforma/models/crfpa.py index 923a697c..e01f1467 100644 --- a/teleforma/models/crfpa.py +++ b/teleforma/models/crfpa.py @@ -145,6 +145,7 @@ class Student(Model): default_application_fees = 40 subscription_fees = models.FloatField(_('subscription fees'), help_text='€', blank=True, null=True) promo_code = models.CharField(_('promo code'), blank=True, max_length=100) + date_registered = models.DateTimeField(_('registration date'), auto_now_add=True, null=True, blank=True) date_subscribed = models.DateTimeField(_('subscription date'), null=True, blank=True) is_subscribed = models.BooleanField(_('subscribed')) confirmation_sent = models.BooleanField(_('confirmation sent')) diff --git a/teleforma/static/teleforma/css/teleforma_pdf.css b/teleforma/static/teleforma/css/teleforma_pdf.css new file mode 100644 index 00000000..43939aac --- /dev/null +++ b/teleforma/static/teleforma/css/teleforma_pdf.css @@ -0,0 +1,49 @@ +@page { + margin: 1cm; + margin-bottom: 2.5cm; + @frame footer { + -pdf-frame-content: footer; + bottom: 1cm; + margin-left: 1cm; + margin-right: 1cm; + height: 1cm; + } +} + +#header{ + font-size: 4em; + color: white; + padding-top: 2em; + padding-left: 1em; + background-color: #3a69b1; +} + +#content { + margin: 50px; + +} + +.table1 { + font-size: 1.5em; + padding-top: 2px; +} + + +.table2 { + margin: 0px; + font-size: 1em; + padding: 0px; +} + +#footer{ + font-size: 1em; + color: white; + padding: 1em; + background-color: #3a69b1; + text-align: center; +} + +.bold { + font-weight: bold; + vertical-align: top; +} diff --git a/teleforma/templates/registration/registration_complete.html b/teleforma/templates/registration/registration_complete.html index 5282dc7e..c3c2ca71 100644 --- a/teleforma/templates/registration/registration_complete.html +++ b/teleforma/templates/registration/registration_complete.html @@ -20,7 +20,7 @@ Service inscription
75006 Paris


- {% trans "Download" %} + {% trans "Download" %} diff --git a/teleforma/templates/registration/registration_form.html b/teleforma/templates/registration/registration_form.html index 8bd656e1..0a27547f 100644 --- a/teleforma/templates/registration/registration_form.html +++ b/teleforma/templates/registration/registration_form.html @@ -3,7 +3,7 @@ {% load telemeta_utils %} {% load teleforma_tags %} -{% block title %}
{% trans "Registration" %} - {% description %}
{% endblock %} +{% block title %}
{% trans "Pre-registration" %} - {% description %}
{% endblock %} {% block content %}
diff --git a/teleforma/templates/registration/registration_pdf.html b/teleforma/templates/registration/registration_pdf.html new file mode 100644 index 00000000..8d47c789 --- /dev/null +++ b/teleforma/templates/registration/registration_pdf.html @@ -0,0 +1,65 @@ + +{% load i18n %} +{% load telemeta_utils %} +{% load teleforma_tags %} + +{% get_current_language as LANGUAGE_CODE %} +{% get_available_languages as LANGUAGES %} + + + + + +{%block head_title %}{% description %} - {% trans "Registration" %}{% endblock %} + +{% block stylesheets %} + +{% endblock %} + +{% block extra_stylesheets %}{% endblock %} + + + + +{% block layout %} +
+ + + +
+ + + + + + + + +
{% trans "Last name" %} : {{ student.user.last_name|upper }}
{% trans "First name" %} : {{ student.user.first_name|upper }}
{% trans "Address" %} : {{ student.user.profile.all.0.address }} {{ student.user.profile.all.0.postal_code }} {{ student.user.profile.all.0.city }}
{% trans "Training" %} : {{ student.training.name }}
{% trans "Training type" %} : {% if student.platform_only %}E-learning{% else %}Présentielle et E-learning{% endif %}
{% trans "Registration date" %} : {{ student.date_registered}}
+ + + + +
+ Je certifie avoir pris connaissance des conditions générales d’inscription et du règlement intérieur. + + A ...................................., le ....................................... +
+ Signature +
+ +
+ +{% block footer %} + +{% endblock %} + +
+{% endblock layout %} + + + diff --git a/teleforma/urls.py b/teleforma/urls.py index 1a311d9f..1e1c1cde 100644 --- a/teleforma/urls.py +++ b/teleforma/urls.py @@ -58,6 +58,9 @@ urlpatterns = patterns('', # (r'^accounts/register0/$', RegistrationView.as_view(), {'form_class':CustomRegistrationForm}), url(r'^accounts/register/$', UserAddView.as_view(), name="teleforma-register"), url(r'^accounts/complete/$', UserCompleteView.as_view(), name="teleforma-register-complete"), + url(r'^accounts/register/(?P.*)/download/$', RegistrationPDFViewDowload.as_view(), + name="teleforma-registration-download"), + url(r'^captcha/', include('captcha.urls')), # Help diff --git a/teleforma/views/core.py b/teleforma/views/core.py index becda0e0..c31b6571 100644 --- a/teleforma/views/core.py +++ b/teleforma/views/core.py @@ -75,6 +75,8 @@ from telemeta.views import * import jqchat.models from xlwt import Workbook +from xhtml2pdf import pisa + try: from telecaster.models import * from telecaster.tools import * @@ -723,3 +725,93 @@ class HelpView(TemplateView): def dispatch(self, *args, **kwargs): return super(HelpView, self).dispatch(*args, **kwargs) + + +class PDFTemplateResponseMixin(TemplateResponseMixin): + """ + Mixin for Django class based views. + Switch normal and pdf template based on request. + + The switch is made when the request has a particular querydict, e.g.:: + + http://www.example.com?format=pdf + + The key and value of the querydict can be overridable using *as_view()*. + That pdf url will be present in the context as *pdf_url*. + + For example it is possible to define a view like this:: + + from django.views.generic import View + + class MyView(PDFTemplateResponseMixin, View): + template_name = 'myapp/myview.html' + pdf_filename = 'report.pdf' + + The pdf generation is automatically done by *xhtml2pdf* using + the *myapp/myview_pdf.html* template. + + Note that the pdf template takes the same context as the normal template. + """ + pdf_template_name = None + pdf_template_name_suffix = '_pdf' + pdf_querydict_key = 'format' + pdf_querydict_value = 'pdf' + pdf_encoding = 'utf-8' + pdf_filename = None + pdf_url_varname = 'pdf_url' + pdf_kwargs = {} + + def is_pdf(self): + value = self.request.REQUEST.get(self.pdf_querydict_key, '') + return value.lower() == self.pdf_querydict_value.lower() + + def _get_pdf_template_name(self, name): + base, ext = os.path.splitext(name) + return '%s%s%s' % (base, self.pdf_template_name_suffix, ext) + + def get_pdf_template_names(self): + """ + If the template name is not given using the class attribute + *pdf_template_name*, then it is obtained using normal template + names, appending *pdf_template_name_suffix*, e.g.:: + + path/to/detail.html -> path/to/detail_pdf.html + """ + if self.pdf_template_name is None: + names = super(PDFTemplateResponseMixin, self).get_template_names() + return map(self._get_pdf_template_name, names) + return [self.pdf_template_name] + + def get_pdf_filename(self): + """ + Return the pdf attachment filename. + If the filename is None, the pdf will not be an attachment. + """ + return self.pdf_filename + + def get_pdf_url(self): + """ + This method is used to put the pdf url in the context. + """ + querydict = self.request.GET.copy() + querydict[self.pdf_querydict_key] = self.pdf_querydict_value + return '%s?%s' % (self.request.path, querydict.urlencode()) + + def get_pdf_response(self, context, **response_kwargs): + return render_to_pdf( + request=self.request, + template=self.get_pdf_template_names(), + context=context, + encoding=self.pdf_encoding, + filename=self.get_pdf_filename(), + **self.pdf_kwargs + ) + + def render_to_response(self, context, **response_kwargs): + if self.is_pdf(): + from django.conf import settings + context['STATIC_ROOT'] = settings.STATIC_ROOT + return self.get_pdf_response(context, **response_kwargs) + context[self.pdf_url_varname] = self.get_pdf_url() + return super(PDFTemplateResponseMixin, self).render_to_response( + context, **response_kwargs) diff --git a/teleforma/views/crfpa.py b/teleforma/views/crfpa.py index 9a89fe9f..a2b25f3e 100644 --- a/teleforma/views/crfpa.py +++ b/teleforma/views/crfpa.py @@ -405,7 +405,7 @@ class UserAddView(CreateWithInlinesView): def forms_valid(self, form, inlines): messages.info(self.request, _("You have successfully register your account.")) - user = form.save() + self.user = form.save() user.username = get_unique_username(user.first_name, user.last_name) user.is_active = False user.save() @@ -418,5 +418,35 @@ class UserCompleteView(TemplateView): def get_context_data(self, **kwargs): context = super(UserCompleteView, self).get_context_data(**kwargs) - context['register_doc_print'] = Document.objects.get(id=settings.TELEFORMA_REGISTER_DEFAULT_DOC_ID) + # context['register_doc_print'] = Document.objects.get(id=settings.TELEFORMA_REGISTER_DEFAULT_DOC_ID) + context['username'] = kwargs['username'] return context + + +class RegistrationPDFView(PDFTemplateResponseMixin, DetailView): + + context_object_name = "student" + model = Student + template_name = 'teleforma/registration_pdf.html' + pdf_template_name = template_name + + def get_context_data(self, **kwargs): + student = self.get_object() + if student.training and not student.trainings: + student.trainings.add(student.training) + student.save() + context = super RegistrationPDFView, self).get_context_data(**kwargs) + return context + + +class RegistrationPDFViewDowload(RegistrationPDFView): + + pdf_filename = 'registration.pdf' + + def get_pdf_filename(self): + super(RegistrationPDFViewDowload, self).get_pdf_filename() + student = self.get_object() + prefix = unicode(_('Registration')) + filename = '_'.join([prefix, student.first_name, student.user.last_name]) + filename += '.pdf' + return filename.encode('utf-8')