]> git.parisson.com Git - teleforma.git/commitdiff
add complete registration with PDF download
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Wed, 3 Feb 2016 22:39:03 +0000 (23:39 +0100)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Wed, 3 Feb 2016 22:39:03 +0000 (23:39 +0100)
teleforma/forms.py
teleforma/models/crfpa.py
teleforma/static/teleforma/css/teleforma_pdf.css [new file with mode: 0644]
teleforma/templates/registration/registration_complete.html
teleforma/templates/registration/registration_form.html
teleforma/templates/registration/registration_pdf.html [new file with mode: 0644]
teleforma/urls.py
teleforma/views/core.py
teleforma/views/crfpa.py

index 1f90cdd6d0d772b6c4e9aa1cea2b02a2463e7509..8dae558651124de222fc0b7e9a9d85250f12ab91 100644 (file)
@@ -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']
index 923a697ca80c4aacb1057cf8e30e734f018e499f..e01f1467faf0e5492a92fa6b52a97404da5405a3 100644 (file)
@@ -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 (file)
index 0000000..43939aa
--- /dev/null
@@ -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;
+}
index 5282dc7ef2f4e53ee7cbe91d58d82a2570f56985..c3c2ca7113ee82b23180ca9c1f94731403528d46 100644 (file)
@@ -20,7 +20,7 @@ Service inscription<br>
 75006 Paris
 <br><br><br>
 
-<a href="{{ register_doc_print.file.url }}" class="component_icon button" id="action_green" target="_blank"><img src="{{STATIC_URL}}telemeta/images/download.png" alt="" style="vertical-align:middle" />&nbsp;{% trans "Download" %}</a>
+<a href="{% url 'teleforma-registration-download' username %}" class="component_icon button" id="action_green" target="_blank"><img src="{{STATIC_URL}}telemeta/images/download.png" alt="" style="vertical-align:middle" />&nbsp;{% trans "Download" %}</a>
 
 </center>
 </div>
index 8bd656e1e61309875edca27da19b7738627ba2e1..0a27547f12deca1aa1a3f1fa2f4a4fd3a0c34e38 100644 (file)
@@ -3,7 +3,7 @@
 {% load telemeta_utils %}
 {% load teleforma_tags %}
 
-{% block title %}<center>{% trans "Registration" %} - {% description %}</center>{% endblock %}
+{% block title %}<center>{% trans "Pre-registration" %} - {% description %}</center>{% endblock %}
 
 {% block content %}
 <center>
diff --git a/teleforma/templates/registration/registration_pdf.html b/teleforma/templates/registration/registration_pdf.html
new file mode 100644 (file)
index 0000000..8d47c78
--- /dev/null
@@ -0,0 +1,65 @@
+<!DOCTYPE html>
+{% load i18n %}
+{% load telemeta_utils %}
+{% load teleforma_tags %}
+
+{% get_current_language as LANGUAGE_CODE %}
+{% get_available_languages as LANGUAGES %}
+<html xmlns="http://www.w3.org/1999/xhtml" lang="{{ LANGUAGE_CODE }}" xml:lang="{{ LANGUAGE_CODE }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
+
+<head>
+<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
+
+<title>{%block head_title %}{% description %} - {% trans "Registration" %}{% endblock %}</title>
+
+{% block stylesheets %}
+<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}teleforma/css/teleforma_pdf.css" />
+{% endblock %}
+
+{% block extra_stylesheets %}{% endblock %}
+
+</head>
+
+<body>
+{% block layout %}
+<div id="layout">
+
+<div id="header">
+    <span style="color: yellow; font-weight: bold;">&gt;</span> {% trans "Training registration" %}
+</div>
+
+<div id="content">
+
+   <table class="table1">
+      <tr><td class="bold">{% trans "Last name" %} : </td><td>{{ student.user.last_name|upper }}</td></tr>
+      <tr><td class="bold">{% trans "First name" %} : </td><td>{{ student.user.first_name|upper }}</td></tr>
+      <tr><td class="bold">{% trans "Address" %} : </td><td>{{ student.user.profile.all.0.address }} {{ student.user.profile.all.0.postal_code }} {{ student.user.profile.all.0.city }}</td></tr>
+      <tr><td class="bold">{% trans "Training" %} : </td><td>{{ student.training.name }}</td></tr>
+      <tr><td class="bold">{% trans "Training type" %} : </td><td>{% if student.platform_only %}E-learning{% else %}Présentielle et E-learning{% endif %}</td></tr>
+      <tr><td class="bold">{% trans "Registration date" %} : </td><td>{{ student.date_registered}}</td></tr>
+    </table>
+
+    <table class="table2">
+      <tr><td>
+          Je certifie avoir pris connaissance des conditions générales d’inscription et du règlement intérieur.
+      </td>
+      <td>
+          A ...................................., le .......................................
+          <br>
+          Signature
+      </td></tr>
+    </table>
+
+</div>
+
+{% block footer %}
+<div id="footer">
+    Copyright &copy; {% current_year %} Pré-Barreau
+ </div>
+{% endblock %}
+
+</div>
+{% endblock layout %}
+
+</body>
+</html>
index 1a311d9f6f90d08692c866bfdb5b8266a163d03d..1e1c1cdec606d5e09cb0f617af29c62e1152066d 100644 (file)
@@ -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<username>.*)/download/$', RegistrationPDFViewDowload.as_view(),
+                                                    name="teleforma-registration-download"),
+
     url(r'^captcha/', include('captcha.urls')),
 
     # Help
index becda0e01be468edffbfb819cb8729310d990291..c31b65714658a60d8c58c643913bf292fe3880ef 100644 (file)
@@ -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)
index 9a89fe9f699ab597f367afcda26fe109a1c85ff5..a2b25f3ec781fcfa10c610fe309e540bc7225799 100644 (file)
@@ -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')