diff options
| author | Barry Warsaw | 2010-08-12 12:58:24 -0400 |
|---|---|---|
| committer | Barry Warsaw | 2010-08-12 12:58:24 -0400 |
| commit | 81663e54791c4e0ccbf54c89b168d8b11f05eba8 (patch) | |
| tree | c976878b745bb96fff834ae3959e38d6eef6b446 /src/mailman/rest/helpers.py | |
| parent | 255c63fd28671c6b0366ef745db0b08fdfec3267 (diff) | |
| download | mailman-81663e54791c4e0ccbf54c89b168d8b11f05eba8.tar.gz mailman-81663e54791c4e0ccbf54c89b168d8b11f05eba8.tar.zst mailman-81663e54791c4e0ccbf54c89b168d8b11f05eba8.zip | |
Diffstat (limited to 'src/mailman/rest/helpers.py')
| -rw-r--r-- | src/mailman/rest/helpers.py | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/mailman/rest/helpers.py b/src/mailman/rest/helpers.py index 4bef720ef..c6a020f61 100644 --- a/src/mailman/rest/helpers.py +++ b/src/mailman/rest/helpers.py @@ -32,7 +32,8 @@ __all__ = [ import json import hashlib -from datetime import datetime +from datetime import datetime, timedelta +from flufl.enum import Enum from lazr.config import as_boolean from restish.http import Response from restish.resource import MethodDecorator @@ -69,6 +70,17 @@ class ExtendedEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime): return obj.isoformat() + elif isinstance(obj, timedelta): + # as_timedelta() does not recognize microseconds, so convert these + # to floating seconds, but only if there are any seconds. + if obj.seconds > 0 or obj.microseconds > 0: + seconds = obj.seconds + obj.microseconds / 1000000.0 + return '{0}d{1}s'.format(obj.days, seconds) + return '{0}d'.format(obj.days) + elif hasattr(obj, 'enumclass') and issubclass(obj.enumclass, Enum): + # It's up to the decoding validator to associate this name with + # the right Enum class. + return obj.enumname return json.JSONEncoder.default(self, obj) |
