17
17
# Authors: Rodrigo Moya <rodrigo.moya@canonical.com>
19
20
"""A dictionary based note record representation."""
21
from time import strptime
22
22
from desktopcouch.records.record import Record
24
24
NOTE_RECORD_TYPE = 'http://www.freedesktop.org/wiki/Specifications/desktopcouch/note'
25
# keep in sync with the above notes record type
28
'note_format': 'string',
32
'last_change_date': 'date',
36
class NoteBase(Record):
38
A base for Note records.
40
Use make_note_class to create the Note class with the required fields.
27
"""An Ubuntuone Note Record."""
43
29
def __init__(self, data=None, record_id=None):
44
super(NoteBase, self).__init__(
30
super(Note, self).__init__(
45
31
record_id=record_id, data=data, record_type=NOTE_RECORD_TYPE)
47
def make_note_class(fields):
48
"""Note class factory function. field_names is a list of strings."""
49
NoteClass = type('Note', (NoteBase,), {})
50
for field_name in fields:
52
def fget(self, _field_name=field_name):
53
return self.get(_field_name)
55
def fset(self, value, _field_name=field_name):
56
field_type = fields[_field_name]
57
if field_type == 'date':
58
# Check that it is a date with the correct format
59
date_value = strptime(value, "%Y-%m-%dT%H:%M:%S")
60
if date_value is None:
62
self[_field_name] = value
64
setattr(NoteClass, field_name, property(fget, fset))
67
Note = make_note_class(FIELDS)