4 # Set this to the name of the table that stores the row.
5 # e.g. table_name = "nodes"
8 # Set this to the name of the primary key of the table. It is
9 # assumed that the this key is a sequence if it is not set when
11 # e.g. primary_key="record_id"
14 # Set this to the names of tables that reference this table's
20 Validates values. Will validate a value with a custom function
21 if a function named 'validate_[key]' exists.
23 # Warn about mandatory fields
24 # XX TODO: Support checking for mandatory fields later
25 #mandatory_fields = self.db.fields(self.table_name, notnull = True, hasdef = False)
26 #for field in mandatory_fields:
27 # if not self.has_key(field) or self[field] is None:
28 # raise GeniInvalidArgument, field + " must be specified and cannot be unset in class %s"%self.__class__.__name__
30 # Validate values before committing
31 for key, value in self.iteritems():
32 if value is not None and hasattr(self, 'validate_' + key):
33 validate = getattr(self, 'validate_' + key)
34 self[key] = validate(value)
37 def validate_timestamp(self, timestamp, check_future = False):
39 Validates the specified GMT timestamp string (must be in
40 %Y-%m-%d %H:%M:%S format) or number (seconds since UNIX epoch,
41 i.e., 1970-01-01 00:00:00 GMT). If check_future is True,
42 raises an exception if timestamp is not in the future. Returns
43 a GMT timestamp string.
46 time_format = "%Y-%m-%d %H:%M:%S"
47 if isinstance(timestamp, StringTypes):
48 # calendar.timegm() is the inverse of time.gmtime()
49 timestamp = calendar.timegm(time.strptime(timestamp, time_format))
51 # Human readable timestamp string
52 human = time.strftime(time_format, time.gmtime(timestamp))
54 if check_future and timestamp < time.time():
55 raise GeniInvalidArgument, "'%s' not in the future" % human