Project

General

Profile

0001-contrib.teamnet_axel-invoice-pdf-download-endpoint-8.patch

Serghei Mihai, 21 Sep 2015 01:35 PM

Download (4.58 KB)

View differences:

Subject: [PATCH] contrib.teamnet_axel: invoice pdf download endpoint (#8035)

 passerelle/contrib/teamnet_axel/models.py                |  9 +++++++++
 .../passerelle/contrib/teamnet_axel/detail.html          |  6 ++++++
 passerelle/contrib/teamnet_axel/urls.py                  |  2 ++
 passerelle/contrib/teamnet_axel/views.py                 | 16 ++++++++++++++++
 4 files changed, 33 insertions(+)
passerelle/contrib/teamnet_axel/models.py
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
import logging
18
import base64
18 19
import xml.etree.ElementTree as ET
19 20

  
20 21
from django.core.urlresolvers import reverse
......
152 153
        invoices = [utils.normalize_invoice(i.attrib, family_id) for i in xml_invoices]
153 154
        return sorted(invoices, key=lambda i: i['created'], reverse=True)
154 155

  
156
    def invoice_pdf(self, family_id, invoice_id):
157
        invoice_xml = ET.Element('FACTUREPDF')
158
        ET.SubElement(invoice_xml, 'IDFAMILLE').text = family_id
159
        ET.SubElement(ET.SubElement(invoice_xml, 'FACTURES'), 'NOFACTURE').text = invoice_id
160
        data = self.get_data('FacturesPDF', invoice_xml)
161
        pdf = data.find('PORTAIL/PDF')
162
        return base64.b64decode(pdf.get('FILE'))
163

  
155 164

  
156 165
class Link(models.Model):
157 166
    resource = models.ForeignKey(Management)
passerelle/contrib/teamnet_axel/templates/passerelle/contrib/teamnet_axel/detail.html
58 58
  <em>2345</em> {% trans 'is family identifier' %},
59 59
  <em>20150916</em> {% trans 'is invoice identifier' %}
60 60
</li>
61
<li>{% trans 'Get invoice pdf:' %}
62
  {% url 'teamnet-axel-invoice-pdf' slug=object.slug regie_id='42' invoice_id='2345-20150916' as invoice_download_url %}
63
  <a href="{{ invoice_download_url }}">
64
    {{ site_base_uri }}{{ invoice_download_url }}
65
  </a>
66
</li>
61 67
</ul>
62 68
</div>
63 69

  
passerelle/contrib/teamnet_axel/urls.py
41 41
       name='teamnet-axel-invoices'),
42 42
    url(r'^(?P<slug>[\w,-]+)/regie/(?P<regie_id>[\w,-]+)/invoice/(?P<invoice_id>[\w,-]+)/$', InvoiceView.as_view(),
43 43
        name='teamnet-axel-invoice'),
44
    url(r'^(?P<slug>[\w,-]+)/regie/(?P<regie_id>[\w,-]+)/invoice/(?P<invoice_id>[\w,-]+)/pdf/$', InvoicePDFView.as_view(),
45
        name='teamnet-axel-invoice-pdf'),
44 46
    #url(r'^(?P<slug>[\w,-]+)/invoice-pdf/(?P<id>[\w,-]+)/$', InvoicePDFView.as_view(),
45 47
    #    name='teamnet-axel-unlink'),
46 48
    #url(r'^(?P<slug>[\w,-]+)/invoice-paid/(?P<id>[\w,-]+)/$', InvoicePayView.as_view(),
passerelle/contrib/teamnet_axel/views.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17

  
17 18
from django.core.urlresolvers import reverse
18 19
from django.views.generic import DetailView as GenericDetailView
19 20
from django.views.generic.edit import CreateView, UpdateView, DeleteView
21
from django.http import HttpResponse, Http404
20 22

  
21 23
from passerelle import utils
22 24

  
......
164 166
        for invoice in invoices:
165 167
            if invoice['id'] == kwargs['invoice_id']:
166 168
                return invoice
169

  
170

  
171
class InvoicePDFView(DetailView):
172

  
173
    def get_data(self, request, *args, **kwargs):
174
        family_id, invoice_id = kwargs['invoice_id'].split('-', 1)
175
        pdf = self.object.invoice_pdf(family_id, invoice_id)
176
        if not pdf:
177
            raise Http404()
178

  
179
        response = HttpResponse(content_type='application/pdf')
180
        response['Content-Disposition'] = 'attachment; filename="%s.pdf"' % invoice_id
181
        response.write(pdf)
182
        return response
167
-