1.1 --- a/imiptools/stores/database/common.py Fri May 26 23:58:06 2017 +0200
1.2 +++ b/imiptools/stores/database/common.py Thu Jun 01 23:26:38 2017 +0200
1.3 @@ -31,6 +31,12 @@
1.4
1.5 def first(l): return l[0]
1.6
1.7 +def have_table(obj, collection, table_name):
1.8 +
1.9 + "Return whether 'obj' is a 'collection' using the given 'table_name'."
1.10 +
1.11 + return isinstance(obj, collection) and obj.table_name == table_name
1.12 +
1.13 # Store classes.
1.14
1.15 class DatabaseStoreBase(DatabaseOperations):
1.16 @@ -463,42 +469,42 @@
1.17
1.18 # Free/busy period access.
1.19
1.20 - def get_freebusy(self, user, name=None, mutable=False, cls=None):
1.21 + def get_freebusy(self, user, name=None, mutable=False, collection=None):
1.22
1.23 "Get free/busy details for the given 'user'."
1.24
1.25 table = name or "freebusy"
1.26 - cls = cls or FreeBusyDatabaseCollection
1.27 - return cls(self.cursor, table, ["store_user"], [user], mutable, self.paramstyle)
1.28 + collection = collection or FreeBusyDatabaseCollection
1.29 + return collection(self.cursor, table, ["store_user"], [user], mutable, self.paramstyle)
1.30
1.31 - def get_freebusy_for_other(self, user, other, mutable=False, cls=None):
1.32 + def get_freebusy_for_other(self, user, other, mutable=False, collection=None):
1.33
1.34 "For the given 'user', get free/busy details for the 'other' user."
1.35
1.36 - cls = cls or FreeBusyDatabaseCollection
1.37 - return cls(self.cursor, self.freebusy_other_table, ["store_user", "other"], [user, other], mutable, self.paramstyle)
1.38 + collection = collection or FreeBusyDatabaseCollection
1.39 + return collection(self.cursor, self.freebusy_other_table, ["store_user", "other"], [user, other], mutable, self.paramstyle)
1.40
1.41 - def set_freebusy(self, user, freebusy, name=None, cls=None):
1.42 + def set_freebusy(self, user, freebusy, name=None, collection=None):
1.43
1.44 "For the given 'user', set 'freebusy' details."
1.45
1.46 table = name or "freebusy"
1.47 - cls = cls or FreeBusyDatabaseCollection
1.48 + collection = collection or FreeBusyDatabaseCollection
1.49
1.50 - if not isinstance(freebusy, cls) or freebusy.table_name != table:
1.51 - fbc = cls(self.cursor, table, ["store_user"], [user], True, self.paramstyle)
1.52 + if not have_table(freebusy, collection, table):
1.53 + fbc = collection(self.cursor, table, ["store_user"], [user], True, self.paramstyle)
1.54 fbc += freebusy
1.55
1.56 return True
1.57
1.58 - def set_freebusy_for_other(self, user, freebusy, other, cls=None):
1.59 + def set_freebusy_for_other(self, user, freebusy, other, collection=None):
1.60
1.61 "For the given 'user', set 'freebusy' details for the 'other' user."
1.62
1.63 - cls = cls or FreeBusyDatabaseCollection
1.64 + collection = collection or FreeBusyDatabaseCollection
1.65
1.66 - if not isinstance(freebusy, cls) or freebusy.table_name != self.freebusy_other_table:
1.67 - fbc = cls(self.cursor, self.freebusy_other_table, ["store_user", "other"], [user, other], True, self.paramstyle)
1.68 + if not have_table(freebusy, collection, self.freebusy_other_table):
1.69 + fbc = collection(self.cursor, self.freebusy_other_table, ["store_user", "other"], [user, other], True, self.paramstyle)
1.70 fbc += freebusy
1.71
1.72 return True
1.73 @@ -544,7 +550,7 @@
1.74
1.75 "For the given 'user', set 'freebusy' offers."
1.76
1.77 - return self.set_freebusy(user, freebusy, "freebusy_offers", cls=FreeBusyOffersDatabaseCollection)
1.78 + return self.set_freebusy(user, freebusy, "freebusy_offers", collection=FreeBusyOffersDatabaseCollection)
1.79
1.80 # Requests and counter-proposals.
1.81
1.82 @@ -1037,9 +1043,9 @@
1.83 # Compatibility methods.
1.84
1.85 def get_freebusy_for_other(self, user, other, mutable=False):
1.86 - return DatabaseStore.get_freebusy_for_other(self, user, other, mutable, cls=FreeBusyGroupDatabaseCollection)
1.87 + return DatabaseStore.get_freebusy_for_other(self, user, other, mutable, collection=FreeBusyGroupDatabaseCollection)
1.88
1.89 def set_freebusy_for_other(self, user, freebusy, other):
1.90 - return DatabaseStore.set_freebusy_for_other(self, user, freebusy, other, cls=FreeBusyGroupDatabaseCollection)
1.91 + return DatabaseStore.set_freebusy_for_other(self, user, freebusy, other, collection=FreeBusyGroupDatabaseCollection)
1.92
1.93 # vim: tabstop=4 expandtab shiftwidth=4