removed another bunch of references to geni
[sfa.git] / sfa / util / row.py
1
2 class Row(dict):
3
4     # Set this to the name of the table that stores the row.
5     # e.g. table_name = "nodes"
6     table_name = None
7
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
10     # sync() is called.
11     # e.g. primary_key="record_id"
12     primary_key = None
13
14     # Set this to the names of tables that reference this table's
15     # primary key.
16     join_tables = []
17
18     def validate(self):
19         """
20         Validates values. Will validate a value with a custom function
21         if a function named 'validate_[key]' exists.
22         """
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 SfaInvalidArgument, field + " must be specified and cannot be unset in class %s"%self.__class__.__name__
29
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)
35
36
37     def validate_timestamp(self, timestamp, check_future = False):
38         """
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.
44         """
45
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))
50
51         # Human readable timestamp string
52         human = time.strftime(time_format, time.gmtime(timestamp))
53
54         if check_future and timestamp < time.time():
55             raise SfaInvalidArgument, "'%s' not in the future" % human
56
57         return human