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