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 FreeBusyPeriod, 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_requests(self): 123 if self.requests is None: 124 cancellations = self.store.get_cancellations(self.user) 125 requests = set(self.store.get_requests(self.user)) 126 self.requests = requests.difference(cancellations) 127 return self.requests 128 129 def _get_request_summary(self): 130 131 "Return a list of periods comprising the request summary." 132 133 summary = [] 134 135 for uid, recurrenceid in self._get_requests(): 136 obj = self.get_stored_object(uid, recurrenceid) 137 if obj: 138 recurrenceids = self._get_recurrences(uid) 139 140 # Obtain only active periods, not those replaced by redefined 141 # recurrences. 142 143 for p in obj.get_active_periods(recurrenceids, self.get_tzid(), self.get_window_end()): 144 145 # Convert the periods to more substantial free/busy items. 146 147 summary.append( 148 FreeBusyPeriod( 149 p.get_start(), 150 p.get_end(), 151 uid, 152 obj.get_value("TRANSP"), 153 recurrenceid, 154 obj.get_value("SUMMARY"), 155 obj.get_value("ORGANIZER"), 156 p.tzid 157 )) 158 return summary 159 160 # Preference methods. 161 162 def get_user_locale(self): 163 if not self.locale: 164 self.locale = self.get_preferences().get("LANG", "en") 165 return self.locale 166 167 # Prettyprinting of dates and times. 168 169 def format_date(self, dt, format): 170 return self._format_datetime(babel.dates.format_date, dt, format) 171 172 def format_time(self, dt, format): 173 return self._format_datetime(babel.dates.format_time, dt, format) 174 175 def format_datetime(self, dt, format): 176 return self._format_datetime( 177 isinstance(dt, datetime) and babel.dates.format_datetime or babel.dates.format_date, 178 dt, format) 179 180 def _format_datetime(self, fn, dt, format): 181 return fn(dt, format=format, locale=self.get_user_locale()) 182 183 # Data management methods. 184 185 def remove_request(self, uid, recurrenceid=None): 186 return self.store.dequeue_request(self.user, uid, recurrenceid) 187 188 def remove_event(self, uid, recurrenceid=None): 189 return self.store.remove_event(self.user, uid, recurrenceid) 190 191 def update_freebusy(self, uid, recurrenceid, obj): 192 193 """ 194 Update stored free/busy details for the event with the given 'uid' and 195 'recurrenceid' having a representation of 'obj'. 196 """ 197 198 is_only_organiser = self.user not in uri_values(obj.get_values("ATTENDEE")) 199 200 freebusy = self.store.get_freebusy(self.user) 201 202 Client.update_freebusy(self, freebusy, self.get_periods(obj), 203 is_only_organiser and "ORG" or obj.get_value("TRANSP"), 204 uid, recurrenceid, 205 obj.get_value("SUMMARY"), 206 obj.get_value("ORGANIZER")) 207 208 # Subtract any recurrences from the free/busy details of a parent 209 # object. 210 211 for recurrenceid in self._get_recurrences(uid): 212 remove_affected_period(freebusy, uid, obj.get_recurrence_start_point(recurrenceid, self.get_tzid())) 213 214 self.store.set_freebusy(self.user, freebusy) 215 self.publish_freebusy(freebusy) 216 217 def remove_from_freebusy(self, uid, recurrenceid=None): 218 freebusy = self.store.get_freebusy(self.user) 219 remove_period(freebusy, uid, recurrenceid) 220 self.store.set_freebusy(self.user, freebusy) 221 self.publish_freebusy(freebusy) 222 223 def publish_freebusy(self, freebusy): 224 225 "Publish the details if configured to share them." 226 227 if self.publisher and self.is_sharing(): 228 self.publisher.set_freebusy(self.user, freebusy) 229 230 # vim: tabstop=4 expandtab shiftwidth=4