summaryrefslogtreecommitdiffstats
path: root/pykolab/xml/attendee.py
blob: 220ab8c5a13c167f9d1665b0937bb8cab5b081fd (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import kolabformat

from pykolab.translate import _

from contact_reference import ContactReference

participant_status_labels = {
        "NEEDS-ACTION": _("Needs Action"),
        "ACCEPTED": _("Accepted"),
        "DECLINED": _("Declined"),
        "TENTATIVE": _("Tentatively Accepted"),
        "DELEGATED": _("Delegated"),
        "COMPLETED": _("Completed"),
        "IN-PROCESS": _("In Process"),
        # support integer values, too
        kolabformat.PartNeedsAction: _("Needs Action"),
        kolabformat.PartAccepted: _("Accepted"),
        kolabformat.PartDeclined: _("Declined"),
        kolabformat.PartTentative: _("Tentatively Accepted"),
        kolabformat.PartDelegated: _("Delegated"),
    }

def participant_status_label(status):
    return participant_status_labels[status] if participant_status_labels.has_key(status) else status


class Attendee(kolabformat.Attendee):
    cutype_map = {
            "INDIVIDUAL": kolabformat.CutypeIndividual,
            "RESOURCE": kolabformat.CutypeResource,
            "GROUP": kolabformat.CutypeGroup,
        }

    participant_status_map = {
            "NEEDS-ACTION": kolabformat.PartNeedsAction,
            "ACCEPTED": kolabformat.PartAccepted,
            "DECLINED": kolabformat.PartDeclined,
            "TENTATIVE": kolabformat.PartTentative,
            "DELEGATED": kolabformat.PartDelegated,
            # Not yet implemented
            #"COMPLETED": ,
            #"IN-PROCESS": ,
        }

    # See RFC 2445, 5445
    role_map = {
            "CHAIR": kolabformat.Chair,
            "REQ-PARTICIPANT": kolabformat.Required,
            "OPT-PARTICIPANT": kolabformat.Optional,
            "NON-PARTICIPANT": kolabformat.NonParticipant,
        }

    rsvp_map = {
            "TRUE": True,
            "FALSE": False,
        }

    def __init__(
            self,
            email,
            name=None,
            rsvp=False,
            role=None,
            participant_status=None,
            cutype=None,
            ical_params=None
        ):

        self.email = email

        self.contactreference = ContactReference(email)

        if not name == None:
            self.contactreference.set_name(name)

        kolabformat.Attendee.__init__(self, self.contactreference)

        if isinstance(rsvp, bool):
            self.setRSVP(rsvp)
        else:
            if self.rsvp_map.has_key(rsvp):
                self.setRSVP(self.rsvp_map[rsvp])

        if not role == None:
            self.set_role(role)

        if not cutype == None:
            self.set_cutype(cutype)

        if ical_params and ical_params.has_key('DELEGATED-FROM'):
            self.delegate_from(Attendee(str(ical_params['DELEGATED-FROM']), role=self.get_role(), cutype=self.get_cutype()))

        if ical_params and ical_params.has_key('DELEGATED-TO'):
            self.delegate_to(Attendee(str(ical_params['DELEGATED-TO'])))

        if not participant_status == None:
            self.set_participant_status(participant_status)

    def delegate_from(self, delegators):
        crefs = []

        if not isinstance(delegators, list):
            delegators = [delegators]

        for delegator in delegators:
            if not isinstance(delegator, Attendee):
                raise ValueError, _("Not a valid attendee")
            else:
                self.set_role(delegator.get_role())
                self.set_cutype(delegator.get_cutype())
                crefs.append(delegator.contactreference)

        if len(crefs) == 0:
            raise ValueError, _("No valid delegator references found")
        else:
            crefs += self.get_delegated_from()

        self.setDelegatedFrom(list(set(crefs)))

    def delegate_to(self, delegatees):
        self.set_participant_status("DELEGATED")

        crefs = []
        if not isinstance(delegatees, list):
            delegatees = [delegatees]

        for delegatee in delegatees:
            if not isinstance(delegatee, Attendee):
                raise ValueError, _("Not a valid attendee")
            else:
                crefs.append(delegatee.contactreference)

        if len(crefs) == 0:
            raise ValueError, _("No valid delegatee references found")
        else:
            crefs += self.get_delegated_to()

        self.setDelegatedTo(list(set(crefs)))

    def get_cutype(self):
        return self.cutype()

    def get_delegated_from(self):
        return self.delegatedFrom()

    def get_delegated_to(self):
        return self.delegatedTo()

    def get_email(self):
        return self.contactreference.get_email()

    def get_name(self):
        return self.contactreference.get_name()

    def get_displayname(self):
        name = self.contactreference.get_name()
        email = self.contactreference.get_email()
        return "%s <%s>" % (name, email) if name is not None else email

    def get_participant_status(self, translated=False):
        partstat = self.partStat()
        if translated:
            partstat_name_map = dict([(v, k) for (k, v) in self.participant_status_map.iteritems()])
            return partstat_name_map[partstat] if partstat_name_map.has_key(partstat) else 'UNKNOWN'
        return partstat

    def get_role(self):
        return self.role()

    def get_rsvp(self):
        return self.rsvp()

    def set_cutype(self, cutype):
        if cutype in self.cutype_map.keys():
            self.setCutype(self.cutype_map[cutype])
        elif cutype in self.cutype_map.values():
            self.setCutype(cutype)
        else:
            raise InvalidAttendeeCutypeError, _("Invalid cutype %r") % (cutype)

    def set_name(self, name):
        self.contactreference.set_name(name)
        self.setContact(self.contactreference)

    def set_participant_status(self, participant_status):
        if participant_status in self.participant_status_map.keys():
            self.setPartStat(self.participant_status_map[participant_status])
        elif participant_status in self.participant_status_map.values():
            self.setPartStat(participant_status)
        else:
            raise InvalidAttendeeParticipantStatusError, _("Invalid participant status %r") % (participant_status)

    def set_role(self, role):
        if role in self.role_map.keys():
            self.setRole(self.role_map[role])
        elif role in self.role_map.values():
            self.setRole(role)
        else:
            raise InvalidAttendeeRoleError, _("Invalid role %r") % (role)

    def set_rsvp(self, rsvp):
        self.setRSVP(rsvp)

    def __str__(self):
        return self.email

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

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

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

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