From c56981690212b2e25350c33de47bebd8d7fafc94 Mon Sep 17 00:00:00 2001 From: Adrian Turjak Date: Fri, 6 Apr 2018 12:35:41 +1200 Subject: [PATCH] Make datetime strings consistent for future parsing Lets set a default timestamp format for any datetime strings we know we need to parse later. While we can always parse the direct output of what we have now, lets control it so we always have a valid format as expected. Change-Id: I4af359ea30ace9361c9c5d21e91a0528076eb892 --- adjutant/actions/v1/base.py | 5 +++-- adjutant/actions/v1/projects.py | 3 ++- adjutant/common/constants.py | 17 +++++++++++++++++ adjutant/common/utils.py | 24 ++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 adjutant/common/constants.py create mode 100644 adjutant/common/utils.py diff --git a/adjutant/actions/v1/base.py b/adjutant/actions/v1/base.py index 4f6a4ec..e9341ee 100644 --- a/adjutant/actions/v1/base.py +++ b/adjutant/actions/v1/base.py @@ -19,6 +19,7 @@ from django.utils import timezone from adjutant.common.quota import QuotaManager from adjutant.common import user_store +from adjutant.common.utils import str_datetime from adjutant.actions.models import Action @@ -331,7 +332,7 @@ class UserMixin(ResourceMixin): user = id_manager.create_user( name=self.username, password=password, email=self.email, domain=self.domain_id, - created_on=str(timezone.now())) + created_on=str_datetime(timezone.now())) except Exception as e: # TODO: Narrow the Exceptions caught to a relevant set. self.add_note( @@ -410,7 +411,7 @@ class ProjectMixin(ResourceMixin): description = getattr(self, "description", "") try: project = id_manager.create_project( - self.project_name, created_on=str(timezone.now()), + self.project_name, created_on=str_datetime(timezone.now()), parent=self.parent_id, domain=self.domain_id, description=description) except Exception as e: diff --git a/adjutant/actions/v1/projects.py b/adjutant/actions/v1/projects.py index de6a0a3..cbb6213 100644 --- a/adjutant/actions/v1/projects.py +++ b/adjutant/actions/v1/projects.py @@ -17,6 +17,7 @@ from uuid import uuid4 from django.utils import timezone from adjutant.common import user_store +from adjutant.common.utils import str_datetime from adjutant.actions.v1.base import ( BaseAction, UserNameAction, UserMixin, ProjectMixin) @@ -261,7 +262,7 @@ class NewProjectWithUserAction(UserNameAction, ProjectMixin, UserMixin): user = id_manager.create_user( name=self.username, password=password, email=self.email, domain=self.domain_id, - created_on=str(timezone.now())) + created_on=str_datetime(timezone.now())) self.set_cache('user_id', user.id) else: user = id_manager.get_user(user_id) diff --git a/adjutant/common/constants.py b/adjutant/common/constants.py new file mode 100644 index 0000000..528fa10 --- /dev/null +++ b/adjutant/common/constants.py @@ -0,0 +1,17 @@ +# Copyright (C) 2018 Catalyst IT Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# Date formats to use when storing time data we expect to parse. +DATE_FORMAT = "%Y-%m-%dT%H:%M:%S" +DATE_FORMAT_MS = "%Y-%m-%dT%H:%M:%S.%f" diff --git a/adjutant/common/utils.py b/adjutant/common/utils.py new file mode 100644 index 0000000..a0b7ef2 --- /dev/null +++ b/adjutant/common/utils.py @@ -0,0 +1,24 @@ +# Copyright (C) 2018 Catalyst IT Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from datetime import datetime + +from adjutant.common import constants + + +def str_datetime(datetime_obj, include_ms=False): + if include_ms: + return datetime.strftime(datetime_obj, constants.DATE_FORMAT_MS) + else: + return datetime.strftime(datetime_obj, constants.DATE_FORMAT)