# HG changeset patch # User Paul Boddie # Date 1416936390 -3600 # Node ID 23b164f98f66a2c6f1eb05ec9f8ab607d05a513d # Parent 3a1b5b29938ca355168b14f861085c1eab44c928 Moved common free/busy processing into a separate mix-in. diff -r 3a1b5b29938c -r 23b164f98f66 imiptools/content.py --- a/imiptools/content.py Tue Nov 25 18:25:44 2014 +0100 +++ b/imiptools/content.py Tue Nov 25 18:26:30 2014 +0100 @@ -292,6 +292,9 @@ return to_dict(obj) finally: f.close() + + # NOTE: Handle parse errors properly. + except (ParseError, ValueError): pass diff -r 3a1b5b29938c -r 23b164f98f66 imiptools/handlers/common.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imiptools/handlers/common.py Tue Nov 25 18:26:30 2014 +0100 @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +""" +Common handler functionality for different entities. +""" + +from imiptools.content import to_part + +class CommonFreebusy: + + "Common free/busy mix-in." + + def request(self): + + """ + Respond to a request by preparing a reply containing free/busy + information for each indicated attendee. + """ + + oa = self.require_organiser_and_attendees() + if not oa: + return None + + (organiser, organiser_attr), attendees = organiser_item, attendees = oa + + # Validate the organiser, ignoring spoofed requests. + + if not self.validate_identities([organiser_item]): + return None + + # Construct an appropriate fragment. + + calendar = [] + cwrite = calendar.append + + # Get the details for each attendee. + + for attendee, attendee_attr in attendees.items(): + freebusy = self.store.get_freebusy(attendee) + + record = [] + rwrite = record.append + + rwrite(("ORGANIZER", organiser_attr, organiser)) + rwrite(("ATTENDEE", attendee_attr, attendee)) + rwrite(("UID", {}, self.uid)) + + if freebusy: + for start, end, uid in freebusy: + rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end])) + + cwrite(("VFREEBUSY", {}, record)) + + # Return the reply. + + return "REPLY", to_part("REPLY", calendar) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 3a1b5b29938c -r 23b164f98f66 imiptools/handlers/person.py --- a/imiptools/handlers/person.py Tue Nov 25 18:25:44 2014 +0100 +++ b/imiptools/handlers/person.py Tue Nov 25 18:26:30 2014 +0100 @@ -7,6 +7,7 @@ from email.mime.text import MIMEText from imiptools.config import MANAGER_PATH, MANAGER_URL from imiptools.content import Handler, to_part +from imiptools.handlers.common import CommonFreebusy from socket import gethostname from vCalendar import to_node @@ -150,7 +151,7 @@ url = "%s/%s" % (get_manager_url().rstrip("/"), self.uid) return "REQUEST", MIMEText("A request has been queued and can be viewed here: %s" % url) -class Freebusy(PersonHandler): +class Freebusy(PersonHandler, CommonFreebusy): "A free/busy handler." @@ -176,46 +177,9 @@ information for each indicated attendee. """ - # NOTE: This is currently the same as the resource handler but should be - # NOTE: subject to policy/preferences. - - oa = self.require_organiser_and_attendees() - if not oa: - return None - - (organiser, organiser_attr), attendees = organiser_item, attendees = oa - - # Validate the organiser, ignoring spoofed requests. - - if not self.validate_identities([organiser_item]): - return None - - # Construct an appropriate fragment. - - calendar = [] - cwrite = calendar.append + # NOTE: This should be subject to policy/preferences. - # Get the details for each attendee. - - for attendee, attendee_attr in attendees.items(): - freebusy = self.store.get_freebusy(attendee) - - record = [] - rwrite = record.append - - rwrite(("ORGANIZER", organiser_attr, organiser)) - rwrite(("ATTENDEE", attendee_attr, attendee)) - rwrite(("UID", {}, self.uid)) - - if freebusy: - for start, end, uid in freebusy: - rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end])) - - cwrite(("VFREEBUSY", {}, record)) - - # Return the reply. - - return "REPLY", to_part("REPLY", calendar) + return CommonFreebusy.request(self) class Journal(PersonHandler): diff -r 3a1b5b29938c -r 23b164f98f66 imiptools/handlers/resource.py --- a/imiptools/handlers/resource.py Tue Nov 25 18:25:44 2014 +0100 +++ b/imiptools/handlers/resource.py Tue Nov 25 18:26:30 2014 +0100 @@ -5,6 +5,7 @@ """ from imiptools.content import Handler, format_datetime, get_address, get_uri, to_part +from imiptools.handlers.common import CommonFreebusy from vCalendar import to_node class Event(Handler): @@ -100,7 +101,7 @@ return "REPLY", to_part("REPLY", calendar) -class Freebusy(Handler): +class Freebusy(Handler, CommonFreebusy): "A free/busy handler." @@ -113,50 +114,7 @@ pass - def request(self): - - """ - Respond to a request by preparing a reply containing free/busy - information for each indicated attendee. - """ - - oa = self.require_organiser_and_attendees() - if not oa: - return None - - (organiser, organiser_attr), attendees = organiser_item, attendees = oa - - # Validate the organiser, ignoring spoofed requests. - - if not self.validate_identities([organiser_item]): - return None - - # Construct an appropriate fragment. - - calendar = [] - cwrite = calendar.append - - # Get the details for each attendee. - - for attendee, attendee_attr in attendees.items(): - freebusy = self.store.get_freebusy(attendee) - - record = [] - rwrite = record.append - - rwrite(("ORGANIZER", organiser_attr, organiser)) - rwrite(("ATTENDEE", attendee_attr, attendee)) - rwrite(("UID", {}, self.uid)) - - if freebusy: - for start, end, uid in freebusy: - rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end])) - - cwrite(("VFREEBUSY", {}, record)) - - # Return the reply. - - return "REPLY", to_part("REPLY", calendar) + # request provided by CommonFreeBusy.request class Journal(Handler):