diff options
author | Tomas Babej <tbabej@redhat.com> | 2014-01-09 11:14:56 +0100 |
---|---|---|
committer | Alexander Bokovoy <abokovoy@redhat.com> | 2014-05-05 18:57:29 +0300 |
commit | 1df696f5432a673a24ff5cb273fe068a7d88d6ea (patch) | |
tree | 38b8ab1445b03806a482c9e58949c169373cc630 /ipalib/rpc.py | |
parent | 093c72d60e94d9545e38f2ad90cb24e6f44cbd62 (diff) | |
download | freeipa-1df696f5432a673a24ff5cb273fe068a7d88d6ea.tar.gz freeipa-1df696f5432a673a24ff5cb273fe068a7d88d6ea.tar.xz freeipa-1df696f5432a673a24ff5cb273fe068a7d88d6ea.zip |
ipalib: Add DateTime parameter
Adds a parameter that represents a DateTime format using datetime.datetime
object from python's native datetime library.
In the CLI, accepts one of the following formats:
Accepts LDAP Generalized time without in the following format:
'%Y%m%d%H%M%SZ'
Accepts subset of values defined by ISO 8601:
'%Y-%m-%dT%H:%M:%SZ'
'%Y-%m-%dT%H:%MZ'
'%Y-%m-%dZ'
Also accepts above formats using ' ' (space) as a separator instead of 'T'.
As a simplification, it does not deal with timezone info and ISO 8601
values with timezone info (+-hhmm) are rejected. Values are expected
to be in the UTC timezone.
Values are saved to LDAP as LDAP Generalized time values in the format
'%Y%m%d%H%SZ' (no time fractions and UTC timezone is assumed). To avoid
confusion, in addition to subset of ISO 8601 values, the LDAP generalized
time in the format '%Y%m%d%H%M%SZ' is also accepted as an input (as this is the
format user will see on the output).
Part of: https://fedorahosted.org/freeipa/ticket/3306
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
Diffstat (limited to 'ipalib/rpc.py')
-rw-r--r-- | ipalib/rpc.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/ipalib/rpc.py b/ipalib/rpc.py index 73ae115b3..c44ffb6e1 100644 --- a/ipalib/rpc.py +++ b/ipalib/rpc.py @@ -33,6 +33,7 @@ Also see the `ipaserver.rpcserver` module. from types import NoneType from decimal import Decimal import sys +import datetime import os import locale import base64 @@ -41,17 +42,18 @@ import json import socket from urllib2 import urlparse -from xmlrpclib import (Binary, Fault, dumps, loads, ServerProxy, Transport, - ProtocolError, MININT, MAXINT) +from xmlrpclib import (Binary, Fault, DateTime, dumps, loads, ServerProxy, + Transport, ProtocolError, MININT, MAXINT) import kerberos from dns import resolver, rdatatype from dns.exception import DNSException from nss.error import NSPRError from ipalib.backend import Connectible +from ipalib.constants import LDAP_GENERALIZED_TIME_FORMAT from ipalib.errors import (public_errors, UnknownError, NetworkError, KerberosError, XMLRPCMarshallError, JSONError, ConversionError) -from ipalib import errors +from ipalib import errors, capabilities from ipalib.request import context, Connection from ipalib.util import get_current_principal from ipapython.ipa_log_manager import root_logger @@ -163,6 +165,14 @@ def xml_wrap(value, version): return unicode(value) if isinstance(value, DN): return str(value) + + # Encode datetime.datetime objects as xmlrpclib.DateTime objects + if isinstance(value, datetime.datetime): + if capabilities.client_has_capability(version, 'datetime_values'): + return DateTime(value) + else: + return value.strftime(LDAP_GENERALIZED_TIME_FORMAT) + assert type(value) in (unicode, int, long, float, bool, NoneType) return value @@ -196,6 +206,9 @@ def xml_unwrap(value, encoding='UTF-8'): if isinstance(value, Binary): assert type(value.data) is str return value.data + if isinstance(value, DateTime): + # xmlprc DateTime is converted to string of %Y%m%dT%H:%M:%S format + return datetime.datetime.strptime(str(value), "%Y%m%dT%H:%M:%S") assert type(value) in (unicode, int, float, bool, NoneType) return value @@ -266,6 +279,11 @@ def json_encode_binary(val, version): return {'__base64__': base64.b64encode(str(val))} elif isinstance(val, DN): return str(val) + elif isinstance(val, datetime.datetime): + if capabilities.client_has_capability(version, 'datetime_values'): + return {'__datetime__': val.strftime(LDAP_GENERALIZED_TIME_FORMAT)} + else: + return val.strftime(LDAP_GENERALIZED_TIME_FORMAT) else: return val @@ -293,6 +311,9 @@ def json_decode_binary(val): if isinstance(val, dict): if '__base64__' in val: return base64.b64decode(val['__base64__']) + elif '__datetime__' in val: + return datetime.datetime.strptime(val['__datetime__'], + LDAP_GENERALIZED_TIME_FORMAT) else: return dict((k, json_decode_binary(v)) for k, v in val.items()) elif isinstance(val, list): |