diff options
Diffstat (limited to 'archaeological_finds/models_treatments.py')
-rw-r--r-- | archaeological_finds/models_treatments.py | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/archaeological_finds/models_treatments.py b/archaeological_finds/models_treatments.py index d687199db..ce9066fc7 100644 --- a/archaeological_finds/models_treatments.py +++ b/archaeological_finds/models_treatments.py @@ -18,6 +18,12 @@ # See the file COPYING for details. import datetime +import lxml.etree +import lxml.builder +import os +import shutil +import tempfile +import zipfile from django.conf import settings from django.contrib.gis.db import models @@ -1379,6 +1385,36 @@ class TreatmentFile( def natural_key(self): return (self.year, self.index) + def is_gam_exportable(self): + self.gam_errors = [] + is_ok = True + return is_ok + + def gam_export(self): + filename = f"export_gam-{self.exhibition_start_date.year}-{slugify(self.name).replace('-', '_')}.zip" + maker = lxml.builder.ElementMaker() + document = maker.proposition( + maker.action("CREATION"), + maker.categorie("PRET"), + ) + content = b'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' + content += lxml.etree.tostring(document, pretty_print=True) + gam_dir = os.path.join(settings.MEDIA_ROOT, "GAM") + if not os.path.exists(gam_dir): + os.mkdir(gam_dir) + media_dir = os.path.join("GAM", str(datetime.date.today().year)) + full_media_dir = os.path.join(settings.MEDIA_ROOT, media_dir) + if not os.path.exists(full_media_dir): + os.mkdir(full_media_dir) + final_name = os.path.join(full_media_dir, filename) + with tempfile.TemporaryDirectory() as tmp_dir_name: + new_file = os.path.join(tmp_dir_name, filename) + with zipfile.ZipFile( + new_file, mode="a", compression=zipfile.ZIP_DEFLATED) as zf: + zf.writestr("pret.xml", content) + shutil.move(new_file, final_name) + return f"{settings.MEDIA_URL}{media_dir}/{filename}" + @property def short_class_name(self): return _("Treatment request") @@ -1598,7 +1634,6 @@ class Exhibition( request, "archaeological_finds.add_treatmentfile" ) if can_add_tf: - actions += [ ( reverse("exhibition-qa-add-loan", args=[self.pk]), |