1 #!/usr/bin/env python 2 3 """ 4 Common resource functionality for Web calendar clients. 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 datetime import datetime 23 from imiptools.client import Client 24 from imiptools.data import get_uri, uri_values 25 from imiptools.dates import get_recurrence_start_point 26 from imiptools.period import remove_period, remove_affected_period 27 from imipweb.env import CGIEnvironment 28 import babel.dates 29 import imip_store 30 import markup 31 32 class Resource(Client): 33 34 "A Web application resource and calendar client." 35 36 def __init__(self, resource=None): 37 self.encoding = "utf-8" 38 self.env = CGIEnvironment(self.encoding) 39 40 user = self.env.get_user() 41 Client.__init__(self, user and get_uri(user) or None) 42 43 self.locale = None 44 self.requests = None 45 46 self.out = resource and resource.out or self.env.get_output() 47 self.page = resource and resource.page or markup.page() 48 self.html_ids = None 49 50 self.store = imip_store.FileStore() 51 self.objects = {} 52 53 try: 54 self.publisher = imip_store.FilePublisher() 55 except OSError: 56 self.publisher = None 57 58 # Presentation methods. 59 60 def new_page(self, title): 61 self.page.init(title=title, charset=self.encoding, css=self.env.new_url("styles.css")) 62 self.html_ids = set() 63 64 def status(self, code, message): 65 self.header("Status", "%s %s" % (code, message)) 66 67 def header(self, header, value): 68 print >>self.out, "%s: %s" % (header, value) 69 70 def no_user(self): 71 self.status(403, "Forbidden") 72 self.new_page(title="Forbidden") 73 self.page.p("You are not logged in and thus cannot access scheduling requests.") 74 75 def no_page(self): 76 self.status(404, "Not Found") 77 self.new_page(title="Not Found") 78 self.page.p("No page is provided at the given address.") 79 80 def redirect(self, url): 81 self.status(302, "Redirect") 82 self.header("Location", url) 83 self.new_page(title="Redirect") 84 self.page.p("Redirecting to: %s" % url) 85 86 def link_to(self, uid, recurrenceid=None): 87 if recurrenceid: 88 return self.env.new_url("/".join([uid, recurrenceid])) 89 else: 90 return self.env.new_url(uid) 91 92 # Control naming helpers. 93 94 def element_identifier(self, name, index=None): 95 return index is not None and "%s-%d" % (name, index) or name 96 97 def element_name(self, name, suffix, index=None): 98 return index is not None and "%s-%s" % (name, suffix) or name 99 100 def element_enable(self, index=None): 101 return index is not None and str(index) or "enable" 102 103 # Access to objects. 104 105 def _get_identifiers(self, path_info): 106 parts = path_info.lstrip("/").split("/") 107 if len(parts) == 1: 108 return parts[0], None 109 else: 110 return parts[:2] 111 112 def _get_object(self, uid, recurrenceid=None): 113 if self.objects.has_key((uid, recurrenceid)): 114 return self.objects[(uid, recurrenceid)] 115 116 obj = self.objects[(uid, recurrenceid)] = self.get_stored_object(uid, recurrenceid) 117 return obj 118 119 def _get_recurrences(self, uid): 120 return self.store.get_recurrences(self.user, uid) 121 122 def _get_active_recurrences(self, uid): 123 return self.store.get_active_recurrences(self.user, uid) 124 125 def _get_requests(self): 126 if self.requests is None: 127 self.requests = self.store.get_requests(self.user) 128 return self.requests 129 130 def _get_request_summary(self): 131 132 "Return a list of periods comprising the request summary." 133 134 summary = [] 135 136 for uid, recurrenceid in self._get_requests(): 137 obj = self.get_stored_object(uid, recurrenceid) 138 if obj: 139 recurrenceids = self._get_active_recurrences(uid) 140 141 # Obtain only active periods, not those replaced by redefined 142 # recurrences, converting to free/busy periods. 143 144 for p in obj.get_active_periods(recurrenceids, self.get_tzid(), self.get_window_end()): 145 summary.append(obj.get_freebusy_period(p)) 146 147 return summary 148 149 # Preference methods. 150 151 def get_user_locale(self): 152 if not self.locale: 153 self.locale = self.get_preferences().get("LANG", "en") 154 return self.locale 155 156 # Prettyprinting of dates and times. 157 158 def format_date(self, dt, format): 159 return self._format_datetime(babel.dates.format_date, dt, format) 160 161 def format_time(self, dt, format): 162 return self._format_datetime(babel.dates.format_time, dt, format) 163 164 def format_datetime(self, dt, format): 165 return self._format_datetime( 166 isinstance(dt, datetime) and babel.dates.format_datetime or babel.dates.format_date, 167 dt, format) 168 169 def _format_datetime(self, fn, dt, format): 170 return fn(dt, format=format, locale=self.get_user_locale()) 171 172 # Data management methods. 173 174 def remove_request(self, uid, recurrenceid=None): 175 return self.store.dequeue_request(self.user, uid, recurrenceid) 176 177 def remove_event(self, uid, recurrenceid=None): 178 return self.store.remove_event(self.user, uid, recurrenceid) 179 180 def update_freebusy(self, uid, recurrenceid, obj): 181 182 """ 183 Update stored free/busy details for the event with the given 'uid' and 184 'recurrenceid' having a representation of 'obj'. 185 """ 186 187 is_only_organiser = self.user not in uri_values(obj.get_values("ATTENDEE")) 188 189 freebusy = self.store.get_freebusy(self.user) 190 191 Client.update_freebusy(self, freebusy, self.get_periods(obj), 192 is_only_organiser and "ORG" or obj.get_value("TRANSP"), 193 uid, recurrenceid, 194 obj.get_value("SUMMARY"), 195 obj.get_value("ORGANIZER")) 196 197 # Subtract any recurrences from the free/busy details of a parent 198 # object. 199 200 for recurrenceid in self._get_recurrences(uid): 201 remove_affected_period(freebusy, uid, obj.get_recurrence_start_point(recurrenceid, self.get_tzid())) 202 203 self.store.set_freebusy(self.user, freebusy) 204 self.publish_freebusy(freebusy) 205 206 # Update free/busy provider information if the event may recur 207 # indefinitely. 208 209 if obj.possibly_recurring_indefinitely(): 210 self.store.append_freebusy_provider(self.user, obj) 211 212 def remove_from_freebusy(self, uid, recurrenceid=None): 213 freebusy = self.store.get_freebusy(self.user) 214 remove_period(freebusy, uid, recurrenceid) 215 self.store.set_freebusy(self.user, freebusy) 216 self.publish_freebusy(freebusy) 217 218 # Update free/busy provider information if the event may recur 219 # indefinitely. 220 221 if obj.possibly_recurring_indefinitely(): 222 self.store.remove_freebusy_provider(self.user, obj) 223 224 def publish_freebusy(self, freebusy): 225 226 "Publish the details if configured to share them." 227 228 if self.publisher and self.is_sharing(): 229 self.publisher.set_freebusy(self.user, freebusy) 230 231 # vim: tabstop=4 expandtab shiftwidth=4