improve (?) contact extension

This commit is contained in:
Lance Edgar 2012-08-10 11:45:20 -07:00
parent def03c00fb
commit e458f9133c

View file

@ -34,7 +34,8 @@ from sqlalchemy.ext.orderinglist import ordering_list
from edbob.db.model import Base, uuid_column from edbob.db.model import Base, uuid_column
__all__ = ['Phone', 'Person', 'PersonPhone'] __all__ = ['PhoneNumber', 'Person', 'PersonPhoneNumber', 'EmailAddress',
'PersonEmailAddress']
def get_person_display_name(context): def get_person_display_name(context):
@ -54,12 +55,12 @@ def get_person_display_name(context):
return None return None
class Phone(Base): class PhoneNumber(Base):
""" """
Represents a phone (or fax) number associated with a contactable entity. Represents a phone (or fax) number associated with a contactable entity.
""" """
__tablename__ = 'phones' __tablename__ = 'phone_numbers'
uuid = uuid_column() uuid = uuid_column()
parent_type = Column(String(20), nullable=False) parent_type = Column(String(20), nullable=False)
@ -77,7 +78,7 @@ class Phone(Base):
return unicode(self.number) return unicode(self.number)
class PersonPhone(Phone): class PersonPhoneNumber(PhoneNumber):
""" """
Represents a phone (or fax) number associated with a :class:`Person`. Represents a phone (or fax) number associated with a :class:`Person`.
""" """
@ -85,6 +86,37 @@ class PersonPhone(Phone):
__mapper_args__ = {'polymorphic_identity': 'Person'} __mapper_args__ = {'polymorphic_identity': 'Person'}
class EmailAddress(Base):
"""
Represents an email address associated with a contactable entity.
"""
__tablename__ = 'email_addresses'
uuid = uuid_column()
parent_type = Column(String(20), nullable=False)
parent_uuid = Column(String(32), nullable=False)
preference = Column(Integer, nullable=False)
type = Column(String(15))
address = Column(String(255), nullable=False)
__mapper_args__ = {'polymorphic_on': parent_type}
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self.address)
def __unicode__(self):
return unicode(self.address)
class PersonEmailAddress(EmailAddress):
"""
Represents an email address associated with a :class:`Person`.
"""
__mapper_args__ = {'polymorphic_identity': 'Person'}
class Person(Base): class Person(Base):
""" """
Represents a real, living and breathing person. (Or, at least was Represents a real, living and breathing person. (Or, at least was
@ -104,20 +136,48 @@ class Person(Base):
def __unicode__(self): def __unicode__(self):
return unicode(self.display_name or '') return unicode(self.display_name or '')
Person.phones = relationship( def add_email_address(self, address, type='Home'):
PersonPhone, email = PersonEmailAddress(address=address, type=type)
backref='person', self.emails.append(email)
primaryjoin=PersonPhone.parent_uuid == Person.uuid,
foreign_keys=[PersonPhone.parent_uuid],
collection_class=ordering_list('preference', count_from=1),
order_by=PersonPhone.preference)
Person.phone = relationship( def add_phone_number(self, number, type='Home'):
PersonPhone, phone = PersonPhoneNumber(number=number, type=type)
self.phones.append(phone)
Person.emails = relationship(
PersonEmailAddress,
backref='person',
primaryjoin=PersonEmailAddress.parent_uuid == Person.uuid,
foreign_keys=[PersonEmailAddress.parent_uuid],
collection_class=ordering_list('preference', count_from=1),
order_by=PersonEmailAddress.preference,
cascade='save-update, merge, delete, delete-orphan')
Person.email = relationship(
PersonEmailAddress,
primaryjoin=and_( primaryjoin=and_(
PersonPhone.parent_uuid == Person.uuid, PersonEmailAddress.parent_uuid == Person.uuid,
PersonPhone.preference == 1, PersonEmailAddress.preference == 1,
), ),
foreign_keys=[PersonPhone.parent_uuid], foreign_keys=[PersonEmailAddress.parent_uuid],
uselist=False,
viewonly=True)
Person.phones = relationship(
PersonPhoneNumber,
backref='person',
primaryjoin=PersonPhoneNumber.parent_uuid == Person.uuid,
foreign_keys=[PersonPhoneNumber.parent_uuid],
collection_class=ordering_list('preference', count_from=1),
order_by=PersonPhoneNumber.preference,
cascade='save-update, merge, delete, delete-orphan')
Person.phone = relationship(
PersonPhoneNumber,
primaryjoin=and_(
PersonPhoneNumber.parent_uuid == Person.uuid,
PersonPhoneNumber.preference == 1,
),
foreign_keys=[PersonPhoneNumber.parent_uuid],
uselist=False, uselist=False,
viewonly=True) viewonly=True)