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@1088 | 22 | from imiptools.stores.common 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@1171 | 27 | from imiptools.period import FreeBusyDatabaseCollection, \ |
paul@1171 | 28 | FreeBusyGroupDatabaseCollection, \ |
paul@1171 | 29 | FreeBusyOffersDatabaseCollection |
paul@1083 | 30 | from imiptools.sql import DatabaseOperations |
paul@1083 | 31 | |
paul@1088 | 32 | class DatabaseStoreBase(DatabaseOperations): |
paul@1083 | 33 | |
paul@1083 | 34 | "A database store supporting user-specific locking." |
paul@1083 | 35 | |
paul@1087 | 36 | def __init__(self, connection, paramstyle=None): |
paul@1087 | 37 | DatabaseOperations.__init__(self, paramstyle=paramstyle) |
paul@1087 | 38 | self.connection = connection |
paul@1087 | 39 | self.cursor = connection.cursor() |
paul@1087 | 40 | |
paul@1083 | 41 | def acquire_lock(self, user, timeout=None): |
paul@1087 | 42 | pass |
paul@1083 | 43 | |
paul@1083 | 44 | def release_lock(self, user): |
paul@1087 | 45 | pass |
paul@1083 | 46 | |
paul@1088 | 47 | class DatabaseStore(DatabaseStoreBase, StoreBase): |
paul@1083 | 48 | |
paul@1083 | 49 | "A database store of tabular free/busy data and objects." |
paul@1083 | 50 | |
paul@1083 | 51 | # User discovery. |
paul@1083 | 52 | |
paul@1083 | 53 | def get_users(self): |
paul@1083 | 54 | |
paul@1083 | 55 | "Return a list of users." |
paul@1083 | 56 | |
paul@1147 | 57 | query = "select distinct store_user from (" \ |
paul@1147 | 58 | "select store_user from freebusy " \ |
paul@1147 | 59 | "union all select store_user from objects " \ |
paul@1147 | 60 | "union all select store_user from recurrences" \ |
paul@1147 | 61 | ") as users" |
paul@1083 | 62 | self.cursor.execute(query) |
paul@1083 | 63 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 64 | |
paul@1083 | 65 | # Event and event metadata access. |
paul@1083 | 66 | |
paul@1142 | 67 | def get_all_events(self, user, dirname=None): |
paul@1142 | 68 | |
paul@1142 | 69 | """ |
paul@1142 | 70 | Return a set of (uid, recurrenceid) tuples for all events. Unless |
paul@1142 | 71 | 'dirname' is specified, only active events are returned; otherwise, |
paul@1142 | 72 | events from the given 'dirname' are returned. |
paul@1142 | 73 | """ |
paul@1142 | 74 | |
paul@1142 | 75 | columns, values = self.get_event_table_filters(dirname) |
paul@1142 | 76 | |
paul@1142 | 77 | columns += ["store_user"] |
paul@1142 | 78 | values += [user] |
paul@1142 | 79 | |
paul@1142 | 80 | query, values = self.get_query( |
paul@1142 | 81 | "select object_uid, null as object_recurrenceid from objects :condition " |
paul@1142 | 82 | "union all " |
paul@1142 | 83 | "select object_uid, object_recurrenceid from recurrences :condition", |
paul@1142 | 84 | columns, values) |
paul@1142 | 85 | |
paul@1142 | 86 | self.cursor.execute(query, values) |
paul@1142 | 87 | return self.cursor.fetchall() |
paul@1142 | 88 | |
paul@1142 | 89 | def get_events(self, user, dirname=None): |
paul@1083 | 90 | |
paul@1083 | 91 | "Return a list of event identifiers." |
paul@1083 | 92 | |
paul@1142 | 93 | columns, values = self.get_event_table_filters(dirname) |
paul@1142 | 94 | |
paul@1142 | 95 | columns += ["store_user"] |
paul@1142 | 96 | values += [user] |
paul@1083 | 97 | |
paul@1083 | 98 | query, values = self.get_query( |
paul@1083 | 99 | "select object_uid from objects :condition", |
paul@1083 | 100 | columns, values) |
paul@1083 | 101 | |
paul@1083 | 102 | self.cursor.execute(query, values) |
paul@1083 | 103 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 104 | |
paul@1142 | 105 | def get_cancelled_events(self, user): |
paul@1083 | 106 | |
paul@1142 | 107 | "Return a list of event identifiers for cancelled events." |
paul@1083 | 108 | |
paul@1142 | 109 | return self.get_events(user, "cancellations") |
paul@1083 | 110 | |
paul@1083 | 111 | def get_event(self, user, uid, recurrenceid=None, dirname=None): |
paul@1083 | 112 | |
paul@1083 | 113 | """ |
paul@1083 | 114 | Get the event for the given 'user' with the given 'uid'. If |
paul@1083 | 115 | the optional 'recurrenceid' is specified, a specific instance or |
paul@1083 | 116 | occurrence of an event is returned. |
paul@1083 | 117 | """ |
paul@1083 | 118 | |
paul@1083 | 119 | table = self.get_event_table(recurrenceid, dirname) |
paul@1083 | 120 | columns, values = self.get_event_table_filters(dirname) |
paul@1083 | 121 | |
paul@1083 | 122 | if recurrenceid: |
paul@1083 | 123 | columns += ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 124 | values += [user, uid, recurrenceid] |
paul@1083 | 125 | else: |
paul@1083 | 126 | columns += ["store_user", "object_uid"] |
paul@1083 | 127 | values += [user, uid] |
paul@1083 | 128 | |
paul@1083 | 129 | query, values = self.get_query( |
paul@1083 | 130 | "select object_text from %(table)s :condition" % { |
paul@1083 | 131 | "table" : table |
paul@1083 | 132 | }, |
paul@1083 | 133 | columns, values) |
paul@1083 | 134 | |
paul@1083 | 135 | self.cursor.execute(query, values) |
paul@1083 | 136 | result = self.cursor.fetchone() |
paul@1083 | 137 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 138 | |
paul@1083 | 139 | def get_complete_event(self, user, uid): |
paul@1083 | 140 | |
paul@1083 | 141 | "Get the event for the given 'user' with the given 'uid'." |
paul@1083 | 142 | |
paul@1083 | 143 | columns = ["store_user", "object_uid"] |
paul@1083 | 144 | values = [user, uid] |
paul@1083 | 145 | |
paul@1083 | 146 | query, values = self.get_query( |
paul@1083 | 147 | "select object_text from objects :condition", |
paul@1083 | 148 | columns, values) |
paul@1083 | 149 | |
paul@1083 | 150 | self.cursor.execute(query, values) |
paul@1083 | 151 | result = self.cursor.fetchone() |
paul@1083 | 152 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 153 | |
paul@1083 | 154 | def set_complete_event(self, user, uid, node): |
paul@1083 | 155 | |
paul@1083 | 156 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@1083 | 157 | |
paul@1083 | 158 | columns = ["store_user", "object_uid"] |
paul@1083 | 159 | values = [user, uid] |
paul@1083 | 160 | setcolumns = ["object_text", "status"] |
paul@1083 | 161 | setvalues = [to_string(node, "utf-8"), "active"] |
paul@1083 | 162 | |
paul@1083 | 163 | query, values = self.get_query( |
paul@1083 | 164 | "update objects :set :condition", |
paul@1083 | 165 | columns, values, setcolumns, setvalues) |
paul@1083 | 166 | |
paul@1083 | 167 | self.cursor.execute(query, values) |
paul@1083 | 168 | |
paul@1083 | 169 | if self.cursor.rowcount > 0 or self.get_complete_event(user, uid): |
paul@1083 | 170 | return True |
paul@1083 | 171 | |
paul@1083 | 172 | columns = ["store_user", "object_uid", "object_text", "status"] |
paul@1083 | 173 | values = [user, uid, to_string(node, "utf-8"), "active"] |
paul@1083 | 174 | |
paul@1083 | 175 | query, values = self.get_query( |
paul@1083 | 176 | "insert into objects (:columns) values (:values)", |
paul@1083 | 177 | columns, values) |
paul@1083 | 178 | |
paul@1083 | 179 | self.cursor.execute(query, values) |
paul@1083 | 180 | return True |
paul@1083 | 181 | |
paul@1083 | 182 | def remove_parent_event(self, user, uid): |
paul@1083 | 183 | |
paul@1083 | 184 | "Remove the parent event for 'user' having the given 'uid'." |
paul@1083 | 185 | |
paul@1083 | 186 | columns = ["store_user", "object_uid"] |
paul@1083 | 187 | values = [user, uid] |
paul@1083 | 188 | |
paul@1083 | 189 | query, values = self.get_query( |
paul@1083 | 190 | "delete from objects :condition", |
paul@1083 | 191 | columns, values) |
paul@1083 | 192 | |
paul@1083 | 193 | self.cursor.execute(query, values) |
paul@1083 | 194 | return self.cursor.rowcount > 0 |
paul@1083 | 195 | |
paul@1083 | 196 | def get_active_recurrences(self, user, uid): |
paul@1083 | 197 | |
paul@1083 | 198 | """ |
paul@1083 | 199 | Get additional event instances for an event of the given 'user' with the |
paul@1083 | 200 | indicated 'uid'. Cancelled recurrences are not returned. |
paul@1083 | 201 | """ |
paul@1083 | 202 | |
paul@1083 | 203 | columns = ["store_user", "object_uid", "status"] |
paul@1083 | 204 | values = [user, uid, "active"] |
paul@1083 | 205 | |
paul@1083 | 206 | query, values = self.get_query( |
paul@1083 | 207 | "select object_recurrenceid from recurrences :condition", |
paul@1083 | 208 | columns, values) |
paul@1083 | 209 | |
paul@1083 | 210 | self.cursor.execute(query, values) |
paul@1083 | 211 | return [t[0] for t in self.cursor.fetchall() or []] |
paul@1083 | 212 | |
paul@1083 | 213 | def get_cancelled_recurrences(self, user, uid): |
paul@1083 | 214 | |
paul@1083 | 215 | """ |
paul@1083 | 216 | Get additional event instances for an event of the given 'user' with the |
paul@1083 | 217 | indicated 'uid'. Only cancelled recurrences are returned. |
paul@1083 | 218 | """ |
paul@1083 | 219 | |
paul@1083 | 220 | columns = ["store_user", "object_uid", "status"] |
paul@1083 | 221 | values = [user, uid, "cancelled"] |
paul@1083 | 222 | |
paul@1083 | 223 | query, values = self.get_query( |
paul@1083 | 224 | "select object_recurrenceid from recurrences :condition", |
paul@1083 | 225 | columns, values) |
paul@1083 | 226 | |
paul@1083 | 227 | self.cursor.execute(query, values) |
paul@1083 | 228 | return [t[0] for t in self.cursor.fetchall() or []] |
paul@1083 | 229 | |
paul@1083 | 230 | def get_recurrence(self, user, uid, recurrenceid): |
paul@1083 | 231 | |
paul@1083 | 232 | """ |
paul@1083 | 233 | For the event of the given 'user' with the given 'uid', return the |
paul@1083 | 234 | specific recurrence indicated by the 'recurrenceid'. |
paul@1083 | 235 | """ |
paul@1083 | 236 | |
paul@1083 | 237 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 238 | values = [user, uid, recurrenceid] |
paul@1083 | 239 | |
paul@1083 | 240 | query, values = self.get_query( |
paul@1083 | 241 | "select object_text from recurrences :condition", |
paul@1083 | 242 | columns, values) |
paul@1083 | 243 | |
paul@1083 | 244 | self.cursor.execute(query, values) |
paul@1083 | 245 | result = self.cursor.fetchone() |
paul@1083 | 246 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 247 | |
paul@1083 | 248 | def set_recurrence(self, user, uid, recurrenceid, node): |
paul@1083 | 249 | |
paul@1083 | 250 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@1083 | 251 | |
paul@1083 | 252 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 253 | values = [user, uid, recurrenceid] |
paul@1083 | 254 | setcolumns = ["object_text", "status"] |
paul@1083 | 255 | setvalues = [to_string(node, "utf-8"), "active"] |
paul@1083 | 256 | |
paul@1083 | 257 | query, values = self.get_query( |
paul@1083 | 258 | "update recurrences :set :condition", |
paul@1083 | 259 | columns, values, setcolumns, setvalues) |
paul@1083 | 260 | |
paul@1083 | 261 | self.cursor.execute(query, values) |
paul@1083 | 262 | |
paul@1083 | 263 | if self.cursor.rowcount > 0 or self.get_recurrence(user, uid, recurrenceid): |
paul@1083 | 264 | return True |
paul@1083 | 265 | |
paul@1083 | 266 | columns = ["store_user", "object_uid", "object_recurrenceid", "object_text", "status"] |
paul@1083 | 267 | values = [user, uid, recurrenceid, to_string(node, "utf-8"), "active"] |
paul@1083 | 268 | |
paul@1083 | 269 | query, values = self.get_query( |
paul@1083 | 270 | "insert into recurrences (:columns) values (:values)", |
paul@1083 | 271 | columns, values) |
paul@1083 | 272 | |
paul@1083 | 273 | self.cursor.execute(query, values) |
paul@1083 | 274 | return True |
paul@1083 | 275 | |
paul@1083 | 276 | def remove_recurrence(self, user, uid, recurrenceid): |
paul@1083 | 277 | |
paul@1083 | 278 | """ |
paul@1083 | 279 | Remove a special recurrence from an event stored by 'user' having the |
paul@1083 | 280 | given 'uid' and 'recurrenceid'. |
paul@1083 | 281 | """ |
paul@1083 | 282 | |
paul@1083 | 283 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 284 | values = [user, uid, recurrenceid] |
paul@1083 | 285 | |
paul@1083 | 286 | query, values = self.get_query( |
paul@1083 | 287 | "delete from recurrences :condition", |
paul@1083 | 288 | columns, values) |
paul@1083 | 289 | |
paul@1083 | 290 | self.cursor.execute(query, values) |
paul@1083 | 291 | return True |
paul@1083 | 292 | |
paul@1083 | 293 | def remove_recurrences(self, user, uid): |
paul@1083 | 294 | |
paul@1083 | 295 | """ |
paul@1083 | 296 | Remove all recurrences for an event stored by 'user' having the given |
paul@1083 | 297 | 'uid'. |
paul@1083 | 298 | """ |
paul@1083 | 299 | |
paul@1083 | 300 | columns = ["store_user", "object_uid"] |
paul@1083 | 301 | values = [user, uid] |
paul@1083 | 302 | |
paul@1083 | 303 | query, values = self.get_query( |
paul@1083 | 304 | "delete from recurrences :condition", |
paul@1083 | 305 | columns, values) |
paul@1083 | 306 | |
paul@1083 | 307 | self.cursor.execute(query, values) |
paul@1083 | 308 | return True |
paul@1083 | 309 | |
paul@1142 | 310 | # Event table computation. |
paul@1142 | 311 | |
paul@1142 | 312 | def get_event_table(self, recurrenceid=None, dirname=None): |
paul@1142 | 313 | |
paul@1142 | 314 | "Get the table providing events for any specified 'dirname'." |
paul@1142 | 315 | |
paul@1142 | 316 | if recurrenceid: |
paul@1142 | 317 | return self.get_recurrence_table(dirname) |
paul@1142 | 318 | else: |
paul@1142 | 319 | return self.get_complete_event_table(dirname) |
paul@1142 | 320 | |
paul@1142 | 321 | def get_event_table_filters(self, dirname=None): |
paul@1142 | 322 | |
paul@1142 | 323 | "Get filter details for any specified 'dirname'." |
paul@1142 | 324 | |
paul@1142 | 325 | if dirname == "cancellations": |
paul@1142 | 326 | return ["status"], ["cancelled"] |
paul@1142 | 327 | else: |
paul@1142 | 328 | return ["status"], ["active"] |
paul@1142 | 329 | |
paul@1142 | 330 | def get_complete_event_table(self, dirname=None): |
paul@1142 | 331 | |
paul@1142 | 332 | "Get the table providing events for any specified 'dirname'." |
paul@1142 | 333 | |
paul@1142 | 334 | if dirname == "counters": |
paul@1142 | 335 | return "countered_objects" |
paul@1142 | 336 | else: |
paul@1142 | 337 | return "objects" |
paul@1142 | 338 | |
paul@1142 | 339 | def get_recurrence_table(self, dirname=None): |
paul@1142 | 340 | |
paul@1142 | 341 | "Get the table providing recurrences for any specified 'dirname'." |
paul@1142 | 342 | |
paul@1142 | 343 | if dirname == "counters": |
paul@1142 | 344 | return "countered_recurrences" |
paul@1142 | 345 | else: |
paul@1142 | 346 | return "recurrences" |
paul@1142 | 347 | |
paul@1083 | 348 | # Free/busy period providers, upon extension of the free/busy records. |
paul@1083 | 349 | |
paul@1083 | 350 | def _get_freebusy_providers(self, user): |
paul@1083 | 351 | |
paul@1083 | 352 | """ |
paul@1083 | 353 | Return the free/busy providers for the given 'user'. |
paul@1083 | 354 | |
paul@1083 | 355 | This function returns any stored datetime and a list of providers as a |
paul@1083 | 356 | 2-tuple. Each provider is itself a (uid, recurrenceid) tuple. |
paul@1083 | 357 | """ |
paul@1083 | 358 | |
paul@1083 | 359 | columns = ["store_user"] |
paul@1083 | 360 | values = [user] |
paul@1083 | 361 | |
paul@1083 | 362 | query, values = self.get_query( |
paul@1083 | 363 | "select object_uid, object_recurrenceid from freebusy_providers :condition", |
paul@1083 | 364 | columns, values) |
paul@1083 | 365 | |
paul@1083 | 366 | self.cursor.execute(query, values) |
paul@1083 | 367 | providers = self.cursor.fetchall() |
paul@1083 | 368 | |
paul@1083 | 369 | columns = ["store_user"] |
paul@1083 | 370 | values = [user] |
paul@1083 | 371 | |
paul@1083 | 372 | query, values = self.get_query( |
paul@1083 | 373 | "select start from freebusy_provider_datetimes :condition", |
paul@1083 | 374 | columns, values) |
paul@1083 | 375 | |
paul@1083 | 376 | self.cursor.execute(query, values) |
paul@1083 | 377 | result = self.cursor.fetchone() |
paul@1083 | 378 | dt_string = result and result[0] |
paul@1083 | 379 | |
paul@1083 | 380 | return dt_string, providers |
paul@1083 | 381 | |
paul@1083 | 382 | def _set_freebusy_providers(self, user, dt_string, t): |
paul@1083 | 383 | |
paul@1083 | 384 | "Set the given provider timestamp 'dt_string' and table 't'." |
paul@1083 | 385 | |
paul@1083 | 386 | # NOTE: Locking? |
paul@1083 | 387 | |
paul@1083 | 388 | columns = ["store_user"] |
paul@1083 | 389 | values = [user] |
paul@1083 | 390 | |
paul@1083 | 391 | query, values = self.get_query( |
paul@1083 | 392 | "delete from freebusy_providers :condition", |
paul@1083 | 393 | columns, values) |
paul@1083 | 394 | |
paul@1083 | 395 | self.cursor.execute(query, values) |
paul@1083 | 396 | |
paul@1083 | 397 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 398 | |
paul@1083 | 399 | for uid, recurrenceid in t: |
paul@1083 | 400 | values = [user, uid, recurrenceid] |
paul@1083 | 401 | |
paul@1083 | 402 | query, values = self.get_query( |
paul@1083 | 403 | "insert into freebusy_providers (:columns) values (:values)", |
paul@1083 | 404 | columns, values) |
paul@1083 | 405 | |
paul@1083 | 406 | self.cursor.execute(query, values) |
paul@1083 | 407 | |
paul@1083 | 408 | columns = ["store_user"] |
paul@1083 | 409 | values = [user] |
paul@1083 | 410 | setcolumns = ["start"] |
paul@1083 | 411 | setvalues = [dt_string] |
paul@1083 | 412 | |
paul@1083 | 413 | query, values = self.get_query( |
paul@1083 | 414 | "update freebusy_provider_datetimes :set :condition", |
paul@1083 | 415 | columns, values, setcolumns, setvalues) |
paul@1083 | 416 | |
paul@1083 | 417 | self.cursor.execute(query, values) |
paul@1083 | 418 | |
paul@1083 | 419 | if self.cursor.rowcount > 0: |
paul@1083 | 420 | return True |
paul@1083 | 421 | |
paul@1083 | 422 | columns = ["store_user", "start"] |
paul@1083 | 423 | values = [user, dt_string] |
paul@1083 | 424 | |
paul@1083 | 425 | query, values = self.get_query( |
paul@1083 | 426 | "insert into freebusy_provider_datetimes (:columns) values (:values)", |
paul@1083 | 427 | columns, values) |
paul@1083 | 428 | |
paul@1083 | 429 | self.cursor.execute(query, values) |
paul@1083 | 430 | return True |
paul@1083 | 431 | |
paul@1083 | 432 | # Free/busy period access. |
paul@1083 | 433 | |
paul@1171 | 434 | def get_freebusy(self, user, name=None, mutable=False, cls=None): |
paul@1083 | 435 | |
paul@1083 | 436 | "Get free/busy details for the given 'user'." |
paul@1083 | 437 | |
paul@1083 | 438 | table = name or "freebusy" |
paul@1171 | 439 | cls = cls or FreeBusyDatabaseCollection |
paul@1171 | 440 | return cls(self.cursor, table, ["store_user"], [user], mutable, self.paramstyle) |
paul@1083 | 441 | |
paul@1192 | 442 | def get_freebusy_for_other(self, user, other, mutable=False, cls=None): |
paul@1083 | 443 | |
paul@1083 | 444 | "For the given 'user', get free/busy details for the 'other' user." |
paul@1083 | 445 | |
paul@1088 | 446 | table = "freebusy_other" |
paul@1192 | 447 | cls = cls or FreeBusyDatabaseCollection |
paul@1192 | 448 | return cls(self.cursor, table, ["store_user", "other"], [user, other], mutable, self.paramstyle) |
paul@1083 | 449 | |
paul@1171 | 450 | def set_freebusy(self, user, freebusy, name=None, cls=None): |
paul@1083 | 451 | |
paul@1083 | 452 | "For the given 'user', set 'freebusy' details." |
paul@1083 | 453 | |
paul@1083 | 454 | table = name or "freebusy" |
paul@1171 | 455 | cls = cls or FreeBusyDatabaseCollection |
paul@1083 | 456 | |
paul@1171 | 457 | if not isinstance(freebusy, cls) or freebusy.table_name != table: |
paul@1171 | 458 | fbc = cls(self.cursor, table, ["store_user"], [user], True, self.paramstyle) |
paul@1083 | 459 | fbc += freebusy |
paul@1083 | 460 | |
paul@1083 | 461 | return True |
paul@1083 | 462 | |
paul@1192 | 463 | def set_freebusy_for_other(self, user, freebusy, other, cls=None): |
paul@1083 | 464 | |
paul@1083 | 465 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@1083 | 466 | |
paul@1088 | 467 | table = "freebusy_other" |
paul@1192 | 468 | cls = cls or FreeBusyDatabaseCollection |
paul@1083 | 469 | |
paul@1192 | 470 | if not isinstance(freebusy, cls) or freebusy.table_name != table: |
paul@1192 | 471 | fbc = cls(self.cursor, table, ["store_user", "other"], [user, other], True, self.paramstyle) |
paul@1083 | 472 | fbc += freebusy |
paul@1083 | 473 | |
paul@1083 | 474 | return True |
paul@1083 | 475 | |
paul@1142 | 476 | def get_freebusy_others(self, user): |
paul@1142 | 477 | |
paul@1142 | 478 | """ |
paul@1142 | 479 | For the given 'user', return a list of other users for whom free/busy |
paul@1142 | 480 | information is retained. |
paul@1142 | 481 | """ |
paul@1142 | 482 | |
paul@1142 | 483 | columns = ["store_user"] |
paul@1142 | 484 | values = [user] |
paul@1142 | 485 | |
paul@1142 | 486 | query, values = self.get_query( |
paul@1146 | 487 | "select distinct other from freebusy_other :condition", |
paul@1142 | 488 | columns, values) |
paul@1142 | 489 | |
paul@1142 | 490 | self.cursor.execute(query, values) |
paul@1142 | 491 | return [r[0] for r in self.cursor.fetchall()] |
paul@1142 | 492 | |
paul@1083 | 493 | # Tentative free/busy periods related to countering. |
paul@1083 | 494 | |
paul@1083 | 495 | def get_freebusy_offers(self, user, mutable=False): |
paul@1083 | 496 | |
paul@1083 | 497 | "Get free/busy offers for the given 'user'." |
paul@1083 | 498 | |
paul@1083 | 499 | # Expire old offers and save the collection if modified. |
paul@1083 | 500 | |
paul@1083 | 501 | now = format_datetime(to_timezone(datetime.utcnow(), "UTC")) |
paul@1083 | 502 | columns = ["store_user", "expires"] |
paul@1083 | 503 | values = [user, now] |
paul@1083 | 504 | |
paul@1083 | 505 | query, values = self.get_query( |
paul@1083 | 506 | "delete from freebusy_offers :condition", |
paul@1083 | 507 | columns, values) |
paul@1083 | 508 | |
paul@1083 | 509 | self.cursor.execute(query, values) |
paul@1083 | 510 | |
paul@1171 | 511 | return self.get_freebusy(user, "freebusy_offers", mutable, FreeBusyOffersDatabaseCollection) |
paul@1083 | 512 | |
paul@1088 | 513 | def set_freebusy_offers(self, user, freebusy): |
paul@1088 | 514 | |
paul@1088 | 515 | "For the given 'user', set 'freebusy' offers." |
paul@1088 | 516 | |
paul@1171 | 517 | return self.set_freebusy(user, freebusy, "freebusy_offers", cls=FreeBusyOffersDatabaseCollection) |
paul@1088 | 518 | |
paul@1083 | 519 | # Requests and counter-proposals. |
paul@1083 | 520 | |
paul@1083 | 521 | def get_requests(self, user): |
paul@1083 | 522 | |
paul@1083 | 523 | "Get requests for the given 'user'." |
paul@1083 | 524 | |
paul@1083 | 525 | columns = ["store_user"] |
paul@1083 | 526 | values = [user] |
paul@1083 | 527 | |
paul@1083 | 528 | query, values = self.get_query( |
paul@1088 | 529 | "select object_uid, object_recurrenceid, request_type from requests :condition", |
paul@1083 | 530 | columns, values) |
paul@1083 | 531 | |
paul@1083 | 532 | self.cursor.execute(query, values) |
paul@1083 | 533 | return self.cursor.fetchall() |
paul@1083 | 534 | |
paul@1083 | 535 | def set_request(self, user, uid, recurrenceid=None, type=None): |
paul@1083 | 536 | |
paul@1083 | 537 | """ |
paul@1083 | 538 | For the given 'user', set the queued 'uid' and 'recurrenceid', |
paul@1083 | 539 | indicating a request, along with any given 'type'. |
paul@1083 | 540 | """ |
paul@1083 | 541 | |
paul@1083 | 542 | columns = ["store_user", "object_uid", "object_recurrenceid", "request_type"] |
paul@1083 | 543 | values = [user, uid, recurrenceid, type] |
paul@1083 | 544 | |
paul@1083 | 545 | query, values = self.get_query( |
paul@1083 | 546 | "insert into requests (:columns) values (:values)", |
paul@1083 | 547 | columns, values) |
paul@1083 | 548 | |
paul@1083 | 549 | self.cursor.execute(query, values) |
paul@1083 | 550 | return True |
paul@1083 | 551 | |
paul@1088 | 552 | def queue_request(self, user, uid, recurrenceid=None, type=None): |
paul@1088 | 553 | |
paul@1088 | 554 | """ |
paul@1088 | 555 | Queue a request for 'user' having the given 'uid'. If the optional |
paul@1088 | 556 | 'recurrenceid' is specified, the entry refers to a specific instance |
paul@1088 | 557 | or occurrence of an event. The 'type' parameter can be used to indicate |
paul@1088 | 558 | a specific type of request. |
paul@1088 | 559 | """ |
paul@1088 | 560 | |
paul@1088 | 561 | if recurrenceid: |
paul@1088 | 562 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1088 | 563 | values = [user, uid, recurrenceid] |
paul@1088 | 564 | else: |
paul@1088 | 565 | columns = ["store_user", "object_uid"] |
paul@1088 | 566 | values = [user, uid] |
paul@1088 | 567 | |
paul@1088 | 568 | setcolumns = ["request_type"] |
paul@1088 | 569 | setvalues = [type] |
paul@1088 | 570 | |
paul@1088 | 571 | query, values = self.get_query( |
paul@1088 | 572 | "update requests :set :condition", |
paul@1088 | 573 | columns, values, setcolumns, setvalues) |
paul@1088 | 574 | |
paul@1088 | 575 | self.cursor.execute(query, values) |
paul@1088 | 576 | |
paul@1088 | 577 | if self.cursor.rowcount > 0: |
paul@1088 | 578 | return |
paul@1088 | 579 | |
paul@1088 | 580 | self.set_request(user, uid, recurrenceid, type) |
paul@1088 | 581 | |
paul@1088 | 582 | def dequeue_request(self, user, uid, recurrenceid=None): |
paul@1088 | 583 | |
paul@1088 | 584 | """ |
paul@1088 | 585 | Dequeue all requests for 'user' having the given 'uid'. If the optional |
paul@1088 | 586 | 'recurrenceid' is specified, all requests for that specific instance or |
paul@1088 | 587 | occurrence of an event are dequeued. |
paul@1088 | 588 | """ |
paul@1088 | 589 | |
paul@1088 | 590 | if recurrenceid: |
paul@1088 | 591 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1088 | 592 | values = [user, uid, recurrenceid] |
paul@1088 | 593 | else: |
paul@1088 | 594 | columns = ["store_user", "object_uid"] |
paul@1088 | 595 | values = [user, uid] |
paul@1088 | 596 | |
paul@1088 | 597 | query, values = self.get_query( |
paul@1088 | 598 | "delete from requests :condition", |
paul@1088 | 599 | columns, values) |
paul@1088 | 600 | |
paul@1088 | 601 | self.cursor.execute(query, values) |
paul@1088 | 602 | return True |
paul@1088 | 603 | |
paul@1083 | 604 | def get_counters(self, user, uid, recurrenceid=None): |
paul@1083 | 605 | |
paul@1083 | 606 | """ |
paul@1083 | 607 | For the given 'user', return a list of users from whom counter-proposals |
paul@1083 | 608 | have been received for the given 'uid' and optional 'recurrenceid'. |
paul@1083 | 609 | """ |
paul@1083 | 610 | |
paul@1083 | 611 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 612 | |
paul@1083 | 613 | if recurrenceid: |
paul@1083 | 614 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 615 | values = [user, uid, recurrenceid] |
paul@1083 | 616 | else: |
paul@1083 | 617 | columns = ["store_user", "object_uid"] |
paul@1083 | 618 | values = [user, uid] |
paul@1083 | 619 | |
paul@1083 | 620 | query, values = self.get_query( |
paul@1083 | 621 | "select other from %(table)s :condition" % { |
paul@1083 | 622 | "table" : table |
paul@1083 | 623 | }, |
paul@1083 | 624 | columns, values) |
paul@1083 | 625 | |
paul@1083 | 626 | self.cursor.execute(query, values) |
paul@1146 | 627 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 628 | |
paul@1083 | 629 | def get_counter(self, user, other, uid, recurrenceid=None): |
paul@1083 | 630 | |
paul@1083 | 631 | """ |
paul@1083 | 632 | For the given 'user', return the counter-proposal from 'other' for the |
paul@1083 | 633 | given 'uid' and optional 'recurrenceid'. |
paul@1083 | 634 | """ |
paul@1083 | 635 | |
paul@1083 | 636 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 637 | |
paul@1083 | 638 | if recurrenceid: |
paul@1083 | 639 | columns = ["store_user", "other", "object_uid", "object_recurrenceid"] |
paul@1083 | 640 | values = [user, other, uid, recurrenceid] |
paul@1083 | 641 | else: |
paul@1083 | 642 | columns = ["store_user", "other", "object_uid"] |
paul@1083 | 643 | values = [user, other, uid] |
paul@1083 | 644 | |
paul@1083 | 645 | query, values = self.get_query( |
paul@1083 | 646 | "select object_text from %(table)s :condition" % { |
paul@1083 | 647 | "table" : table |
paul@1083 | 648 | }, |
paul@1083 | 649 | columns, values) |
paul@1083 | 650 | |
paul@1083 | 651 | self.cursor.execute(query, values) |
paul@1088 | 652 | result = self.cursor.fetchone() |
paul@1083 | 653 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 654 | |
paul@1083 | 655 | def set_counter(self, user, other, node, uid, recurrenceid=None): |
paul@1083 | 656 | |
paul@1083 | 657 | """ |
paul@1083 | 658 | For the given 'user', store a counter-proposal received from 'other' the |
paul@1083 | 659 | given 'node' representing that proposal for the given 'uid' and |
paul@1083 | 660 | 'recurrenceid'. |
paul@1083 | 661 | """ |
paul@1083 | 662 | |
paul@1083 | 663 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 664 | |
paul@1088 | 665 | if recurrenceid: |
paul@1088 | 666 | columns = ["store_user", "other", "object_uid", "object_recurrenceid", "object_text"] |
paul@1088 | 667 | values = [user, other, uid, recurrenceid, to_string(node, "utf-8")] |
paul@1088 | 668 | else: |
paul@1088 | 669 | columns = ["store_user", "other", "object_uid", "object_text"] |
paul@1088 | 670 | values = [user, other, uid, to_string(node, "utf-8")] |
paul@1083 | 671 | |
paul@1083 | 672 | query, values = self.get_query( |
paul@1083 | 673 | "insert into %(table)s (:columns) values (:values)" % { |
paul@1083 | 674 | "table" : table |
paul@1083 | 675 | }, |
paul@1083 | 676 | columns, values) |
paul@1083 | 677 | |
paul@1083 | 678 | self.cursor.execute(query, values) |
paul@1083 | 679 | return True |
paul@1083 | 680 | |
paul@1083 | 681 | def remove_counters(self, user, uid, recurrenceid=None): |
paul@1083 | 682 | |
paul@1083 | 683 | """ |
paul@1083 | 684 | For the given 'user', remove all counter-proposals associated with the |
paul@1083 | 685 | given 'uid' and 'recurrenceid'. |
paul@1083 | 686 | """ |
paul@1083 | 687 | |
paul@1083 | 688 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 689 | |
paul@1083 | 690 | if recurrenceid: |
paul@1083 | 691 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 692 | values = [user, uid, recurrenceid] |
paul@1083 | 693 | else: |
paul@1083 | 694 | columns = ["store_user", "object_uid"] |
paul@1083 | 695 | values = [user, uid] |
paul@1083 | 696 | |
paul@1083 | 697 | query, values = self.get_query( |
paul@1083 | 698 | "delete from %(table)s :condition" % { |
paul@1083 | 699 | "table" : table |
paul@1083 | 700 | }, |
paul@1083 | 701 | columns, values) |
paul@1083 | 702 | |
paul@1083 | 703 | self.cursor.execute(query, values) |
paul@1083 | 704 | return True |
paul@1083 | 705 | |
paul@1083 | 706 | def remove_counter(self, user, other, uid, recurrenceid=None): |
paul@1083 | 707 | |
paul@1083 | 708 | """ |
paul@1083 | 709 | For the given 'user', remove any counter-proposal from 'other' |
paul@1083 | 710 | associated with the given 'uid' and 'recurrenceid'. |
paul@1083 | 711 | """ |
paul@1083 | 712 | |
paul@1083 | 713 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 714 | |
paul@1083 | 715 | if recurrenceid: |
paul@1083 | 716 | columns = ["store_user", "other", "object_uid", "object_recurrenceid"] |
paul@1083 | 717 | values = [user, other, uid, recurrenceid] |
paul@1083 | 718 | else: |
paul@1083 | 719 | columns = ["store_user", "other", "object_uid"] |
paul@1083 | 720 | values = [user, other, uid] |
paul@1083 | 721 | |
paul@1083 | 722 | query, values = self.get_query( |
paul@1083 | 723 | "delete from %(table)s :condition" % { |
paul@1083 | 724 | "table" : table |
paul@1083 | 725 | }, |
paul@1083 | 726 | columns, values) |
paul@1083 | 727 | |
paul@1083 | 728 | self.cursor.execute(query, values) |
paul@1083 | 729 | return True |
paul@1083 | 730 | |
paul@1083 | 731 | # Event cancellation. |
paul@1083 | 732 | |
paul@1083 | 733 | def cancel_event(self, user, uid, recurrenceid=None): |
paul@1083 | 734 | |
paul@1083 | 735 | """ |
paul@1083 | 736 | Cancel an event for 'user' having the given 'uid'. If the optional |
paul@1083 | 737 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@1083 | 738 | event is cancelled. |
paul@1083 | 739 | """ |
paul@1083 | 740 | |
paul@1083 | 741 | table = self.get_event_table(recurrenceid) |
paul@1083 | 742 | |
paul@1083 | 743 | if recurrenceid: |
paul@1083 | 744 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 745 | values = [user, uid, recurrenceid] |
paul@1083 | 746 | else: |
paul@1083 | 747 | columns = ["store_user", "object_uid"] |
paul@1083 | 748 | values = [user, uid] |
paul@1083 | 749 | |
paul@1083 | 750 | setcolumns = ["status"] |
paul@1083 | 751 | setvalues = ["cancelled"] |
paul@1083 | 752 | |
paul@1083 | 753 | query, values = self.get_query( |
paul@1083 | 754 | "update %(table)s :set :condition" % { |
paul@1083 | 755 | "table" : table |
paul@1083 | 756 | }, |
paul@1083 | 757 | columns, values, setcolumns, setvalues) |
paul@1083 | 758 | |
paul@1083 | 759 | self.cursor.execute(query, values) |
paul@1083 | 760 | return True |
paul@1083 | 761 | |
paul@1083 | 762 | def uncancel_event(self, user, uid, recurrenceid=None): |
paul@1083 | 763 | |
paul@1083 | 764 | """ |
paul@1083 | 765 | Uncancel an event for 'user' having the given 'uid'. If the optional |
paul@1083 | 766 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@1083 | 767 | event is uncancelled. |
paul@1083 | 768 | """ |
paul@1083 | 769 | |
paul@1083 | 770 | table = self.get_event_table(recurrenceid) |
paul@1083 | 771 | |
paul@1083 | 772 | if recurrenceid: |
paul@1083 | 773 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 774 | values = [user, uid, recurrenceid] |
paul@1083 | 775 | else: |
paul@1083 | 776 | columns = ["store_user", "object_uid"] |
paul@1083 | 777 | values = [user, uid] |
paul@1083 | 778 | |
paul@1083 | 779 | setcolumns = ["status"] |
paul@1083 | 780 | setvalues = ["active"] |
paul@1083 | 781 | |
paul@1083 | 782 | query, values = self.get_query( |
paul@1083 | 783 | "update %(table)s :set :condition" % { |
paul@1083 | 784 | "table" : table |
paul@1083 | 785 | }, |
paul@1083 | 786 | columns, values, setcolumns, setvalues) |
paul@1083 | 787 | |
paul@1083 | 788 | self.cursor.execute(query, values) |
paul@1083 | 789 | return True |
paul@1083 | 790 | |
paul@1083 | 791 | def remove_cancellation(self, user, uid, recurrenceid=None): |
paul@1083 | 792 | |
paul@1083 | 793 | """ |
paul@1083 | 794 | Remove a cancellation for 'user' for the event having the given 'uid'. |
paul@1083 | 795 | If the optional 'recurrenceid' is specified, a specific instance or |
paul@1083 | 796 | occurrence of an event is affected. |
paul@1083 | 797 | """ |
paul@1083 | 798 | |
paul@1083 | 799 | table = self.get_event_table(recurrenceid) |
paul@1083 | 800 | |
paul@1083 | 801 | if recurrenceid: |
paul@1083 | 802 | columns = ["store_user", "object_uid", "object_recurrenceid", "status"] |
paul@1083 | 803 | values = [user, uid, recurrenceid, "cancelled"] |
paul@1083 | 804 | else: |
paul@1083 | 805 | columns = ["store_user", "object_uid", "status"] |
paul@1083 | 806 | values = [user, uid, "cancelled"] |
paul@1083 | 807 | |
paul@1083 | 808 | query, values = self.get_query( |
paul@1083 | 809 | "delete from %(table)s :condition" % { |
paul@1083 | 810 | "table" : table |
paul@1083 | 811 | }, |
paul@1083 | 812 | columns, values) |
paul@1083 | 813 | |
paul@1083 | 814 | self.cursor.execute(query, values) |
paul@1083 | 815 | return True |
paul@1083 | 816 | |
paul@1192 | 817 | class DatabaseJournal(DatabaseStore, JournalBase): |
paul@1083 | 818 | |
paul@1083 | 819 | "A journal system to support quotas." |
paul@1083 | 820 | |
paul@1083 | 821 | # Quota and user identity/group discovery. |
paul@1083 | 822 | |
paul@1083 | 823 | def get_quotas(self): |
paul@1083 | 824 | |
paul@1083 | 825 | "Return a list of quotas." |
paul@1083 | 826 | |
paul@1146 | 827 | query = "select distinct quota from (" \ |
paul@1146 | 828 | "select quota from quota_freebusy " \ |
paul@1146 | 829 | "union all select quota from quota_limits" \ |
paul@1146 | 830 | ") as quotas" |
paul@1083 | 831 | self.cursor.execute(query) |
paul@1083 | 832 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 833 | |
paul@1083 | 834 | def get_quota_users(self, quota): |
paul@1083 | 835 | |
paul@1176 | 836 | "Return a list of quota users for the 'quota'." |
paul@1176 | 837 | |
paul@1176 | 838 | columns = ["quota"] |
paul@1176 | 839 | values = [quota] |
paul@1176 | 840 | |
paul@1176 | 841 | query, values = self.get_query( |
paul@1176 | 842 | "select distinct user_group from (" \ |
paul@1176 | 843 | "select user_group from quota_freebusy :condition " \ |
paul@1176 | 844 | "union all select user_group from quota_delegates :condition" \ |
paul@1176 | 845 | ") as users", |
paul@1176 | 846 | columns, values) |
paul@1176 | 847 | |
paul@1176 | 848 | self.cursor.execute(query, values) |
paul@1176 | 849 | return [r[0] for r in self.cursor.fetchall()] |
paul@1176 | 850 | |
paul@1176 | 851 | # Delegate information for the quota. |
paul@1176 | 852 | |
paul@1176 | 853 | def get_delegates(self, quota): |
paul@1176 | 854 | |
paul@1176 | 855 | "Return a list of delegates for 'quota'." |
paul@1083 | 856 | |
paul@1083 | 857 | columns = ["quota"] |
paul@1083 | 858 | values = [quota] |
paul@1083 | 859 | |
paul@1083 | 860 | query, values = self.get_query( |
paul@1176 | 861 | "select distinct store_user from quota_delegates :condition", |
paul@1083 | 862 | columns, values) |
paul@1083 | 863 | |
paul@1146 | 864 | self.cursor.execute(query, values) |
paul@1083 | 865 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 866 | |
paul@1176 | 867 | def set_delegates(self, quota, delegates): |
paul@1176 | 868 | |
paul@1176 | 869 | "For the given 'quota', set the list of 'delegates'." |
paul@1176 | 870 | |
paul@1176 | 871 | columns = ["quota"] |
paul@1176 | 872 | values = [quota] |
paul@1176 | 873 | |
paul@1176 | 874 | query, values = self.get_query( |
paul@1176 | 875 | "delete from quota_delegates :condition", |
paul@1176 | 876 | columns, values) |
paul@1176 | 877 | |
paul@1176 | 878 | self.cursor.execute(query, values) |
paul@1176 | 879 | |
paul@1176 | 880 | for store_user in delegates: |
paul@1176 | 881 | |
paul@1176 | 882 | columns = ["quota", "store_user"] |
paul@1176 | 883 | values = [quota, store_user] |
paul@1176 | 884 | |
paul@1176 | 885 | query, values = self.get_query( |
paul@1176 | 886 | "insert into quota_delegates (:columns) values (:values)", |
paul@1176 | 887 | columns, values) |
paul@1176 | 888 | |
paul@1186 | 889 | self.cursor.execute(query, values) |
paul@1186 | 890 | |
paul@1176 | 891 | return True |
paul@1176 | 892 | |
paul@1083 | 893 | # Groups of users sharing quotas. |
paul@1083 | 894 | |
paul@1083 | 895 | def get_groups(self, quota): |
paul@1083 | 896 | |
paul@1083 | 897 | "Return the identity mappings for the given 'quota' as a dictionary." |
paul@1083 | 898 | |
paul@1083 | 899 | columns = ["quota"] |
paul@1083 | 900 | values = [quota] |
paul@1083 | 901 | |
paul@1083 | 902 | query, values = self.get_query( |
paul@1083 | 903 | "select store_user, user_group from user_groups :condition", |
paul@1083 | 904 | columns, values) |
paul@1083 | 905 | |
paul@1089 | 906 | self.cursor.execute(query, values) |
paul@1083 | 907 | return dict(self.cursor.fetchall()) |
paul@1083 | 908 | |
paul@1176 | 909 | def set_groups(self, quota, groups): |
paul@1142 | 910 | |
paul@1176 | 911 | "For the given 'quota', set 'groups' mapping users to groups." |
paul@1142 | 912 | |
paul@1176 | 913 | columns = ["quota"] |
paul@1176 | 914 | values = [quota] |
paul@1142 | 915 | |
paul@1142 | 916 | query, values = self.get_query( |
paul@1176 | 917 | "delete from user_groups :condition", |
paul@1176 | 918 | columns, values) |
paul@1142 | 919 | |
paul@1142 | 920 | self.cursor.execute(query, values) |
paul@1142 | 921 | |
paul@1176 | 922 | for store_user, user_group in groups.items(): |
paul@1142 | 923 | |
paul@1176 | 924 | columns = ["quota", "store_user", "user_group"] |
paul@1176 | 925 | values = [quota, store_user, user_group] |
paul@1142 | 926 | |
paul@1176 | 927 | query, values = self.get_query( |
paul@1176 | 928 | "insert into user_groups (:columns) values (:values)", |
paul@1176 | 929 | columns, values) |
paul@1142 | 930 | |
paul@1186 | 931 | self.cursor.execute(query, values) |
paul@1186 | 932 | |
paul@1142 | 933 | return True |
paul@1142 | 934 | |
paul@1083 | 935 | def get_limits(self, quota): |
paul@1083 | 936 | |
paul@1083 | 937 | """ |
paul@1083 | 938 | Return the limits for the 'quota' as a dictionary mapping identities or |
paul@1083 | 939 | groups to durations. |
paul@1083 | 940 | """ |
paul@1083 | 941 | |
paul@1083 | 942 | columns = ["quota"] |
paul@1083 | 943 | values = [quota] |
paul@1083 | 944 | |
paul@1083 | 945 | query, values = self.get_query( |
paul@1083 | 946 | "select user_group, quota_limit from quota_limits :condition", |
paul@1083 | 947 | columns, values) |
paul@1083 | 948 | |
paul@1089 | 949 | self.cursor.execute(query, values) |
paul@1083 | 950 | return dict(self.cursor.fetchall()) |
paul@1083 | 951 | |
paul@1176 | 952 | def set_limits(self, quota, limits): |
paul@1089 | 953 | |
paul@1089 | 954 | """ |
paul@1176 | 955 | For the given 'quota', set the given 'limits' on resource usage mapping |
paul@1176 | 956 | groups to limits. |
paul@1089 | 957 | """ |
paul@1089 | 958 | |
paul@1176 | 959 | columns = ["quota"] |
paul@1176 | 960 | values = [quota] |
paul@1089 | 961 | |
paul@1089 | 962 | query, values = self.get_query( |
paul@1176 | 963 | "delete from quota_limits :condition", |
paul@1176 | 964 | columns, values) |
paul@1089 | 965 | |
paul@1089 | 966 | self.cursor.execute(query, values) |
paul@1089 | 967 | |
paul@1176 | 968 | for user_group, limit in limits.items(): |
paul@1089 | 969 | |
paul@1176 | 970 | columns = ["quota", "user_group", "quota_limit"] |
paul@1176 | 971 | values = [quota, user_group, limit] |
paul@1089 | 972 | |
paul@1176 | 973 | query, values = self.get_query( |
paul@1176 | 974 | "insert into quota_limits (:columns) values (:values)", |
paul@1176 | 975 | columns, values) |
paul@1089 | 976 | |
paul@1186 | 977 | self.cursor.execute(query, values) |
paul@1186 | 978 | |
paul@1089 | 979 | return True |
paul@1089 | 980 | |
paul@1083 | 981 | # Journal entry methods. |
paul@1083 | 982 | |
paul@1083 | 983 | def get_entries(self, quota, group, mutable=False): |
paul@1083 | 984 | |
paul@1083 | 985 | """ |
paul@1083 | 986 | Return a list of journal entries for the given 'quota' for the indicated |
paul@1083 | 987 | 'group'. |
paul@1083 | 988 | """ |
paul@1083 | 989 | |
paul@1193 | 990 | return self.get_freebusy_for_other(quota, group, mutable) |
paul@1083 | 991 | |
paul@1083 | 992 | def set_entries(self, quota, group, entries): |
paul@1083 | 993 | |
paul@1083 | 994 | """ |
paul@1083 | 995 | For the given 'quota' and indicated 'group', set the list of journal |
paul@1083 | 996 | 'entries'. |
paul@1083 | 997 | """ |
paul@1083 | 998 | |
paul@1193 | 999 | return self.set_freebusy_for_other(quota, entries, group) |
paul@1193 | 1000 | |
paul@1193 | 1001 | # Compatibility methods. |
paul@1193 | 1002 | |
paul@1193 | 1003 | def get_freebusy_for_other(self, user, other, mutable=False): |
paul@1193 | 1004 | return DatabaseStore.get_freebusy_for_other(self, user, other, mutable, cls=FreeBusyGroupDatabaseCollection) |
paul@1193 | 1005 | |
paul@1193 | 1006 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@1193 | 1007 | return DatabaseStore.set_freebusy_for_other(self, user, freebusy, other, cls=FreeBusyGroupDatabaseCollection) |
paul@1083 | 1008 | |
paul@1083 | 1009 | # vim: tabstop=4 expandtab shiftwidth=4 |