eo_facture: factorize invoice filename (#61786)
This commit is contained in:
parent
ad6738cd27
commit
da785466db
|
@ -28,6 +28,7 @@ from django.db.models import F, Q, Sum
|
|||
from django.db.models.query import QuerySet
|
||||
from django.db.models.signals import post_delete, post_save
|
||||
from django.template.loader import get_template
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from weasyprint import HTML
|
||||
|
@ -428,6 +429,13 @@ class Facture(models.Model):
|
|||
payment.save()
|
||||
return facture_avoir
|
||||
|
||||
def filename(self, add_client_name=False):
|
||||
return '%s%s%s.pdf' % (
|
||||
self.code(),
|
||||
'-%s' % force_text(self.contrat.client.nom) if add_client_name else '',
|
||||
'-AVOIR' if self.annulation else '',
|
||||
)
|
||||
|
||||
class Meta:
|
||||
ordering = ("-id",)
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
{% if original.contrat %}
|
||||
<li><a href="{% url "admin:eo_facture_contrat_change" original.contrat.id %}" class="historylink">Contrat</a></li>
|
||||
{% endif %}
|
||||
<li><a href="{{history_url}}../view_pdf/{{ original.code }}{% if original.annulation %}-AVOIR{% endif %}.pdf">Imprimer</a></li>
|
||||
<li><a href="{{history_url}}../view_pdf/{{ original.filename }}">Imprimer</a></li>
|
||||
{% if original.client.chorus_structure and not original.proforma %}
|
||||
<li><a href="{% url "admin:eo_facture_facture_send_to_chorus" original.id %}">Envoyer à Chorus</a></li>
|
||||
{% endif %}
|
||||
|
|
|
@ -24,7 +24,6 @@ from django.contrib import messages
|
|||
from django.contrib.admin.models import LogEntry
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from eo_gestion.chorus.chorus import push_to_chorus
|
||||
|
||||
|
@ -46,15 +45,7 @@ def facture_pdf(request, facture_id):
|
|||
else:
|
||||
pdf = facture.pdf(base_uri=request.build_absolute_uri('/'))
|
||||
if hasattr(settings, 'FACTURE_DIR'):
|
||||
filename = os.path.join(
|
||||
settings.FACTURE_DIR,
|
||||
'%s-%s%s.pdf'
|
||||
% (
|
||||
facture.code(),
|
||||
force_text(facture.contrat.client.nom),
|
||||
'-AVOIR' if facture.annulation else '',
|
||||
),
|
||||
)
|
||||
filename = os.path.join(settings.FACTURE_DIR, facture.filename(add_client_name=True))
|
||||
with open(filename, 'wb') as fd:
|
||||
fd.write(pdf)
|
||||
return http.HttpResponse(pdf, content_type='application/pdf')
|
||||
|
|
Loading…
Reference in New Issue