7212: convert invalid date to text on import

Consolidate the new logic with the older code -- now malformatted
dates, as well as well-formatted invalid ones, will also be
converted to text.
This commit is contained in:
Vassilii Khachaturov 2013-11-25 12:37:19 +02:00
parent b3259d0a00
commit 3af50b0bea
2 changed files with 28 additions and 12 deletions

View File

@ -465,15 +465,13 @@ class VCardParser(object):
"""Read the BDAY property of a VCard."""
date_str = data.strip()
date_match = VCardParser.DATE_RE.match(date_str)
date = Date()
if date_match:
if date_match.group(2):
date_str = "%s-%s-%s" % (date_match.group(2),
date_match.group(3), date_match.group(4))
else:
date_str = date_match.group(1)
event = Event()
event.set_type(EventType(EventType.BIRTH))
date = Date()
y, m, d = [int(x, 10) for x in date_str.split('-')]
try:
date.set(value=(d, m, y, False))
@ -482,18 +480,24 @@ class VCardParser(object):
# in the format string, but you may re-order them if needed.
LOG.warning(_(
"Invalid date {date} in BDAY {vcard_snippet}, "
"preserving date as text"
"preserving date as text."
).format(date=e.date.to_struct(), vcard_snippet=data))
date.set(modifier=Date.MOD_TEXTONLY, text=data)
event.set_date_object(date)
self.database.add_event(event, self.trans)
event_ref = EventRef()
event_ref.set_reference_handle(event.get_handle())
self.person.set_birth_ref(event_ref)
else:
LOG.warn("Date %s not in appropriate format yyyy-mm-dd, "
"line skipped." % date_str)
# TRANSLATORS: leave the {vcard_snippet} untranslated.
LOG.warning(_(
"Date {vcard_snippet} not in appropriate format yyyy-mm-dd, "
"preserving date as text."
).format(vcard_snippet=date_str))
date.set(modifier=Date.MOD_TEXTONLY, text=date_str)
event = Event()
event.set_type(EventType(EventType.BIRTH))
event.set_date_object(date)
self.database.add_event(event, self.trans)
event_ref = EventRef()
event_ref.set_reference_handle(event.get_handle())
self.person.set_birth_ref(event_ref)
def add_occupation(self, fields, data):
"""Read the ROLE property of a VCard."""

View File

@ -462,6 +462,18 @@ class VCardCheck(unittest.TestCase):
ET.SubElement(event, 'datestr', {'val': '20010229'})
self.do_test("\r\n".join(self.vcard), self.gramps)
def test_birthday_invalid_format_converted_to_datestr(self):
self.vcard.insert(4, 'BDAY:unforgettable')
attribs = {'hlink': 'E0000', 'role': 'Primary'}
eventref = ET.SubElement(self.person, 'eventref', attribs)
events = ET.Element('events')
self.gramps.insert(1, events)
attribs = {'handle': 'E0000', 'id': 'E0000'}
event = ET.SubElement(events, 'event', attribs)
ET.SubElement(event, 'type').text = 'Birth'
ET.SubElement(event, 'datestr', {'val': 'unforgettable'})
self.do_test("\r\n".join(self.vcard), self.gramps)
def test_add_birthday_one_dash(self):
self.vcard.insert(4, 'BDAY:2001-0928')
attribs = {'hlink': 'E0000', 'role': 'Primary'}