+import time
+import calendar
+
from PLC.Faults import *
from PLC.Parameter import Parameter
mandatory_fields = self.api.db.fields(self.table_name, notnull = True, hasdef = False)
for field in mandatory_fields:
if not self.has_key(field) or self[field] is None:
- raise PLCInvalidArgument, field + " must be specified and cannot be unset"
+ raise PLCInvalidArgument, field + " must be specified and cannot be unset in class %s"%self.__class__.__name__
# Validate values before committing
for key, value in self.iteritems():
validate = getattr(self, 'validate_' + key)
self[key] = validate(value)
+ time_format = "%Y-%m-%d %H:%M:%S"
+ def validate_timestamp (self, timestamp, check_future=False):
+ # in case we try to sync the same object twice
+ if isinstance(timestamp,str):
+ # calendar.timegm is the inverse of time.gmtime, in that it computes in UTC
+ # surprisingly enough, no other method in the time module behaves this way
+ # this method is documented in the time module's documentation
+ timestamp = calendar.timegm (time.strptime (timestamp,Row.time_format))
+ human = time.strftime (Row.time_format, time.gmtime(timestamp))
+ if check_future and (timestamp < time.time()):
+ raise PLCInvalidArgument, "%s: date must be in the future"%human
+ return human
+
def sync(self, commit = True, insert = None):
"""
Flush changes back to the database.