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

« back to all changes in this revision

Viewing changes to desktopcouch/notes/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-notes.
4
4
#
16
16
#
17
17
# Authors: Rodrigo Moya <rodrigo.moya@canonical.com>
18
18
 
 
19
 
19
20
"""A dictionary based note record representation."""
20
21
 
21
 
from time import strptime
22
22
from desktopcouch.records.record import Record
23
23
 
24
24
NOTE_RECORD_TYPE = 'http://www.freedesktop.org/wiki/Specifications/desktopcouch/note'
25
 
# keep in sync with the above notes record type
26
 
FIELDS = {
27
 
    # String fields
28
 
    'note_format': 'string',
29
 
    'title': 'string',
30
 
    'content': 'string',
31
 
    # Date fields
32
 
    'last_change_date': 'date',
33
 
    'create_date': 'date'
34
 
}
35
 
 
36
 
class NoteBase(Record):
37
 
    """
38
 
    A base for Note records.
39
 
 
40
 
    Use make_note_class to create the Note class with the required fields.
41
 
    """
 
25
 
 
26
class Note(Record):
 
27
    """An Ubuntuone Note Record."""
42
28
 
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)
46
 
 
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:
51
 
 
52
 
        def fget(self, _field_name=field_name):
53
 
            return self.get(_field_name)
54
 
 
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:
61
 
                    return
62
 
            self[_field_name] = value
63
 
 
64
 
        setattr(NoteClass, field_name, property(fget, fset))
65
 
    return NoteClass
66
 
 
67
 
Note = make_note_class(FIELDS)