"""
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)
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):
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.
"""
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)