X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FTable.py;h=9041c260e8ff588bfb343774baeadccb64100af2;hb=9cffc9f1869249a70c527a784c64ddc902ee8d48;hp=95244636439d1aaa16d9e785dae5c42bfc8e0229;hpb=50310719970c6a669e876ac3d33562e899879409;p=plcapi.git diff --git a/PLC/Table.py b/PLC/Table.py index 9524463..9041c26 100644 --- a/PLC/Table.py +++ b/PLC/Table.py @@ -1,3 +1,6 @@ +import time +import calendar + from PLC.Faults import * from PLC.Parameter import Parameter @@ -39,7 +42,7 @@ class Row(dict): 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(): @@ -47,6 +50,19 @@ class Row(dict): 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.