From 365d679d7673f34cbb405e00091975202c40934b Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 21 Jul 2020 15:07:28 -0500 Subject: [PATCH] Add `split_street()` method for MemberInfo --- corepos/db/office_op/model.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/corepos/db/office_op/model.py b/corepos/db/office_op/model.py index 3684d39..8e4a1dc 100644 --- a/corepos/db/office_op/model.py +++ b/corepos/db/office_op/model.py @@ -24,12 +24,16 @@ Data model for CORE POS "office_op" DB """ +import logging + import sqlalchemy as sa from sqlalchemy import orm from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.associationproxy import association_proxy +log = logging.getLogger(__name__) + Base = declarative_base() @@ -1056,6 +1060,23 @@ class MemberInfo(Base): def __str__(self): return self.full_name + def split_street(self): + """ + Tries to split the :attr:`street` attribute into 2 separate lines, e.g. + "street1" and "street2" style. Always returns a 2-tuple even if the + second line would be empty. + """ + address = (self.street or '').strip() + lines = address.split('\n') + street1 = lines[0].strip() or None + street2 = None + if len(lines) > 1: + street2 = lines[1].strip() or None + if len(lines) > 2: + log.warning("member #%s has %s address lines: %s", + self.card_number, len(lines), self) + return (street1, street2) + class MemberDate(Base): """