3 Representation of a row in a database table. To use, optionally
4 instantiate with a dict of values. Update as you would a
5 dict. Commit to the database with flush().
8 # Set this to a dict of the valid columns in this table. If a column
9 # name ends in 's' and the column value is set by referring to the
10 # column without the 's', it is assumed that the column values
11 # should be aggregated into lists. For example, if fields contains
12 # the column 'role_ids' and row['role_id'] is set repeatedly to
13 # different values, row['role_ids'] will contain a list of the set
17 # These fields are derived from join tables and are not actually
21 # These fields are derived from join tables and are not returned
22 # by default unless specified.
25 def __init__(self, fields):
28 def update(self, fields):
29 for key, value in fields.iteritems():
30 self.__setitem__(key, value)
32 def __setitem__(self, key, value):
34 Magically takes care of aggregating certain variables into
39 all_fields = self.fields.keys() + \
40 self.join_fields.keys() + \
41 self.extra_fields.keys()
43 # Aggregate into lists
44 if (key + 's') in all_fields:
47 if value not in self[key] and value is not None:
48 self[key].append(value)
56 elif key in all_fields:
57 dict.__setitem__(self, key, value)
61 Validates values. Will validate a value with a custom function
62 if a function named 'validate_[key]' exists.
65 # Validate values before committing
66 # XXX Also truncate strings that are too long
67 for key, value in self.iteritems():
68 if value is not None and hasattr(self, 'validate_' + key):
69 validate = getattr(self, 'validate_' + key)
70 self[key] = validate(value)
72 def flush(self, commit = True):
74 Flush changes back to the database.
81 Representation of row(s) in a database table.
84 def flush(self, commit = True):
86 Flush changes back to the database.
89 for row in self.values():