0001-item-pdf-download-view-8220.patch
lingo/models.py | ||
---|---|---|
29 | 29 |
from django.forms import models as model_forms, Select |
30 | 30 |
from django.utils.translation import ugettext_lazy as _ |
31 | 31 |
from django.utils import timezone |
32 |
from django.core.exceptions import PermissionDenied |
|
32 | 33 | |
33 | 34 |
from ckeditor.fields import RichTextField |
34 | 35 | |
... | ... | |
52 | 53 |
display_id=data['display_id'], |
53 | 54 |
total_amount=data.get('total_amount'), |
54 | 55 |
amount=data.get('amount'), |
55 |
subject=data.get('label')) |
|
56 |
subject=data.get('label'), |
|
57 |
has_pdf=data.get('has_pdf')) |
|
56 | 58 | |
57 | 59 | |
58 | 60 |
class Regie(models.Model): |
... | ... | |
106 | 108 |
return [build_remote_item(item, self) for item in items.get('data')] |
107 | 109 |
return [] |
108 | 110 | |
111 |
def download_item(self, request, item_id): |
|
112 |
""" |
|
113 |
downloads item's file |
|
114 |
""" |
|
115 |
if self.is_remote(): |
|
116 |
if hasattr(request, 'session') and request.session.get('mellon_session'): |
|
117 |
mellon = request.session.get('mellon_session') |
|
118 |
url = self.webservice_url + '/invoice/%s/pdf' % item_id |
|
119 |
return self.get_url(request, url, NameID=mellon['name_id_content']) |
|
120 |
raise PermissionDenied |
|
121 | ||
109 | 122 |
def as_api_dict(self): |
110 | 123 |
return {'slug': self.slug, |
111 | 124 |
'label': self.label, |
... | ... | |
144 | 157 | |
145 | 158 |
class RemoteItem(object): |
146 | 159 |
def __init__(self, id, regie, creation_date, total_amount, |
147 |
amount, display_id, subject): |
|
160 |
amount, display_id, subject, has_pdf):
|
|
148 | 161 |
self.id = id |
149 | 162 |
self.regie = regie |
150 | 163 |
self.creation_date = parser.parse(creation_date) |
... | ... | |
152 | 165 |
self.display_id = display_id |
153 | 166 |
self.amount = amount |
154 | 167 |
self.subject = subject |
168 |
self.has_pdf = has_pdf |
|
155 | 169 | |
156 | 170 | |
157 | 171 |
class Transaction(models.Model): |
lingo/templates/lingo/combo/items.html | ||
---|---|---|
9 | 9 |
<td>{% trans "Label" %}</td> |
10 | 10 |
<td>{% trans "Issue date" %}</td> |
11 | 11 |
<td>{% trans "Amount" %}</td> |
12 |
<td></td> |
|
12 | 13 |
</tr> |
13 | 14 |
</thead> |
14 | 15 |
<tbody> |
... | ... | |
21 | 22 |
{{ amount }}€ |
22 | 23 |
{% endblocktrans %} |
23 | 24 |
</td> |
25 |
<td> |
|
26 |
{% if item.has_pdf %} |
|
27 |
<a href="{% url 'download-item-pdf' regie_id=item.regie.pk item_id=item.id %}" class="icon-pdf"> |
|
28 |
{% trans "Download" %}</a> |
|
29 |
{% endif %} |
|
30 |
</td> |
|
24 | 31 |
</tr> |
25 | 32 |
{% endfor %} |
26 | 33 |
</tbody> |
lingo/urls.py | ||
---|---|---|
18 | 18 | |
19 | 19 |
from combo.urls_utils import decorated_includes, manager_required |
20 | 20 | |
21 |
from .views import RegiesApiView, AddBasketItemApiView, PayView, CallbackView |
|
21 |
from .views import (RegiesApiView, AddBasketItemApiView, PayView, CallbackView, |
|
22 |
ItemDownloadView) |
|
22 | 23 |
from .manager_views import (RegieListView, RegieCreateView, RegieUpdateView, |
23 | 24 |
RegieDeleteView) |
24 | 25 | |
... | ... | |
39 | 40 |
url(r'^lingo/callback/(?P<regie_pk>\w+)/$', CallbackView.as_view(), name='lingo-callback'), |
40 | 41 |
url(r'^manage/lingo/', decorated_includes(manager_required, |
41 | 42 |
include(lingo_manager_urls))), |
43 |
url(r'^lingo/item/(?P<regie_id>[\w,-]+)/(?P<item_id>[\w,-]+)/pdf$', |
|
44 |
ItemDownloadView.as_view(), name='download-item-pdf'), |
|
42 | 45 |
) |
lingo/views.py | ||
---|---|---|
159 | 159 |
# ignore errors, it will be retried later on if it fails |
160 | 160 |
pass |
161 | 161 |
return HttpResponseRedirect('/') |
162 | ||
163 | ||
164 |
class ItemDownloadView(View): |
|
165 |
http_method_names = [u'get'] |
|
166 | ||
167 |
def get(self, request, *args, **kwargs): |
|
168 |
regie = Regie.objects.get(pk=kwargs['regie_id']) |
|
169 |
data = regie.download_item(request, kwargs['item_id']) |
|
170 |
r = HttpResponse(data, content_type='application/pdf') |
|
171 |
r['Content-Disposition'] = 'attachment; filename="%(item_id)s.pdf"' % kwargs |
|
172 |
return r |
|
162 |
- |