paul@1083 | 1 | #!/usr/bin/env python |
paul@1083 | 2 | |
paul@1083 | 3 | """ |
paul@1083 | 4 | A database store of calendar data. |
paul@1083 | 5 | |
paul@1083 | 6 | Copyright (C) 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@1083 | 7 | |
paul@1083 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1083 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1083 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1083 | 11 | version. |
paul@1083 | 12 | |
paul@1083 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1083 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1083 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1083 | 16 | details. |
paul@1083 | 17 | |
paul@1083 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1083 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1083 | 20 | """ |
paul@1083 | 21 | |
paul@1083 | 22 | from imiptools.stores import StoreBase, JournalBase |
paul@1083 | 23 | |
paul@1083 | 24 | from datetime import datetime |
paul@1083 | 25 | from imiptools.data import parse_string, to_string |
paul@1083 | 26 | from imiptools.dates import format_datetime, get_datetime, to_timezone |
paul@1083 | 27 | from imiptools.period import FreeBusyPeriod, FreeBusyDatabaseCollection |
paul@1083 | 28 | from imiptools.sql import DatabaseOperations |
paul@1083 | 29 | |
paul@1083 | 30 | class DatabaseStoreBase: |
paul@1083 | 31 | |
paul@1083 | 32 | "A database store supporting user-specific locking." |
paul@1083 | 33 | |
paul@1083 | 34 | def acquire_lock(self, user, timeout=None): |
paul@1083 | 35 | FileBase.acquire_lock(self, timeout, user) |
paul@1083 | 36 | |
paul@1083 | 37 | def release_lock(self, user): |
paul@1083 | 38 | FileBase.release_lock(self, user) |
paul@1083 | 39 | |
paul@1083 | 40 | class DatabaseStore(DatabaseStoreBase, StoreBase, DatabaseOperations): |
paul@1083 | 41 | |
paul@1083 | 42 | "A database store of tabular free/busy data and objects." |
paul@1083 | 43 | |
paul@1083 | 44 | def __init__(self, connection, paramstyle=None): |
paul@1083 | 45 | DatabaseOperations.__init__(self, paramstyle=paramstyle) |
paul@1083 | 46 | self.connection = connection |
paul@1083 | 47 | self.cursor = connection.cursor() |
paul@1083 | 48 | |
paul@1083 | 49 | # User discovery. |
paul@1083 | 50 | |
paul@1083 | 51 | def get_users(self): |
paul@1083 | 52 | |
paul@1083 | 53 | "Return a list of users." |
paul@1083 | 54 | |
paul@1083 | 55 | query = "select distinct store_user from freebusy" |
paul@1083 | 56 | self.cursor.execute(query) |
paul@1083 | 57 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 58 | |
paul@1083 | 59 | # Event and event metadata access. |
paul@1083 | 60 | |
paul@1083 | 61 | def get_events(self, user): |
paul@1083 | 62 | |
paul@1083 | 63 | "Return a list of event identifiers." |
paul@1083 | 64 | |
paul@1083 | 65 | columns = ["store_user", "status"] |
paul@1083 | 66 | values = [user, "active"] |
paul@1083 | 67 | |
paul@1083 | 68 | query, values = self.get_query( |
paul@1083 | 69 | "select object_uid from objects :condition", |
paul@1083 | 70 | columns, values) |
paul@1083 | 71 | |
paul@1083 | 72 | self.cursor.execute(query, values) |
paul@1083 | 73 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 74 | |
paul@1083 | 75 | def get_all_events(self, user): |
paul@1083 | 76 | |
paul@1083 | 77 | "Return a set of (uid, recurrenceid) tuples for all events." |
paul@1083 | 78 | |
paul@1083 | 79 | query, values = self.get_query( |
paul@1083 | 80 | "select object_uid, null as object_recurrenceid from objects :condition " |
paul@1083 | 81 | "union all " |
paul@1083 | 82 | "select object_uid, object_recurrenceid from recurrences :condition", |
paul@1083 | 83 | ["store_user"], [user]) |
paul@1083 | 84 | |
paul@1083 | 85 | self.cursor.execute(query, values) |
paul@1083 | 86 | return self.cursor.fetchall() |
paul@1083 | 87 | |
paul@1083 | 88 | def get_event_table(self, recurrenceid=None, dirname=None): |
paul@1083 | 89 | |
paul@1083 | 90 | "Get the table providing events for any specified 'dirname'." |
paul@1083 | 91 | |
paul@1083 | 92 | if recurrenceid: |
paul@1083 | 93 | return self.get_recurrence_table(dirname) |
paul@1083 | 94 | else: |
paul@1083 | 95 | return self.get_complete_event_table(dirname) |
paul@1083 | 96 | |
paul@1083 | 97 | def get_event_table_filters(self, dirname=None): |
paul@1083 | 98 | |
paul@1083 | 99 | "Get filter details for any specified 'dirname'." |
paul@1083 | 100 | |
paul@1083 | 101 | if dirname == "cancellations": |
paul@1083 | 102 | return ["status"], ["cancelled"] |
paul@1083 | 103 | else: |
paul@1083 | 104 | return [], [] |
paul@1083 | 105 | |
paul@1083 | 106 | def get_event(self, user, uid, recurrenceid=None, dirname=None): |
paul@1083 | 107 | |
paul@1083 | 108 | """ |
paul@1083 | 109 | Get the event for the given 'user' with the given 'uid'. If |
paul@1083 | 110 | the optional 'recurrenceid' is specified, a specific instance or |
paul@1083 | 111 | occurrence of an event is returned. |
paul@1083 | 112 | """ |
paul@1083 | 113 | |
paul@1083 | 114 | table = self.get_event_table(recurrenceid, dirname) |
paul@1083 | 115 | columns, values = self.get_event_table_filters(dirname) |
paul@1083 | 116 | |
paul@1083 | 117 | if recurrenceid: |
paul@1083 | 118 | columns += ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 119 | values += [user, uid, recurrenceid] |
paul@1083 | 120 | else: |
paul@1083 | 121 | columns += ["store_user", "object_uid"] |
paul@1083 | 122 | values += [user, uid] |
paul@1083 | 123 | |
paul@1083 | 124 | query, values = self.get_query( |
paul@1083 | 125 | "select object_text from %(table)s :condition" % { |
paul@1083 | 126 | "table" : table |
paul@1083 | 127 | }, |
paul@1083 | 128 | columns, values) |
paul@1083 | 129 | |
paul@1083 | 130 | self.cursor.execute(query, values) |
paul@1083 | 131 | result = self.cursor.fetchone() |
paul@1083 | 132 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 133 | |
paul@1083 | 134 | def get_complete_event_table(self, dirname=None): |
paul@1083 | 135 | |
paul@1083 | 136 | "Get the table providing events for any specified 'dirname'." |
paul@1083 | 137 | |
paul@1083 | 138 | if dirname == "counters": |
paul@1083 | 139 | return "countered_objects" |
paul@1083 | 140 | else: |
paul@1083 | 141 | return "objects" |
paul@1083 | 142 | |
paul@1083 | 143 | def get_complete_event(self, user, uid): |
paul@1083 | 144 | |
paul@1083 | 145 | "Get the event for the given 'user' with the given 'uid'." |
paul@1083 | 146 | |
paul@1083 | 147 | columns = ["store_user", "object_uid"] |
paul@1083 | 148 | values = [user, uid] |
paul@1083 | 149 | |
paul@1083 | 150 | query, values = self.get_query( |
paul@1083 | 151 | "select object_text from objects :condition", |
paul@1083 | 152 | columns, values) |
paul@1083 | 153 | |
paul@1083 | 154 | self.cursor.execute(query, values) |
paul@1083 | 155 | result = self.cursor.fetchone() |
paul@1083 | 156 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 157 | |
paul@1083 | 158 | def set_complete_event(self, user, uid, node): |
paul@1083 | 159 | |
paul@1083 | 160 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@1083 | 161 | |
paul@1083 | 162 | columns = ["store_user", "object_uid"] |
paul@1083 | 163 | values = [user, uid] |
paul@1083 | 164 | setcolumns = ["object_text", "status"] |
paul@1083 | 165 | setvalues = [to_string(node, "utf-8"), "active"] |
paul@1083 | 166 | |
paul@1083 | 167 | query, values = self.get_query( |
paul@1083 | 168 | "update objects :set :condition", |
paul@1083 | 169 | columns, values, setcolumns, setvalues) |
paul@1083 | 170 | |
paul@1083 | 171 | self.cursor.execute(query, values) |
paul@1083 | 172 | |
paul@1083 | 173 | if self.cursor.rowcount > 0 or self.get_complete_event(user, uid): |
paul@1083 | 174 | return True |
paul@1083 | 175 | |
paul@1083 | 176 | columns = ["store_user", "object_uid", "object_text", "status"] |
paul@1083 | 177 | values = [user, uid, to_string(node, "utf-8"), "active"] |
paul@1083 | 178 | |
paul@1083 | 179 | query, values = self.get_query( |
paul@1083 | 180 | "insert into objects (:columns) values (:values)", |
paul@1083 | 181 | columns, values) |
paul@1083 | 182 | |
paul@1083 | 183 | self.cursor.execute(query, values) |
paul@1083 | 184 | return True |
paul@1083 | 185 | |
paul@1083 | 186 | def remove_parent_event(self, user, uid): |
paul@1083 | 187 | |
paul@1083 | 188 | "Remove the parent event for 'user' having the given 'uid'." |
paul@1083 | 189 | |
paul@1083 | 190 | columns = ["store_user", "object_uid"] |
paul@1083 | 191 | values = [user, uid] |
paul@1083 | 192 | |
paul@1083 | 193 | query, values = self.get_query( |
paul@1083 | 194 | "delete from objects :condition", |
paul@1083 | 195 | columns, values) |
paul@1083 | 196 | |
paul@1083 | 197 | self.cursor.execute(query, values) |
paul@1083 | 198 | return self.cursor.rowcount > 0 |
paul@1083 | 199 | |
paul@1083 | 200 | def get_active_recurrences(self, user, uid): |
paul@1083 | 201 | |
paul@1083 | 202 | """ |
paul@1083 | 203 | Get additional event instances for an event of the given 'user' with the |
paul@1083 | 204 | indicated 'uid'. Cancelled recurrences are not returned. |
paul@1083 | 205 | """ |
paul@1083 | 206 | |
paul@1083 | 207 | columns = ["store_user", "object_uid", "status"] |
paul@1083 | 208 | values = [user, uid, "active"] |
paul@1083 | 209 | |
paul@1083 | 210 | query, values = self.get_query( |
paul@1083 | 211 | "select object_recurrenceid from recurrences :condition", |
paul@1083 | 212 | columns, values) |
paul@1083 | 213 | |
paul@1083 | 214 | self.cursor.execute(query, values) |
paul@1083 | 215 | return [t[0] for t in self.cursor.fetchall() or []] |
paul@1083 | 216 | |
paul@1083 | 217 | def get_cancelled_recurrences(self, user, uid): |
paul@1083 | 218 | |
paul@1083 | 219 | """ |
paul@1083 | 220 | Get additional event instances for an event of the given 'user' with the |
paul@1083 | 221 | indicated 'uid'. Only cancelled recurrences are returned. |
paul@1083 | 222 | """ |
paul@1083 | 223 | |
paul@1083 | 224 | columns = ["store_user", "object_uid", "status"] |
paul@1083 | 225 | values = [user, uid, "cancelled"] |
paul@1083 | 226 | |
paul@1083 | 227 | query, values = self.get_query( |
paul@1083 | 228 | "select object_recurrenceid from recurrences :condition", |
paul@1083 | 229 | columns, values) |
paul@1083 | 230 | |
paul@1083 | 231 | self.cursor.execute(query, values) |
paul@1083 | 232 | return [t[0] for t in self.cursor.fetchall() or []] |
paul@1083 | 233 | |
paul@1083 | 234 | def get_recurrence_table(self, dirname=None): |
paul@1083 | 235 | |
paul@1083 | 236 | "Get the table providing recurrences for any specified 'dirname'." |
paul@1083 | 237 | |
paul@1083 | 238 | if dirname == "counters": |
paul@1083 | 239 | return "countered_recurrences" |
paul@1083 | 240 | else: |
paul@1083 | 241 | return "recurrences" |
paul@1083 | 242 | |
paul@1083 | 243 | def get_recurrence(self, user, uid, recurrenceid): |
paul@1083 | 244 | |
paul@1083 | 245 | """ |
paul@1083 | 246 | For the event of the given 'user' with the given 'uid', return the |
paul@1083 | 247 | specific recurrence indicated by the 'recurrenceid'. |
paul@1083 | 248 | """ |
paul@1083 | 249 | |
paul@1083 | 250 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 251 | values = [user, uid, recurrenceid] |
paul@1083 | 252 | |
paul@1083 | 253 | query, values = self.get_query( |
paul@1083 | 254 | "select object_text from recurrences :condition", |
paul@1083 | 255 | columns, values) |
paul@1083 | 256 | |
paul@1083 | 257 | self.cursor.execute(query, values) |
paul@1083 | 258 | result = self.cursor.fetchone() |
paul@1083 | 259 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 260 | |
paul@1083 | 261 | def set_recurrence(self, user, uid, recurrenceid, node): |
paul@1083 | 262 | |
paul@1083 | 263 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@1083 | 264 | |
paul@1083 | 265 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 266 | values = [user, uid, recurrenceid] |
paul@1083 | 267 | setcolumns = ["object_text", "status"] |
paul@1083 | 268 | setvalues = [to_string(node, "utf-8"), "active"] |
paul@1083 | 269 | |
paul@1083 | 270 | query, values = self.get_query( |
paul@1083 | 271 | "update recurrences :set :condition", |
paul@1083 | 272 | columns, values, setcolumns, setvalues) |
paul@1083 | 273 | |
paul@1083 | 274 | self.cursor.execute(query, values) |
paul@1083 | 275 | |
paul@1083 | 276 | if self.cursor.rowcount > 0 or self.get_recurrence(user, uid, recurrenceid): |
paul@1083 | 277 | return True |
paul@1083 | 278 | |
paul@1083 | 279 | columns = ["store_user", "object_uid", "object_recurrenceid", "object_text", "status"] |
paul@1083 | 280 | values = [user, uid, recurrenceid, to_string(node, "utf-8"), "active"] |
paul@1083 | 281 | |
paul@1083 | 282 | query, values = self.get_query( |
paul@1083 | 283 | "insert into recurrences (:columns) values (:values)", |
paul@1083 | 284 | columns, values) |
paul@1083 | 285 | |
paul@1083 | 286 | self.cursor.execute(query, values) |
paul@1083 | 287 | return True |
paul@1083 | 288 | |
paul@1083 | 289 | def remove_recurrence(self, user, uid, recurrenceid): |
paul@1083 | 290 | |
paul@1083 | 291 | """ |
paul@1083 | 292 | Remove a special recurrence from an event stored by 'user' having the |
paul@1083 | 293 | given 'uid' and 'recurrenceid'. |
paul@1083 | 294 | """ |
paul@1083 | 295 | |
paul@1083 | 296 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 297 | values = [user, uid, recurrenceid] |
paul@1083 | 298 | |
paul@1083 | 299 | query, values = self.get_query( |
paul@1083 | 300 | "delete from recurrences :condition", |
paul@1083 | 301 | columns, values) |
paul@1083 | 302 | |
paul@1083 | 303 | self.cursor.execute(query, values) |
paul@1083 | 304 | return True |
paul@1083 | 305 | |
paul@1083 | 306 | def remove_recurrences(self, user, uid): |
paul@1083 | 307 | |
paul@1083 | 308 | """ |
paul@1083 | 309 | Remove all recurrences for an event stored by 'user' having the given |
paul@1083 | 310 | 'uid'. |
paul@1083 | 311 | """ |
paul@1083 | 312 | |
paul@1083 | 313 | columns = ["store_user", "object_uid"] |
paul@1083 | 314 | values = [user, uid] |
paul@1083 | 315 | |
paul@1083 | 316 | query, values = self.get_query( |
paul@1083 | 317 | "delete from recurrences :condition", |
paul@1083 | 318 | columns, values) |
paul@1083 | 319 | |
paul@1083 | 320 | self.cursor.execute(query, values) |
paul@1083 | 321 | return True |
paul@1083 | 322 | |
paul@1083 | 323 | # Free/busy period providers, upon extension of the free/busy records. |
paul@1083 | 324 | |
paul@1083 | 325 | def _get_freebusy_providers(self, user): |
paul@1083 | 326 | |
paul@1083 | 327 | """ |
paul@1083 | 328 | Return the free/busy providers for the given 'user'. |
paul@1083 | 329 | |
paul@1083 | 330 | This function returns any stored datetime and a list of providers as a |
paul@1083 | 331 | 2-tuple. Each provider is itself a (uid, recurrenceid) tuple. |
paul@1083 | 332 | """ |
paul@1083 | 333 | |
paul@1083 | 334 | columns = ["store_user"] |
paul@1083 | 335 | values = [user] |
paul@1083 | 336 | |
paul@1083 | 337 | query, values = self.get_query( |
paul@1083 | 338 | "select object_uid, object_recurrenceid from freebusy_providers :condition", |
paul@1083 | 339 | columns, values) |
paul@1083 | 340 | |
paul@1083 | 341 | self.cursor.execute(query, values) |
paul@1083 | 342 | providers = self.cursor.fetchall() |
paul@1083 | 343 | |
paul@1083 | 344 | columns = ["store_user"] |
paul@1083 | 345 | values = [user] |
paul@1083 | 346 | |
paul@1083 | 347 | query, values = self.get_query( |
paul@1083 | 348 | "select start from freebusy_provider_datetimes :condition", |
paul@1083 | 349 | columns, values) |
paul@1083 | 350 | |
paul@1083 | 351 | self.cursor.execute(query, values) |
paul@1083 | 352 | result = self.cursor.fetchone() |
paul@1083 | 353 | dt_string = result and result[0] |
paul@1083 | 354 | |
paul@1083 | 355 | return dt_string, providers |
paul@1083 | 356 | |
paul@1083 | 357 | def _set_freebusy_providers(self, user, dt_string, t): |
paul@1083 | 358 | |
paul@1083 | 359 | "Set the given provider timestamp 'dt_string' and table 't'." |
paul@1083 | 360 | |
paul@1083 | 361 | # NOTE: Locking? |
paul@1083 | 362 | |
paul@1083 | 363 | columns = ["store_user"] |
paul@1083 | 364 | values = [user] |
paul@1083 | 365 | |
paul@1083 | 366 | query, values = self.get_query( |
paul@1083 | 367 | "delete from freebusy_providers :condition", |
paul@1083 | 368 | columns, values) |
paul@1083 | 369 | |
paul@1083 | 370 | self.cursor.execute(query, values) |
paul@1083 | 371 | |
paul@1083 | 372 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 373 | |
paul@1083 | 374 | for uid, recurrenceid in t: |
paul@1083 | 375 | values = [user, uid, recurrenceid] |
paul@1083 | 376 | |
paul@1083 | 377 | query, values = self.get_query( |
paul@1083 | 378 | "insert into freebusy_providers (:columns) values (:values)", |
paul@1083 | 379 | columns, values) |
paul@1083 | 380 | |
paul@1083 | 381 | self.cursor.execute(query, values) |
paul@1083 | 382 | |
paul@1083 | 383 | columns = ["store_user"] |
paul@1083 | 384 | values = [user] |
paul@1083 | 385 | setcolumns = ["start"] |
paul@1083 | 386 | setvalues = [dt_string] |
paul@1083 | 387 | |
paul@1083 | 388 | query, values = self.get_query( |
paul@1083 | 389 | "update freebusy_provider_datetimes :set :condition", |
paul@1083 | 390 | columns, values, setcolumns, setvalues) |
paul@1083 | 391 | |
paul@1083 | 392 | self.cursor.execute(query, values) |
paul@1083 | 393 | |
paul@1083 | 394 | if self.cursor.rowcount > 0: |
paul@1083 | 395 | return True |
paul@1083 | 396 | |
paul@1083 | 397 | columns = ["store_user", "start"] |
paul@1083 | 398 | values = [user, dt_string] |
paul@1083 | 399 | |
paul@1083 | 400 | query, values = self.get_query( |
paul@1083 | 401 | "insert into freebusy_provider_datetimes (:columns) values (:values)", |
paul@1083 | 402 | columns, values) |
paul@1083 | 403 | |
paul@1083 | 404 | self.cursor.execute(query, values) |
paul@1083 | 405 | return True |
paul@1083 | 406 | |
paul@1083 | 407 | # Free/busy period access. |
paul@1083 | 408 | |
paul@1083 | 409 | def get_freebusy(self, user, name=None, mutable=False): |
paul@1083 | 410 | |
paul@1083 | 411 | "Get free/busy details for the given 'user'." |
paul@1083 | 412 | |
paul@1083 | 413 | table = name or "freebusy" |
paul@1083 | 414 | return FreeBusyDatabaseCollection(self.cursor, table, ["store_user"], [user], mutable, self.paramstyle) |
paul@1083 | 415 | |
paul@1083 | 416 | def get_freebusy_for_other(self, user, other, mutable=False): |
paul@1083 | 417 | |
paul@1083 | 418 | "For the given 'user', get free/busy details for the 'other' user." |
paul@1083 | 419 | |
paul@1083 | 420 | table = "freebusy" |
paul@1083 | 421 | return FreeBusyDatabaseCollection(self.cursor, table, ["store_user", "other"], [user, other], mutable, self.paramstyle) |
paul@1083 | 422 | |
paul@1083 | 423 | def set_freebusy(self, user, freebusy, name=None): |
paul@1083 | 424 | |
paul@1083 | 425 | "For the given 'user', set 'freebusy' details." |
paul@1083 | 426 | |
paul@1083 | 427 | table = name or "freebusy" |
paul@1083 | 428 | |
paul@1083 | 429 | if not isinstance(freebusy, FreeBusyDatabaseCollection) or freebusy.table_name != table: |
paul@1083 | 430 | fbc = FreeBusyDatabaseCollection(self.cursor, table, ["store_user"], [user], True, self.paramstyle) |
paul@1083 | 431 | fbc += freebusy |
paul@1083 | 432 | |
paul@1083 | 433 | return True |
paul@1083 | 434 | |
paul@1083 | 435 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@1083 | 436 | |
paul@1083 | 437 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@1083 | 438 | |
paul@1083 | 439 | table = "freebusy" |
paul@1083 | 440 | |
paul@1083 | 441 | if not isinstance(freebusy, FreeBusyDatabaseCollection) or freebusy.table_name != table: |
paul@1083 | 442 | fbc = FreeBusyDatabaseCollection(self.cursor, table, ["store_user", "other"], [user, other], True, self.paramstyle) |
paul@1083 | 443 | fbc += freebusy |
paul@1083 | 444 | |
paul@1083 | 445 | return True |
paul@1083 | 446 | |
paul@1083 | 447 | # Tentative free/busy periods related to countering. |
paul@1083 | 448 | |
paul@1083 | 449 | def get_freebusy_offers(self, user, mutable=False): |
paul@1083 | 450 | |
paul@1083 | 451 | "Get free/busy offers for the given 'user'." |
paul@1083 | 452 | |
paul@1083 | 453 | # Expire old offers and save the collection if modified. |
paul@1083 | 454 | |
paul@1083 | 455 | now = format_datetime(to_timezone(datetime.utcnow(), "UTC")) |
paul@1083 | 456 | columns = ["store_user", "expires"] |
paul@1083 | 457 | values = [user, now] |
paul@1083 | 458 | |
paul@1083 | 459 | query, values = self.get_query( |
paul@1083 | 460 | "delete from freebusy_offers :condition", |
paul@1083 | 461 | columns, values) |
paul@1083 | 462 | |
paul@1083 | 463 | self.cursor.execute(query, values) |
paul@1083 | 464 | |
paul@1083 | 465 | return self.get_freebusy(user, "freebusy_offers", mutable) |
paul@1083 | 466 | |
paul@1083 | 467 | # Requests and counter-proposals. |
paul@1083 | 468 | |
paul@1083 | 469 | def get_requests(self, user): |
paul@1083 | 470 | |
paul@1083 | 471 | "Get requests for the given 'user'." |
paul@1083 | 472 | |
paul@1083 | 473 | columns = ["store_user"] |
paul@1083 | 474 | values = [user] |
paul@1083 | 475 | |
paul@1083 | 476 | query, values = self.get_query( |
paul@1083 | 477 | "select object_uid, object_recurrenceid from requests :condition", |
paul@1083 | 478 | columns, values) |
paul@1083 | 479 | |
paul@1083 | 480 | self.cursor.execute(query, values) |
paul@1083 | 481 | return self.cursor.fetchall() |
paul@1083 | 482 | |
paul@1083 | 483 | def set_requests(self, user, requests): |
paul@1083 | 484 | |
paul@1083 | 485 | "For the given 'user', set the list of queued 'requests'." |
paul@1083 | 486 | |
paul@1083 | 487 | # NOTE: Locking? |
paul@1083 | 488 | |
paul@1083 | 489 | columns = ["store_user"] |
paul@1083 | 490 | values = [user] |
paul@1083 | 491 | |
paul@1083 | 492 | query, values = self.get_query( |
paul@1083 | 493 | "delete from requests :condition", |
paul@1083 | 494 | columns, values) |
paul@1083 | 495 | |
paul@1083 | 496 | self.cursor.execute(query, values) |
paul@1083 | 497 | |
paul@1083 | 498 | for uid, recurrenceid, type in requests: |
paul@1083 | 499 | columns = ["store_user", "object_uid", "object_recurrenceid", "request_type"] |
paul@1083 | 500 | values = [user, uid, recurrenceid, type] |
paul@1083 | 501 | |
paul@1083 | 502 | query, values = self.get_query( |
paul@1083 | 503 | "insert into requests (:columns) values (:values)", |
paul@1083 | 504 | columns, values) |
paul@1083 | 505 | |
paul@1083 | 506 | self.cursor.execute(query, values) |
paul@1083 | 507 | |
paul@1083 | 508 | return True |
paul@1083 | 509 | |
paul@1083 | 510 | def set_request(self, user, uid, recurrenceid=None, type=None): |
paul@1083 | 511 | |
paul@1083 | 512 | """ |
paul@1083 | 513 | For the given 'user', set the queued 'uid' and 'recurrenceid', |
paul@1083 | 514 | indicating a request, along with any given 'type'. |
paul@1083 | 515 | """ |
paul@1083 | 516 | |
paul@1083 | 517 | columns = ["store_user", "object_uid", "object_recurrenceid", "request_type"] |
paul@1083 | 518 | values = [user, uid, recurrenceid, type] |
paul@1083 | 519 | |
paul@1083 | 520 | query, values = self.get_query( |
paul@1083 | 521 | "insert into requests (:columns) values (:values)", |
paul@1083 | 522 | columns, values) |
paul@1083 | 523 | |
paul@1083 | 524 | self.cursor.execute(query, values) |
paul@1083 | 525 | return True |
paul@1083 | 526 | |
paul@1083 | 527 | def get_counters(self, user, uid, recurrenceid=None): |
paul@1083 | 528 | |
paul@1083 | 529 | """ |
paul@1083 | 530 | For the given 'user', return a list of users from whom counter-proposals |
paul@1083 | 531 | have been received for the given 'uid' and optional 'recurrenceid'. |
paul@1083 | 532 | """ |
paul@1083 | 533 | |
paul@1083 | 534 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 535 | |
paul@1083 | 536 | if recurrenceid: |
paul@1083 | 537 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 538 | values = [user, uid, recurrenceid] |
paul@1083 | 539 | else: |
paul@1083 | 540 | columns = ["store_user", "object_uid"] |
paul@1083 | 541 | values = [user, uid] |
paul@1083 | 542 | |
paul@1083 | 543 | query, values = self.get_query( |
paul@1083 | 544 | "select other from %(table)s :condition" % { |
paul@1083 | 545 | "table" : table |
paul@1083 | 546 | }, |
paul@1083 | 547 | columns, values) |
paul@1083 | 548 | |
paul@1083 | 549 | self.cursor.execute(query, values) |
paul@1083 | 550 | return self.cursor.fetchall() |
paul@1083 | 551 | |
paul@1083 | 552 | def get_counter(self, user, other, uid, recurrenceid=None): |
paul@1083 | 553 | |
paul@1083 | 554 | """ |
paul@1083 | 555 | For the given 'user', return the counter-proposal from 'other' for the |
paul@1083 | 556 | given 'uid' and optional 'recurrenceid'. |
paul@1083 | 557 | """ |
paul@1083 | 558 | |
paul@1083 | 559 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 560 | |
paul@1083 | 561 | if recurrenceid: |
paul@1083 | 562 | columns = ["store_user", "other", "object_uid", "object_recurrenceid"] |
paul@1083 | 563 | values = [user, other, uid, recurrenceid] |
paul@1083 | 564 | else: |
paul@1083 | 565 | columns = ["store_user", "other", "object_uid"] |
paul@1083 | 566 | values = [user, other, uid] |
paul@1083 | 567 | |
paul@1083 | 568 | query, values = self.get_query( |
paul@1083 | 569 | "select object_text from %(table)s :condition" % { |
paul@1083 | 570 | "table" : table |
paul@1083 | 571 | }, |
paul@1083 | 572 | columns, values) |
paul@1083 | 573 | |
paul@1083 | 574 | self.cursor.execute(query, values) |
paul@1083 | 575 | result = self.cursor.fetchall() |
paul@1083 | 576 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 577 | |
paul@1083 | 578 | def set_counter(self, user, other, node, uid, recurrenceid=None): |
paul@1083 | 579 | |
paul@1083 | 580 | """ |
paul@1083 | 581 | For the given 'user', store a counter-proposal received from 'other' the |
paul@1083 | 582 | given 'node' representing that proposal for the given 'uid' and |
paul@1083 | 583 | 'recurrenceid'. |
paul@1083 | 584 | """ |
paul@1083 | 585 | |
paul@1083 | 586 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 587 | |
paul@1083 | 588 | columns = ["store_user", "other", "object_uid", "object_recurrenceid", "object_text"] |
paul@1083 | 589 | values = [user, other, uid, recurrenceid, to_string(node, "utf-8")] |
paul@1083 | 590 | |
paul@1083 | 591 | query, values = self.get_query( |
paul@1083 | 592 | "insert into %(table)s (:columns) values (:values)" % { |
paul@1083 | 593 | "table" : table |
paul@1083 | 594 | }, |
paul@1083 | 595 | columns, values) |
paul@1083 | 596 | |
paul@1083 | 597 | self.cursor.execute(query, values) |
paul@1083 | 598 | return True |
paul@1083 | 599 | |
paul@1083 | 600 | def remove_counters(self, user, uid, recurrenceid=None): |
paul@1083 | 601 | |
paul@1083 | 602 | """ |
paul@1083 | 603 | For the given 'user', remove all counter-proposals associated with the |
paul@1083 | 604 | given 'uid' and 'recurrenceid'. |
paul@1083 | 605 | """ |
paul@1083 | 606 | |
paul@1083 | 607 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 608 | |
paul@1083 | 609 | if recurrenceid: |
paul@1083 | 610 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 611 | values = [user, uid, recurrenceid] |
paul@1083 | 612 | else: |
paul@1083 | 613 | columns = ["store_user", "object_uid"] |
paul@1083 | 614 | values = [user, uid] |
paul@1083 | 615 | |
paul@1083 | 616 | query, values = self.get_query( |
paul@1083 | 617 | "delete from %(table)s :condition" % { |
paul@1083 | 618 | "table" : table |
paul@1083 | 619 | }, |
paul@1083 | 620 | columns, values) |
paul@1083 | 621 | |
paul@1083 | 622 | self.cursor.execute(query, values) |
paul@1083 | 623 | return True |
paul@1083 | 624 | |
paul@1083 | 625 | def remove_counter(self, user, other, uid, recurrenceid=None): |
paul@1083 | 626 | |
paul@1083 | 627 | """ |
paul@1083 | 628 | For the given 'user', remove any counter-proposal from 'other' |
paul@1083 | 629 | associated with the given 'uid' and 'recurrenceid'. |
paul@1083 | 630 | """ |
paul@1083 | 631 | |
paul@1083 | 632 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 633 | |
paul@1083 | 634 | if recurrenceid: |
paul@1083 | 635 | columns = ["store_user", "other", "object_uid", "object_recurrenceid"] |
paul@1083 | 636 | values = [user, other, uid, recurrenceid] |
paul@1083 | 637 | else: |
paul@1083 | 638 | columns = ["store_user", "other", "object_uid"] |
paul@1083 | 639 | values = [user, other, uid] |
paul@1083 | 640 | |
paul@1083 | 641 | query, values = self.get_query( |
paul@1083 | 642 | "delete from %(table)s :condition" % { |
paul@1083 | 643 | "table" : table |
paul@1083 | 644 | }, |
paul@1083 | 645 | columns, values) |
paul@1083 | 646 | |
paul@1083 | 647 | self.cursor.execute(query, values) |
paul@1083 | 648 | return True |
paul@1083 | 649 | |
paul@1083 | 650 | # Event cancellation. |
paul@1083 | 651 | |
paul@1083 | 652 | def cancel_event(self, user, uid, recurrenceid=None): |
paul@1083 | 653 | |
paul@1083 | 654 | """ |
paul@1083 | 655 | Cancel an event for 'user' having the given 'uid'. If the optional |
paul@1083 | 656 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@1083 | 657 | event is cancelled. |
paul@1083 | 658 | """ |
paul@1083 | 659 | |
paul@1083 | 660 | table = self.get_event_table(recurrenceid) |
paul@1083 | 661 | |
paul@1083 | 662 | if recurrenceid: |
paul@1083 | 663 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 664 | values = [user, uid, recurrenceid] |
paul@1083 | 665 | else: |
paul@1083 | 666 | columns = ["store_user", "object_uid"] |
paul@1083 | 667 | values = [user, uid] |
paul@1083 | 668 | |
paul@1083 | 669 | setcolumns = ["status"] |
paul@1083 | 670 | setvalues = ["cancelled"] |
paul@1083 | 671 | |
paul@1083 | 672 | query, values = self.get_query( |
paul@1083 | 673 | "update %(table)s :set :condition" % { |
paul@1083 | 674 | "table" : table |
paul@1083 | 675 | }, |
paul@1083 | 676 | columns, values, setcolumns, setvalues) |
paul@1083 | 677 | |
paul@1083 | 678 | self.cursor.execute(query, values) |
paul@1083 | 679 | return True |
paul@1083 | 680 | |
paul@1083 | 681 | def uncancel_event(self, user, uid, recurrenceid=None): |
paul@1083 | 682 | |
paul@1083 | 683 | """ |
paul@1083 | 684 | Uncancel an event for 'user' having the given 'uid'. If the optional |
paul@1083 | 685 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@1083 | 686 | event is uncancelled. |
paul@1083 | 687 | """ |
paul@1083 | 688 | |
paul@1083 | 689 | table = self.get_event_table(recurrenceid) |
paul@1083 | 690 | |
paul@1083 | 691 | if recurrenceid: |
paul@1083 | 692 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 693 | values = [user, uid, recurrenceid] |
paul@1083 | 694 | else: |
paul@1083 | 695 | columns = ["store_user", "object_uid"] |
paul@1083 | 696 | values = [user, uid] |
paul@1083 | 697 | |
paul@1083 | 698 | setcolumns = ["status"] |
paul@1083 | 699 | setvalues = ["active"] |
paul@1083 | 700 | |
paul@1083 | 701 | query, values = self.get_query( |
paul@1083 | 702 | "update %(table)s :set :condition" % { |
paul@1083 | 703 | "table" : table |
paul@1083 | 704 | }, |
paul@1083 | 705 | columns, values, setcolumns, setvalues) |
paul@1083 | 706 | |
paul@1083 | 707 | self.cursor.execute(query, values) |
paul@1083 | 708 | return True |
paul@1083 | 709 | |
paul@1083 | 710 | def remove_cancellation(self, user, uid, recurrenceid=None): |
paul@1083 | 711 | |
paul@1083 | 712 | """ |
paul@1083 | 713 | Remove a cancellation for 'user' for the event having the given 'uid'. |
paul@1083 | 714 | If the optional 'recurrenceid' is specified, a specific instance or |
paul@1083 | 715 | occurrence of an event is affected. |
paul@1083 | 716 | """ |
paul@1083 | 717 | |
paul@1083 | 718 | table = self.get_event_table(recurrenceid) |
paul@1083 | 719 | |
paul@1083 | 720 | if recurrenceid: |
paul@1083 | 721 | columns = ["store_user", "object_uid", "object_recurrenceid", "status"] |
paul@1083 | 722 | values = [user, uid, recurrenceid, "cancelled"] |
paul@1083 | 723 | else: |
paul@1083 | 724 | columns = ["store_user", "object_uid", "status"] |
paul@1083 | 725 | values = [user, uid, "cancelled"] |
paul@1083 | 726 | |
paul@1083 | 727 | query, values = self.get_query( |
paul@1083 | 728 | "delete from %(table)s :condition" % { |
paul@1083 | 729 | "table" : table |
paul@1083 | 730 | }, |
paul@1083 | 731 | columns, values) |
paul@1083 | 732 | |
paul@1083 | 733 | self.cursor.execute(query, values) |
paul@1083 | 734 | return True |
paul@1083 | 735 | |
paul@1083 | 736 | class DatabaseJournal(DatabaseStoreBase, JournalBase): |
paul@1083 | 737 | |
paul@1083 | 738 | "A journal system to support quotas." |
paul@1083 | 739 | |
paul@1083 | 740 | # Quota and user identity/group discovery. |
paul@1083 | 741 | |
paul@1083 | 742 | def get_quotas(self): |
paul@1083 | 743 | |
paul@1083 | 744 | "Return a list of quotas." |
paul@1083 | 745 | |
paul@1083 | 746 | query = "select distinct journal_quota from quota_freebusy" |
paul@1083 | 747 | self.cursor.execute(query) |
paul@1083 | 748 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 749 | |
paul@1083 | 750 | def get_quota_users(self, quota): |
paul@1083 | 751 | |
paul@1083 | 752 | "Return a list of quota users." |
paul@1083 | 753 | |
paul@1083 | 754 | columns = ["quota"] |
paul@1083 | 755 | values = [quota] |
paul@1083 | 756 | |
paul@1083 | 757 | query, values = self.get_query( |
paul@1083 | 758 | "select distinct user_group from quota_freebusy :condition", |
paul@1083 | 759 | columns, values) |
paul@1083 | 760 | |
paul@1083 | 761 | self.cursor.execute(query) |
paul@1083 | 762 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 763 | |
paul@1083 | 764 | # Groups of users sharing quotas. |
paul@1083 | 765 | |
paul@1083 | 766 | def get_groups(self, quota): |
paul@1083 | 767 | |
paul@1083 | 768 | "Return the identity mappings for the given 'quota' as a dictionary." |
paul@1083 | 769 | |
paul@1083 | 770 | columns = ["quota"] |
paul@1083 | 771 | values = [quota] |
paul@1083 | 772 | |
paul@1083 | 773 | query, values = self.get_query( |
paul@1083 | 774 | "select store_user, user_group from user_groups :condition", |
paul@1083 | 775 | columns, values) |
paul@1083 | 776 | |
paul@1083 | 777 | self.cursor.execute(query) |
paul@1083 | 778 | return dict(self.cursor.fetchall()) |
paul@1083 | 779 | |
paul@1083 | 780 | def get_limits(self, quota): |
paul@1083 | 781 | |
paul@1083 | 782 | """ |
paul@1083 | 783 | Return the limits for the 'quota' as a dictionary mapping identities or |
paul@1083 | 784 | groups to durations. |
paul@1083 | 785 | """ |
paul@1083 | 786 | |
paul@1083 | 787 | columns = ["quota"] |
paul@1083 | 788 | values = [quota] |
paul@1083 | 789 | |
paul@1083 | 790 | query, values = self.get_query( |
paul@1083 | 791 | "select user_group, quota_limit from quota_limits :condition", |
paul@1083 | 792 | columns, values) |
paul@1083 | 793 | |
paul@1083 | 794 | self.cursor.execute(query) |
paul@1083 | 795 | return dict(self.cursor.fetchall()) |
paul@1083 | 796 | |
paul@1083 | 797 | # Free/busy period access for users within quota groups. |
paul@1083 | 798 | |
paul@1083 | 799 | def get_freebusy(self, quota, user, mutable=False): |
paul@1083 | 800 | |
paul@1083 | 801 | "Get free/busy details for the given 'quota' and 'user'." |
paul@1083 | 802 | |
paul@1083 | 803 | table = "user_freebusy" |
paul@1083 | 804 | return FreeBusyDatabaseCollection(self.cursor, table, ["quota", "store_user"], [quota, user], mutable, self.paramstyle) |
paul@1083 | 805 | |
paul@1083 | 806 | def set_freebusy(self, quota, user, freebusy): |
paul@1083 | 807 | |
paul@1083 | 808 | "For the given 'quota' and 'user', set 'freebusy' details." |
paul@1083 | 809 | |
paul@1083 | 810 | table = "user_freebusy" |
paul@1083 | 811 | |
paul@1083 | 812 | if not isinstance(freebusy, FreeBusyDatabaseCollection) or freebusy.table_name != table: |
paul@1083 | 813 | fbc = FreeBusyDatabaseCollection(self.cursor, table, ["quota", "store_user"], [quota, user], True, self.paramstyle) |
paul@1083 | 814 | fbc += freebusy |
paul@1083 | 815 | |
paul@1083 | 816 | return True |
paul@1083 | 817 | |
paul@1083 | 818 | # Journal entry methods. |
paul@1083 | 819 | |
paul@1083 | 820 | def get_entries(self, quota, group, mutable=False): |
paul@1083 | 821 | |
paul@1083 | 822 | """ |
paul@1083 | 823 | Return a list of journal entries for the given 'quota' for the indicated |
paul@1083 | 824 | 'group'. |
paul@1083 | 825 | """ |
paul@1083 | 826 | |
paul@1083 | 827 | table = "quota_freebusy" |
paul@1083 | 828 | return FreeBusyDatabaseCollection(self.cursor, table, ["quota", "user_group"], [quota, group], mutable, self.paramstyle) |
paul@1083 | 829 | |
paul@1083 | 830 | def set_entries(self, quota, group, entries): |
paul@1083 | 831 | |
paul@1083 | 832 | """ |
paul@1083 | 833 | For the given 'quota' and indicated 'group', set the list of journal |
paul@1083 | 834 | 'entries'. |
paul@1083 | 835 | """ |
paul@1083 | 836 | |
paul@1083 | 837 | table = "quota_freebusy" |
paul@1083 | 838 | |
paul@1083 | 839 | if not isinstance(entries, FreeBusyDatabaseCollection) or entries.table_name != table: |
paul@1083 | 840 | fbc = FreeBusyDatabaseCollection(self.cursor, table, ["quota", "user_group"], [quota, group], True, self.paramstyle) |
paul@1083 | 841 | fbc += entries |
paul@1083 | 842 | |
paul@1083 | 843 | return True |
paul@1083 | 844 | |
paul@1083 | 845 | # vim: tabstop=4 expandtab shiftwidth=4 |