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