From 043e99bf5416e0899c02490fa600be4cfd049da1 Mon Sep 17 00:00:00 2001
From: Elena ``of Valhalla'' Grandi <valhalla@trueelena.org>
Date: Sat, 5 Aug 2017 18:41:38 +0200
Subject: Correctly load date fields

---
 scripts/tellico2lesana | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

(limited to 'scripts/tellico2lesana')

diff --git a/scripts/tellico2lesana b/scripts/tellico2lesana
index 7b4df16..0b0ec64 100755
--- a/scripts/tellico2lesana
+++ b/scripts/tellico2lesana
@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 
+import datetime
 from xml.etree import ElementTree
 import zipfile
 
@@ -44,7 +45,23 @@ class T2L(guacamole.Command):
             )
 
     def read_field_data(self, xfield):
-        if xfield.iter().__next__():
+        if xfield.tag in self.date_fields:
+            for child in xfield:
+                if 'year' in child.tag:
+                    year = child.text
+                elif 'month' in child.tag:
+                    month = child.text
+                elif 'day' in child.tag:
+                    day = child.text
+            try:
+                data = datetime.date(
+                    int(year),
+                    int(month),
+                    int(day)
+                    )
+            except ValueError:
+                data = None
+        elif xfield.iter().__next__():
             data = []
             for child in xfield:
                 data.append(self.read_field_data(child))
@@ -62,9 +79,14 @@ class T2L(guacamole.Command):
         # get collection settings
         title = xml_collection.attrib['title']
         xml_fields = xml_collection.find('tellico:fields', NS)
+        self.date_fields = []
         fields = []
         for xf in xml_fields:
-            f_type = F_TYPE_MAP.get(xf.attrib['format'])
+            if xf.attrib['type'] == '12':
+                self.date_fields.append(
+                    '{' + NS['tellico'] + '}' + xf.attrib['name']
+                    )
+            f_type = F_TYPE_MAP.get(xf.attrib['type'])
             # TODO: support fields with the multiple values flag
             # (they should probably become lists)
             try:
-- 
cgit v1.2.3