From c68b1343be7d07bf8cbefe6027eea555a52ba6a1 Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Mon, 25 Sep 2006 15:17:17 +0000 Subject: [PATCH] - support new schema - use new table views instead of defining join_fields, extra_fields, and all_fields - rename flush() to sync() to be like shelve - get rid of weird magic aggregation, let table views do this --- PLC/Table.py | 47 ++++++++--------------------------------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/PLC/Table.py b/PLC/Table.py index 3505d412..04b45e77 100644 --- a/PLC/Table.py +++ b/PLC/Table.py @@ -2,29 +2,16 @@ class Row(dict): """ Representation of a row in a database table. To use, optionally instantiate with a dict of values. Update as you would a - dict. Commit to the database with flush(). + dict. Commit to the database with sync(). """ - # Set this to a dict of the valid columns in this table. If a column - # name ends in 's' and the column value is set by referring to the - # column without the 's', it is assumed that the column values - # should be aggregated into lists. For example, if fields contains - # the column 'role_ids' and row['role_id'] is set repeatedly to - # different values, row['role_ids'] will contain a list of the set - # values. + # Set this to a dict of the valid fields of this object. Not all + # fields (e.g., joined fields) may be updated via sync(). fields = {} - # These fields are derived from join tables and are not actually - # in the sites table. - join_fields = {} - - # These fields are derived from join tables and are not returned - # by default unless specified. - extra_fields = {} - def __init__(self, fields): self.update(fields) - + def update(self, fields): for key, value in fields.iteritems(): self.__setitem__(key, value) @@ -35,25 +22,7 @@ class Row(dict): lists. """ - # All known keys - all_fields = self.fields.keys() + \ - self.join_fields.keys() + \ - self.extra_fields.keys() - - # Aggregate into lists - if (key + 's') in all_fields: - key += 's' - try: - if value not in self[key] and value is not None: - self[key].append(value) - except KeyError: - if value is None: - self[key] = [] - else: - self[key] = [value] - return - - elif key in all_fields: + if key in self.fields: dict.__setitem__(self, key, value) def validate(self): @@ -69,7 +38,7 @@ class Row(dict): validate = getattr(self, 'validate_' + key) self[key] = validate(value) - def flush(self, commit = True): + def sync(self, commit = True): """ Flush changes back to the database. """ @@ -81,10 +50,10 @@ class Table(dict): Representation of row(s) in a database table. """ - def flush(self, commit = True): + def sync(self, commit = True): """ Flush changes back to the database. """ for row in self.values(): - row.flush(commit) + row.sync(commit) -- 2.47.0