diff options
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index caf9be26c..2a7e386b2 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1617,6 +1617,46 @@ class DocumentTemplate(models.Model): output.write(result) return output_name + def convert_from_v1(self): + """ + Convert the current template from v1 to v2. + """ + from old.ooo_replace import ooo_replace + from archaeological_operations.models import AdministrativeAct + + old_dir = settings.MEDIA_ROOT + "/upload/templates/v1/" + if not os.path.exists(old_dir): + os.makedirs(old_dir) + shutil.copy(settings.MEDIA_ROOT + self.template.name, old_dir) + + tempdir = tempfile.mkdtemp("-ishtardocs") + output_name = tempdir + os.path.sep + self.template.name.split( + os.sep)[-1] + + objects = [] + filters = [ + {'operation__isnull': False}, + {'associated_file__isnull': False}, + {'treatment_file__isnull': False}, + {'treatment__isnull': False}, + ] + for filtr in filters: + q = AdministrativeAct.objects.filter(**filtr) + if q.count(): + objects.append(q.all()[0]) + + if not objects: + return + values = {} + for obj in objects: + values.update(obj.get_values()) + for key in values: + values[key] = "{{ " + key + " }}" + + ooo_replace(self.template, output_name, values) + shutil.move(output_name, settings.MEDIA_ROOT + self.template.name) + return output_name + class State(models.Model): label = models.CharField(_(u"Label"), max_length=30) |