~ubuntu-branches/ubuntu/trusty/desktopcouch/trusty

« back to all changes in this revision

Viewing changes to desktopcouch/contacts/record.py

  • Committer: Bazaar Package Importer
  • Author(s): Ken VanDine
  • Date: 2010-04-19 12:52:22 UTC
  • mfrom: (12.1.9 lucid)
  • Revision ID: james.westby@ubuntu.com-20100419125222-zsh9lrm15h84951j
* debian/patches/lp_522538.patch
  - Handle reconnects if the server isn't running (LP: #522538)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009-2010 Canonical Ltd.
 
1
# Copyright 2009 Canonical Ltd.
2
2
#
3
3
# This file is part of desktopcouch-contacts.
4
4
#
18
18
#          Nicola Larosa <nicola.larosa@canonical.com>
19
19
#          Mark G. Saye <mark.saye@canonical.com>
20
20
 
 
21
 
21
22
"""A dictionary based contact record representation."""
22
23
 
23
24
from desktopcouch.records.record import Record
24
25
 
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',
32
 
))
33
 
LIST_FIELDS = frozenset((
34
 
    'addresses', 'phone_numbers', 'email_addresses', 'urls', 'im_addresses',
35
 
))
36
 
ALL_FIELDS = SINGLE_FIELDS | LIST_FIELDS
37
 
 
38
 
 
39
 
class ContactBase(Record):
40
 
    """
41
 
    A base for Contact records.
42
 
 
43
 
    Use make_contact_class to create the Contact class with the required fields.
44
 
    """
 
26
CONTACT_RECORD_TYPE = 'http://www.freedesktop.org/wiki/Specifications/desktopcouch/contact'
 
27
 
 
28
 
 
29
class Contact(Record):
 
30
    """An Ubuntuone Contact Record."""
45
31
 
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)
49
 
 
50
 
 
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:
55
 
 
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)
59
 
 
60
 
        def fset(self, value, _field_name=field_name):
61
 
            self[_field_name] = value
62
 
 
63
 
        setattr(ContactClass, field_name, property(fget, fset))
64
 
    return ContactClass
65
 
 
66
 
 
67
 
Contact = make_contact_class(ALL_FIELDS)