eo_factures: add action to exports invoices (#61786)
This commit is contained in:
parent
04aafc724f
commit
2c0a27bd57
|
@ -15,7 +15,9 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import csv
|
||||
import datetime
|
||||
import os
|
||||
import zipfile
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.http import HttpResponse
|
||||
|
@ -73,3 +75,18 @@ def export_references_as_fodt(modeladmin, request, queryset):
|
|||
|
||||
|
||||
export_references_as_fodt.short_description = _("Export selected contracts as fodt file")
|
||||
|
||||
|
||||
def export_invoices_as_zip(modeladmin, request, queryset):
|
||||
folder = 'EO_Factures_%s' % datetime.date.today().strftime('%Y%m%d')
|
||||
response = HttpResponse(content_type='application/zip')
|
||||
response['Content-Disposition'] = 'attachment; filename=%s.zip' % folder
|
||||
with zipfile.ZipFile(response, mode='w') as zip_file:
|
||||
for facture in queryset:
|
||||
pdf = facture.pdf(base_uri=request.build_absolute_uri('/'))
|
||||
filename = os.path.join(folder, facture.filename())
|
||||
zip_file.writestr(filename, pdf)
|
||||
return response
|
||||
|
||||
|
||||
export_invoices_as_zip.short_description = _("Export selected invoices")
|
||||
|
|
|
@ -278,6 +278,7 @@ class FactureAdmin(LookupAllowed, admin.ModelAdmin):
|
|||
save_on_top = True
|
||||
search_fields = ['contrat__intitule', 'contrat__client__nom']
|
||||
list_select_related = True
|
||||
actions = [actions.export_invoices_as_zip]
|
||||
autocomplete_fields = ['client']
|
||||
|
||||
def column_montant(self, obj):
|
||||
|
|
|
@ -66,3 +66,18 @@ def test_odt_export(app):
|
|||
res = form.submit('Go')
|
||||
assert res.status_code == 200
|
||||
assert res.content_type == 'application/vnd.oasis.opendocument.text'
|
||||
|
||||
|
||||
def test_zip_export(app):
|
||||
response = app.get("/eo_facture/facture/").follow()
|
||||
response.form.set("username", "admin")
|
||||
response.form.set("password", "admin")
|
||||
|
||||
factures = response.form.submit().follow()
|
||||
form = factures.forms['changelist-form']
|
||||
for i in range(5):
|
||||
form.set('_selected_action', True, index=i)
|
||||
form['action'] = 'export_invoices_as_zip'
|
||||
res = form.submit('Go')
|
||||
assert res.status_code == 200
|
||||
assert res.content_type == 'application/zip'
|
||||
|
|
Loading…
Reference in New Issue