3
logger = logging.getLogger("contact_data")
5
def get_instance(cls, data):
8
@param cls: The class of the object to instantiate.
10
@param data: A dictiornary with the data of the object to desirialize.
12
Method that takes a class and return a new instance with the passed
13
data in athe given dictionary.
15
# create an instance using the class
17
for attribute in cls._attributes:
19
setattr(instance, attribute, data[attribute])
22
def get_dictionary(instance):
26
for attribute in type(instance)._attributes:
27
current_data = getattr(instance, attribute)
28
if current_data and attribute != "id":
29
data[attribute] = current_data
32
class ContactData(object):
34
Represents the bas class that should be extended of contact data that
35
can be added to a contact.
38
def __init__(self, description=None):
39
self.description = description
41
def __cmp__(self, other):
42
if isinstance(other, type(self)):
43
for current_attribute in self._attributes:
44
first_value = getattr(self, current_attribute)
45
second_value = getattr(other, current_attribute)
46
if not first_value == second_value:
47
return cmp(first_value, second_value)
50
raise TypeError("{0} {1} cannot be compared".format(
51
str(type(self)), str(type(other))))
53
def __eq__(self, other):
54
if isinstance(other, type(self)):
55
return cmp(self, other) == 0
56
raise TypeError("{0} {1} cannot be compared".format(
57
str(type(self)), str(type(other))))
61
for current_attribute in self._attributes:
62
result += hash(getattr(self, current_attribute))
67
def _get_description(self):
68
# test if the class has a description enum
69
if type(self)._description_enum is not None:
70
if hasattr(self, "_description"):
71
return self._description
75
"The class {0} does not support descriptions.".format(
78
def _set_description(self, value):
79
# test if the class has a description enum
80
if type(self)._description_enum is not None:
81
if value in type(self)._description_enum or value is None:
82
self._description = value
84
raise ValueError("Wrong description provided {0}".format(
88
"The class {0} does not support descriptions.".format(
91
def _attributes(self):
93
Returns the accepted attributes of the class.
95
return type(self)._attributes
98
description = property(_get_description, _set_description)
100
################################################################################
101
########################### Address implementation #############################
102
################################################################################
104
WORK_ADDRESS = "work"
105
HOME_ADDRESS = "home"
106
OTHER_ADDRESS = "other"
108
ADDRESS_DESCRIPTIONS = (WORK_ADDRESS,
112
class Address(ContactData):
114
Represents an address in the system. An address will have the required
115
information to send packages etc...
118
_attributes = ("country",
125
_description_enum = ADDRESS_DESCRIPTIONS
127
def __init__(self, country=None, state=None, city=None, postalcode=None,
128
street=None, description=None):
130
@type country: string
133
@type postalcode: string
135
@type description: string
136
@param country: Country where the address can be found.
137
@param state: State where the address can be found.
138
@param city: City where the addrss can be found
139
@param postalcode: The postcalcode of the address.
140
@param street: The actual address
141
@param description: Description that gives more information about the
144
Creates a new address that can be added to a user.
146
ContactData.__init__(self, description=description)
147
self.country = country
150
self.postalcode = postalcode
154
return "{0} {1} {2} {3}".format(
155
self.country, self.state, self.city, self.street)
157
###############################################################################
158
########################### Email address implementation ######################
159
###############################################################################
161
WORK_EMAIL_ADDRESS = "work"
162
HOME_EMAIL_ADDRESS = "home"
163
OTHER_EMAIL_ADDRESS = "other"
165
EMAIL_ADDRESS_DESCRIPTIONS = (WORK_EMAIL_ADDRESS,
167
OTHER_EMAIL_ADDRESS,)
169
class EmailAddress(ContactData):
171
Represents an emial address in the api.
174
_attributes = ("address", "description",)
176
_description_enum = EMAIL_ADDRESS_DESCRIPTIONS
178
def __init__(self, address=None, description=None):
180
@type address: string
181
@param address: The string that represents the address
182
@type description: string
183
@param description: A nice description about the address.
185
Creates a new address that can be used in the API.
187
ContactData.__init__(self, description=description)
188
self.address = address
193
###############################################################################
194
######################### Instant messenger implementation ####################
195
###############################################################################
198
GADU_GADU_PROTOCOL = "gadu-gadu"
199
GROUPWISE_PROTOCOL = "groupwise"
202
JABBER_PROTOCOL = "jabber"
204
SKYPE_PROTOCOL = "skype"
205
YAHOO_PROTOCOL = "yahoo"
207
IM_ADDRESS_PROTOCOLS = (AIM_PROTOCOL,
217
class InstantMessenger(ContactData):
219
This class represents an instant messenger account that a contact can have.
222
_attributes = ("address", "description", )
224
_description_enum = IM_ADDRESS_PROTOCOLS
226
def __init__(self, address=None, protocol=None, description=None):
228
@type user_name: string
229
@param user_name: The user name in the m protocol.
230
@type protocol: Protocol
231
@param protocol: The protocol used by the im.
232
Creates a new instant messenger to be used by the contact.
234
ContactData.__init__(self, description=protocol)
235
self.address = address
236
self._text_description = description
238
def _get_text_description(self):
239
return self._text_description
241
def _set_text_description(self, value):
242
self._text_description = value
246
# rebind the description since the instant messenger uses it in a diff
248
description = property(_get_text_description, _set_text_description)
249
protocol = property(ContactData._get_description,
250
ContactData._set_description)
252
###############################################################################
253
######################## Telephone Number implementation ######################
254
###############################################################################
256
HOME_PHONE_NUMBER = "home"
257
MOBILE_PHONE_NUMBER = "mobile"
258
HOME_FAX_NUMBER = "home fax"
259
WORK_PHONE_NUMBER = "work"
260
CAR_PHONE_NUMBER = "car"
261
PAGER_PHONE_NUMBER = "pager"
262
WORK_FAX_PHONE_NUMBER = "work fax"
263
ASSISTANT_PHONE_NUMBER = "assistant"
264
CALLBACK_PHONE_NUMBER = "callback"
265
COMPANY_PHONE_NUMBER = "company"
266
OTHER_PHONE_NUMBER = "other"
267
OTHER_FAX_PHONE_NUMBER = "other fax"
268
PRIMARY_PHONE_NUMBER = "primary"
269
TELEX_PHONE_NUMBER = "telex"
271
PHONE_NUMBER_DESCRIPTIONS = (HOME_PHONE_NUMBER,
277
WORK_FAX_PHONE_NUMBER,
278
ASSISTANT_PHONE_NUMBER,
279
CALLBACK_PHONE_NUMBER,
280
COMPANY_PHONE_NUMBER,
282
OTHER_FAX_PHONE_NUMBER,
283
PRIMARY_PHONE_NUMBER,
284
TELEX_PHONE_NUMBER, )
286
class TelephoneNumber(ContactData):
288
Represents a telephone in the address book.
291
_attributes = ("number", "description", )
293
_description_enum = PHONE_NUMBER_DESCRIPTIONS
295
def __init__(self, number=None, description=None):
297
Creates a new number to be used in the system.
299
ContactData.__init__(self, description=description)
302
###############################################################################
303
############################ Url implementation ###############################
304
###############################################################################
306
HOME_PAGE_URL = "home page"
308
URL_DESCRIPTIONS = (HOME_PAGE_URL, BLOG_URL, )
310
class Url(ContactData):
312
Represents a url in the system that can be added in API.
315
_attributes = ("url", "description", )
317
_description_enum = URL_DESCRIPTIONS
319
def __init__(self, url=None, description=None):
322
@param url: The url of the webage.
323
@type description: string
324
@param description: A useful description about the webpage/url
326
Creates a new instance of the object.
328
ContactData.__init__(self, description=description)