imip-agent

Annotated imiptools/handlers/common.py

421:f658ca7505b2
2015-03-22 Paul Boddie Moved methods around.
paul@108 1
#!/usr/bin/env python
paul@108 2
paul@108 3
"""
paul@108 4
Common handler functionality for different entities.
paul@146 5
paul@146 6
Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>
paul@146 7
paul@146 8
This program is free software; you can redistribute it and/or modify it under
paul@146 9
the terms of the GNU General Public License as published by the Free Software
paul@146 10
Foundation; either version 3 of the License, or (at your option) any later
paul@146 11
version.
paul@146 12
paul@146 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@146 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@146 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@146 16
details.
paul@146 17
paul@146 18
You should have received a copy of the GNU General Public License along with
paul@146 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@108 20
"""
paul@108 21
paul@292 22
from imiptools.data import get_address, get_uri, make_freebusy, to_part
paul@327 23
from imiptools.dates import format_datetime
paul@108 24
paul@222 25
class CommonFreebusy:
paul@108 26
paul@222 27
    "Common free/busy mix-in."
paul@181 28
paul@222 29
    def request(self):
paul@181 30
paul@181 31
        """
paul@222 32
        Respond to a request by preparing a reply containing free/busy
paul@222 33
        information for each indicated attendee.
paul@181 34
        """
paul@181 35
paul@108 36
        oa = self.require_organiser_and_attendees()
paul@108 37
        if not oa:
paul@228 38
            return
paul@108 39
paul@181 40
        (organiser, organiser_attr), attendees = oa
paul@108 41
paul@181 42
        # Get the details for each attendee.
paul@108 43
paul@222 44
        responses = []
paul@222 45
        rwrite = responses.append
paul@222 46
paul@222 47
        # For replies, the organiser and attendee are preserved.
paul@108 48
paul@181 49
        for attendee, attendee_attr in attendees.items():
paul@222 50
            freebusy = self.store.get_freebusy(attendee)
paul@292 51
paul@292 52
            # Indicate the actual sender of the reply.
paul@292 53
paul@292 54
            if self.messenger:
paul@292 55
                attendee_attr["SENT-BY"] = get_uri(self.messenger.sender)
paul@292 56
paul@327 57
            dtstart = format_datetime(self.obj.get_utc_datetime("DTSTART"))
paul@327 58
            dtend = format_datetime(self.obj.get_utc_datetime("DTEND"))
paul@327 59
paul@327 60
            rwrite(make_freebusy(freebusy, self.uid, organiser, organiser_attr, attendee, attendee_attr, dtstart, dtend))
paul@183 61
paul@183 62
        # Return the reply.
paul@183 63
paul@228 64
        self.add_result("REPLY", [get_address(organiser)], to_part("REPLY", responses))
paul@183 65
paul@108 66
# vim: tabstop=4 expandtab shiftwidth=4