Project

General

Profile

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

Serghei Mihai, 18 Sep 2015 05:52 PM

Download (4.38 KB)

View differences:

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

 passerelle/contrib/teamnet_axel/models.py               |  7 +++++++
 .../passerelle/contrib/teamnet_axel/detail.html         |  6 ++++++
 passerelle/contrib/teamnet_axel/urls.py                 |  2 ++
 passerelle/contrib/teamnet_axel/views.py                | 17 +++++++++++++++++
 4 files changed, 32 insertions(+)
passerelle/contrib/teamnet_axel/models.py
152 152
        invoices = [utils.normalize_invoice(i.attrib, family_id) for i in xml_invoices]
153 153
        return sorted(invoices, key=lambda i: i['created'], reverse=True)
154 154

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

  
156 163
class Link(models.Model):
157 164
    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
import base64
18

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

  
21 24
from passerelle import utils
22 25

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

  
171

  
172
class InvoicePDFView(DetailView):
173

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

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