summaryrefslogtreecommitdiffstats
path: root/pykolab/xml/todo.py
blob: b04b233399131a69260551bbca8b309d890cd4a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import datetime
import kolabformat
import icalendar
import pytz

import pykolab
from pykolab import constants
from pykolab.xml import Event
from pykolab.xml import utils as xmlutils
from pykolab.xml.event import InvalidEventDateError
from pykolab.translate import _

log = pykolab.getLogger('pykolab.xml_todo')

def todo_from_ical(string):
    return Todo(from_ical=string)

def todo_from_string(string):
    return Todo(from_string=string)

def todo_from_message(message):
    todo = None
    if message.is_multipart():
        for part in message.walk():
            if part.get_content_type() == "application/calendar+xml":
                payload = part.get_payload(decode=True)
                todo = todo_from_string(payload)

            # append attachment parts to Todo object
            elif todo and part.has_key('Content-ID'):
                todo._attachment_parts.append(part)

    return todo

# FIXME: extend a generic pykolab.xml.Xcal class instead of Event
class Todo(Event):
    type = 'task'

    def __init__(self, from_ical="", from_string=""):
        self._attendees = []
        self._categories = []
        self._attachment_parts = []

        self.properties_map.update({
            "due": "get_due",
            "percent-complete": "get_percentcomplete",
            "duration": "void",
            "end": "void"
        })

        if from_ical == "":
            if from_string == "":
                self.event = kolabformat.Todo()
            else:
                self.event = kolabformat.readTodo(from_string, False)
                self._load_attendees()
        else:
            self.from_ical(from_ical)

        self.uid = self.get_uid()

    def from_ical(self, ical):
        if hasattr(icalendar.Todo, 'from_ical'):
            ical_todo = icalendar.Todo.from_ical(ical)
        elif hasattr(icalendar.Todo, 'from_string'):
            ical_todo = icalendar.Todo.from_string(ical)

        # use the libkolab calendaring bindings to load the full iCal data
        if ical_todo.has_key('ATTACH') or [part for part in ical_todo.walk() if part.name == 'VALARM']:
            self._xml_from_ical(ical)
        else:
            self.event = kolabformat.Todo()

        for attr in list(set(ical_todo.required)):
            if ical_todo.has_key(attr):
                self.set_from_ical(attr.lower(), ical_todo[attr])

        for attr in list(set(ical_todo.singletons)):
            if ical_todo.has_key(attr):
                self.set_from_ical(attr.lower(), ical_todo[attr])

        for attr in list(set(ical_todo.multiple)):
            if ical_todo.has_key(attr):
                self.set_from_ical(attr.lower(), ical_todo[attr])

        # although specified by RFC 2445/5545, icalendar doesn't have this property listed
        if ical_todo.has_key('PERCENT-COMPLETE'):
            self.set_from_ical('percentcomplete', ical_todo['PERCENT-COMPLETE'])

    def _xml_from_ical(self, ical):
        self.event = Todo()
        self.event.fromICal("BEGIN:VCALENDAR\nVERSION:2.0\n" + ical + "\nEND:VCALENDAR")

    def set_ical_due(self, due):
        self.set_due(due)

    def set_due(self, _datetime):
        valid_datetime = False
        if isinstance(_datetime, datetime.date):
            valid_datetime = True

        if isinstance(_datetime, datetime.datetime):
            # If no timezone information is passed on, make it UTC
            if _datetime.tzinfo == None:
                _datetime = _datetime.replace(tzinfo=pytz.utc)

            valid_datetime = True

        if not valid_datetime:
            raise InvalidEventDateError, _("Todo due needs datetime.date or datetime.datetime instance")

        self.event.setDue(xmlutils.to_cdatetime(_datetime, True))

    def set_ical_percent(self, percent):
        self.set_percentcomplete(percent)

    def set_percentcomplete(self, percent):
        self.event.setPercentComplete(int(percent))

    def get_due(self):
        return xmlutils.from_cdatetime(self.event.due(), True)

    def get_ical_due(self):
        dt = self.get_due()
        if dt:
            return icalendar.vDatetime(dt)
        return None

    def get_percentcomplete(self):
        return self.event.percentComplete()

    def get_duration(self):
        return None

    def as_string_itip(self, method="REQUEST"):
        cal = icalendar.Calendar()
        cal.add(
            'prodid',
            '-//pykolab-%s-%s//kolab.org//' % (
                constants.__version__,
                constants.__release__
            )
        )

        cal.add('version', '2.0')
        cal.add('calscale', 'GREGORIAN')
        cal.add('method', method)

        ical_todo = icalendar.Todo()

        singletons = list(set(ical_todo.singletons))
        singletons.extend(['PERCENT-COMPLETE'])
        for attr in singletons:
            ical_getter = 'get_ical_%s' % (attr.lower())
            default_getter = 'get_%s' % (attr.lower())
            retval = None
            if hasattr(self, ical_getter):
                retval = getattr(self, ical_getter)()
                if not retval == None and not retval == "":
                    ical_todo.add(attr.lower(), retval)
            elif hasattr(self, default_getter):
                retval = getattr(self, default_getter)()
                if not retval == None and not retval == "":
                    ical_todo.add(attr.lower(), retval, encode=0)

        for attr in list(set(ical_todo.multiple)):
            ical_getter = 'get_ical_%s' % (attr.lower())
            default_getter = 'get_%s' % (attr.lower())
            retval = None
            if hasattr(self, ical_getter):
                retval = getattr(self, ical_getter)()
            elif hasattr(self, default_getter):
                retval = getattr(self, default_getter)()

            if isinstance(retval, list) and not len(retval) == 0:
                for _retval in retval:
                    ical_todo.add(attr.lower(), _retval, encode=0)

        # copy custom properties to iCal
        for cs in self.event.customProperties():
            ical_todo.add(cs.identifier, cs.value)

        cal.add_component(ical_todo)

        if hasattr(cal, 'to_ical'):
            return cal.to_ical()
        elif hasattr(cal, 'as_string'):
            return cal.as_string()

    def __str__(self):
        xml = kolabformat.writeTodo(self.event)

        error = kolabformat.error()

        if error == None or not error:
            return xml
        else:
            raise TodoIntegrityError, kolabformat.errorMessage()


class TodoIntegrityError(Exception):
    def __init__(self, message):
        Exception.__init__(self, message)