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
This commit is contained in:
Adrian Turjak 2018-04-06 12:35:41 +12:00
parent 59fc30c993
commit c569816902
4 changed files with 46 additions and 3 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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"

24
adjutant/common/utils.py Normal file
View File

@ -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)