1 #!/usr/bin/env python 2 3 """ 4 Common calendar client utilities. 5 6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from imiptools.data import get_window_end, uri_items 23 from imiptools.dates import get_default_timezone 24 from imiptools.profile import Preferences 25 26 def update_attendees(obj, added, removed): 27 28 """ 29 Update the attendees in 'obj' with the given 'added' and 'removed' 30 attendee lists. A list is returned containing the attendees whose 31 attendance should be cancelled. 32 """ 33 34 to_cancel = [] 35 36 if added or removed: 37 attendees = uri_items(obj.get_items("ATTENDEE") or []) 38 sequence = obj.get_value("SEQUENCE") 39 40 if removed: 41 remaining = [] 42 43 for attendee, attendee_attr in attendees: 44 if attendee in removed: 45 46 # Without a sequence number, assume that the event has not 47 # been published and that attendees can be silently removed. 48 49 if sequence is not None: 50 to_cancel.append((attendee, attendee_attr)) 51 else: 52 remaining.append((attendee, attendee_attr)) 53 54 attendees = remaining 55 56 if added: 57 for attendee in added: 58 attendees.append((attendee, {"PARTSTAT" : "NEEDS-ACTION", "RSVP" : "TRUE"})) 59 60 obj["ATTENDEE"] = attendees 61 62 return to_cancel 63 64 class Client: 65 66 "Common handler and manager methods." 67 68 def __init__(self, user): 69 self.user = user 70 self.preferences = None 71 72 def get_preferences(self): 73 if not self.preferences: 74 self.preferences = Preferences(self.user) 75 return self.preferences 76 77 def get_tzid(self): 78 prefs = self.get_preferences() 79 return prefs.get("TZID") or get_default_timezone() 80 81 def get_window_size(self): 82 prefs = self.get_preferences() 83 try: 84 return int(prefs.get("window_size")) 85 except (TypeError, ValueError): 86 return 100 87 88 def get_window_end(self): 89 return get_window_end(self.get_tzid(), self.get_window_size()) 90 91 def is_sharing(self): 92 return self.get_preferences().get("freebusy_sharing") == "share" 93 94 def is_bundling(self): 95 return self.get_preferences().get("freebusy_bundling") == "always" 96 97 # vim: tabstop=4 expandtab shiftwidth=4