summaryrefslogtreecommitdiff
path: root/Mailman/database/types.py
diff options
context:
space:
mode:
authorBarry Warsaw2007-08-05 00:32:09 -0400
committerBarry Warsaw2007-08-05 00:32:09 -0400
commit959f34a62e0ec3cbe73da3d43640ccb6791cf3a0 (patch)
treeafcf868061fe6a5b56aeb7493c1e72e215fcce1a /Mailman/database/types.py
parentec734fab4791c107610caf73931e570b2d1b6bd0 (diff)
downloadmailman-959f34a62e0ec3cbe73da3d43640ccb6791cf3a0.tar.gz
mailman-959f34a62e0ec3cbe73da3d43640ccb6791cf3a0.tar.zst
mailman-959f34a62e0ec3cbe73da3d43640ccb6791cf3a0.zip
Diffstat (limited to 'Mailman/database/types.py')
-rw-r--r--Mailman/database/types.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/Mailman/database/types.py b/Mailman/database/types.py
index 79ea8767d..0f0e46fa3 100644
--- a/Mailman/database/types.py
+++ b/Mailman/database/types.py
@@ -17,11 +17,12 @@
import sys
+from datetime import timedelta
from sqlalchemy import types
-# SQLAlchemy custom type for storing enums in the database.
+# SQLAlchemy custom type for storing munepy Enums in the database.
class EnumType(types.TypeDecorator):
# Enums can be stored as strings of the form:
# full.path.to.Enum:intval
@@ -30,7 +31,7 @@ class EnumType(types.TypeDecorator):
def convert_bind_param(self, value, engine):
if value is None:
return None
- return '%s:%s.%d' % (value.enumclass.__module__,
+ return '%s.%s:%d' % (value.enumclass.__module__,
value.enumclass.__name__,
int(value))
@@ -38,7 +39,27 @@ class EnumType(types.TypeDecorator):
if value is None:
return None
path, intvalue = value.rsplit(':', 1)
- modulename, classname = intvalue.rsplit('.', 1)
+ modulename, classname = path.rsplit('.', 1)
__import__(modulename)
cls = getattr(sys.modules[modulename], classname)
return cls[int(intvalue)]
+
+
+
+class TimeDeltaType(types.TypeDecorator):
+ # timedeltas are stored as the string representation of three integers,
+ # separated by colons. The values represent the three timedelta
+ # attributes days, seconds, microseconds.
+ impl = types.String
+
+ def convert_bind_param(self, value, engine):
+ if value is None:
+ return None
+ return '%s:%s:%s' % (value.days, value.seconds, value.microseconds)
+
+ def convert_result_value(self, value, engine):
+ if value is None:
+ return None
+ parts = value.split(':')
+ assert len(parts) == 3, 'Bad timedelta representation: %s' % value
+ return timedelta(*(int(value) for value in parts))