18
18
# Nicola Larosa <nicola.larosa@canonical.com>
19
19
# Mark G. Saye <mark.saye@canonical.com>
21
22
"""A dictionary based contact record representation."""
23
24
from desktopcouch.records.record import Record
25
CONTACT_RECORD_TYPE = (
26
'http://www.freedesktop.org/wiki/Specifications/desktopcouch/contact')
27
# keep in sync with the above contacts record type
28
SINGLE_FIELDS = frozenset((
29
'first_name', 'middle_name', 'last_name', 'title', 'suffix', 'birth_date',
30
'nick_name', 'spouse_name', 'wedding_date', 'company', 'department',
31
'job_title', 'manager_name', 'assistant_name', 'office',
33
LIST_FIELDS = frozenset((
34
'addresses', 'phone_numbers', 'email_addresses', 'urls', 'im_addresses',
36
ALL_FIELDS = SINGLE_FIELDS | LIST_FIELDS
39
class ContactBase(Record):
41
A base for Contact records.
43
Use make_contact_class to create the Contact class with the required fields.
26
CONTACT_RECORD_TYPE = 'http://www.freedesktop.org/wiki/Specifications/desktopcouch/contact'
29
class Contact(Record):
30
"""An Ubuntuone Contact Record."""
46
32
def __init__(self, data=None, record_id=None):
47
super(ContactBase, self).__init__(
33
super(Contact, self).__init__(
48
34
record_id=record_id, data=data, record_type=CONTACT_RECORD_TYPE)
51
def make_contact_class(field_names):
52
"""Contact class factory function. field_names is a strings iterable."""
53
ContactClass = type('Contact', (ContactBase,), {})
54
for field_name in field_names:
56
# without the _field_name param, only the last one gets used
57
def fget(self, _field_name=field_name):
58
return self.get(_field_name)
60
def fset(self, value, _field_name=field_name):
61
self[_field_name] = value
63
setattr(ContactClass, field_name, property(fget, fset))
67
Contact = make_contact_class(ALL_FIELDS)