From ac23067fdfd6289e537ec10d82e3828736ea7a79 Mon Sep 17 00:00:00 2001 From: rr- Date: Sun, 3 Jul 2016 14:46:15 +0200 Subject: [PATCH] server/general: use UTC time stamps --- server/szurubooru/api/comment_api.py | 2 +- server/szurubooru/api/info_api.py | 4 ++-- server/szurubooru/api/post_api.py | 2 +- server/szurubooru/api/tag_api.py | 2 +- server/szurubooru/func/cache.py | 2 +- server/szurubooru/func/comments.py | 2 +- server/szurubooru/func/favorites.py | 2 +- server/szurubooru/func/posts.py | 4 ++-- server/szurubooru/func/scores.py | 2 +- server/szurubooru/func/snapshots.py | 2 +- server/szurubooru/func/tags.py | 2 +- server/szurubooru/func/users.py | 4 ++-- server/szurubooru/func/util.py | 6 +++--- server/szurubooru/middleware/context_adapter.py | 2 +- server/szurubooru/tests/api/test_post_retrieving.py | 2 +- server/szurubooru/tests/db/test_user.py | 12 ++++++------ .../tests/search/configs/test_post_search_config.py | 10 +++++----- 17 files changed, 31 insertions(+), 31 deletions(-) diff --git a/server/szurubooru/api/comment_api.py b/server/szurubooru/api/comment_api.py index 2d2dc9a..b7baa71 100644 --- a/server/szurubooru/api/comment_api.py +++ b/server/szurubooru/api/comment_api.py @@ -42,7 +42,7 @@ class CommentDetailApi(BaseApi): infix = 'own' if ctx.user.user_id == comment.user_id else 'any' text = ctx.get_param_as_string('text', required=True) auth.verify_privilege(ctx.user, 'comments:edit:%s' % infix) - comment.last_edit_time = datetime.datetime.now() + comment.last_edit_time = datetime.datetime.utcnow() comments.update_comment_text(comment, text) ctx.session.commit() return _serialize(ctx, comment) diff --git a/server/szurubooru/api/info_api.py b/server/szurubooru/api/info_api.py index e37ac2c..f0e1751 100644 --- a/server/szurubooru/api/info_api.py +++ b/server/szurubooru/api/info_api.py @@ -20,7 +20,7 @@ class InfoApi(BaseApi): 'featuringTime': post_feature.time if post_feature else None, 'featuringUser': users.serialize_user(post_feature.user, ctx.user) \ if post_feature else None, - 'serverTime': datetime.datetime.now(), + 'serverTime': datetime.datetime.utcnow(), 'config': { 'userNameRegex': config.config['user_name_regex'], 'passwordRegex': config.config['password_regex'], @@ -34,7 +34,7 @@ class InfoApi(BaseApi): def _get_disk_usage(self): threshold = datetime.timedelta(hours=1) - now = datetime.datetime.now() + now = datetime.datetime.utcnow() if self._cache_time and self._cache_time > now - threshold: return self._cache_result total_size = 0 diff --git a/server/szurubooru/api/post_api.py b/server/szurubooru/api/post_api.py index 147faab..52bf770 100644 --- a/server/szurubooru/api/post_api.py +++ b/server/szurubooru/api/post_api.py @@ -86,7 +86,7 @@ class PostDetailApi(BaseApi): if ctx.has_file('thumbnail'): auth.verify_privilege(ctx.user, 'posts:edit:thumbnail') posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) - post.last_edit_time = datetime.datetime.now() + post.last_edit_time = datetime.datetime.utcnow() ctx.session.flush() snapshots.save_entity_modification(post, ctx.user) ctx.session.commit() diff --git a/server/szurubooru/api/tag_api.py b/server/szurubooru/api/tag_api.py index 93f4aee..72e29ff 100644 --- a/server/szurubooru/api/tag_api.py +++ b/server/szurubooru/api/tag_api.py @@ -81,7 +81,7 @@ class TagDetailApi(BaseApi): implications = ctx.get_param_as_list('implications') _create_if_needed(implications, ctx.user) tags.update_tag_implications(tag, implications) - tag.last_edit_time = datetime.datetime.now() + tag.last_edit_time = datetime.datetime.utcnow() ctx.session.flush() snapshots.save_entity_modification(tag, ctx.user) ctx.session.commit() diff --git a/server/szurubooru/func/cache.py b/server/szurubooru/func/cache.py index 34a506c..d6f581b 100644 --- a/server/szurubooru/func/cache.py +++ b/server/szurubooru/func/cache.py @@ -4,7 +4,7 @@ class LruCacheItem(object): def __init__(self, key, value): self.key = key self.value = value - self.timestamp = datetime.now() + self.timestamp = datetime.utcnow() class LruCache(object): def __init__(self, length, delta=None): diff --git a/server/szurubooru/func/comments.py b/server/szurubooru/func/comments.py index 38a0926..46896a4 100644 --- a/server/szurubooru/func/comments.py +++ b/server/szurubooru/func/comments.py @@ -37,7 +37,7 @@ def create_comment(user, post, text): comment.user = user comment.post = post update_comment_text(comment, text) - comment.creation_time = datetime.datetime.now() + comment.creation_time = datetime.datetime.utcnow() return comment def update_comment_text(comment, text): diff --git a/server/szurubooru/func/favorites.py b/server/szurubooru/func/favorites.py index 1cb0a3c..40406a6 100644 --- a/server/szurubooru/func/favorites.py +++ b/server/szurubooru/func/favorites.py @@ -32,5 +32,5 @@ def set_favorite(entity, user): fav_entity = table() setattr(fav_entity, get_column(table).name, get_column(entity)) fav_entity.user = user - fav_entity.time = datetime.datetime.now() + fav_entity.time = datetime.datetime.utcnow() db.session.add(fav_entity) diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index 902680b..2e38857 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -153,7 +153,7 @@ def create_post(content, tag_names, user): post = db.Post() post.safety = db.Post.SAFETY_SAFE post.user = user - post.creation_time = datetime.datetime.now() + post.creation_time = datetime.datetime.utcnow() post.flags = [] # we'll need post ID @@ -293,7 +293,7 @@ def update_post_flags(post, flags): def feature_post(post, user): post_feature = db.PostFeature() - post_feature.time = datetime.datetime.now() + post_feature.time = datetime.datetime.utcnow() post_feature.post = post post_feature.user = user db.session.add(post_feature) diff --git a/server/szurubooru/func/scores.py b/server/szurubooru/func/scores.py index 646f1f4..4ed8632 100644 --- a/server/szurubooru/func/scores.py +++ b/server/szurubooru/func/scores.py @@ -55,5 +55,5 @@ def set_score(entity, user, score): setattr(score_entity, get_column(table).name, get_column(entity)) score_entity.score = score score_entity.user = user - score_entity.time = datetime.datetime.now() + score_entity.time = datetime.datetime.utcnow() db.session.add(score_entity) diff --git a/server/szurubooru/func/snapshots.py b/server/szurubooru/func/snapshots.py index f899041..832ac16 100644 --- a/server/szurubooru/func/snapshots.py +++ b/server/szurubooru/func/snapshots.py @@ -82,7 +82,7 @@ def get_serialized_history(entity): def _save(operation, entity, auth_user): resource_type, resource_id, resource_repr = db.util.get_resource_info(entity) - now = datetime.datetime.now() + now = datetime.datetime.utcnow() snapshot = db.Snapshot() snapshot.creation_time = now diff --git a/server/szurubooru/func/tags.py b/server/szurubooru/func/tags.py index 9532f70..e4eb2a1 100644 --- a/server/szurubooru/func/tags.py +++ b/server/szurubooru/func/tags.py @@ -165,7 +165,7 @@ def merge_tags(source_tag, target_tag): def create_tag(names, category_name, suggestions, implications): tag = db.Tag() - tag.creation_time = datetime.datetime.now() + tag.creation_time = datetime.datetime.utcnow() update_tag_names(tag, names) update_tag_category_name(tag, category_name) update_tag_suggestions(tag, suggestions) diff --git a/server/szurubooru/func/users.py b/server/szurubooru/func/users.py index 617b833..4b4cb30 100644 --- a/server/szurubooru/func/users.py +++ b/server/szurubooru/func/users.py @@ -107,7 +107,7 @@ def create_user(name, password, email): user.rank = util.flip(auth.RANK_MAP)[config.config['default_rank']] else: user.rank = db.User.RANK_ADMINISTRATOR - user.creation_time = datetime.datetime.now() + user.creation_time = datetime.datetime.utcnow() user.avatar_style = db.User.AVATAR_GRAVATAR return user @@ -185,7 +185,7 @@ def update_user_avatar(user, avatar_style, avatar_content): avatar_style, ['gravatar', 'manual'])) def bump_user_login_time(user): - user.last_login_time = datetime.datetime.now() + user.last_login_time = datetime.datetime.utcnow() def reset_user_password(user): password = auth.create_password() diff --git a/server/szurubooru/func/util.py b/server/szurubooru/func/util.py index 65fc196..ff6c385 100644 --- a/server/szurubooru/func/util.py +++ b/server/szurubooru/func/util.py @@ -74,7 +74,7 @@ class dotdict(dict): # pylint: disable=invalid-name __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__ -def parse_time_range(value, timezone=datetime.timezone(datetime.timedelta())): +def parse_time_range(value): ''' Return tuple containing min/max time for given text representation. ''' one_day = datetime.timedelta(days=1) one_second = datetime.timedelta(seconds=1) @@ -84,14 +84,14 @@ def parse_time_range(value, timezone=datetime.timezone(datetime.timedelta())): raise errors.ValidationError('Empty date format.') if value == 'today': - now = datetime.datetime.now(tz=timezone) + now = datetime.datetime.utcnow() return ( datetime.datetime(now.year, now.month, now.day, 0, 0, 0), datetime.datetime(now.year, now.month, now.day, 0, 0, 0) \ + one_day - one_second) if value == 'yesterday': - now = datetime.datetime.now(tz=timezone) + now = datetime.datetime.utcnow() return ( datetime.datetime(now.year, now.month, now.day, 0, 0, 0) - one_day, datetime.datetime(now.year, now.month, now.day, 0, 0, 0) \ diff --git a/server/szurubooru/middleware/context_adapter.py b/server/szurubooru/middleware/context_adapter.py index fb79f44..2a1816a 100644 --- a/server/szurubooru/middleware/context_adapter.py +++ b/server/szurubooru/middleware/context_adapter.py @@ -6,7 +6,7 @@ import falcon def json_serializer(obj): ''' JSON serializer for objects not serializable by default JSON code ''' if isinstance(obj, datetime.datetime): - serial = obj.isoformat() + serial = obj.isoformat('T') + 'Z' return serial raise TypeError('Type not serializable') diff --git a/server/szurubooru/tests/api/test_post_retrieving.py b/server/szurubooru/tests/api/test_post_retrieving.py index 1474e62..d62d32f 100644 --- a/server/szurubooru/tests/api/test_post_retrieving.py +++ b/server/szurubooru/tests/api/test_post_retrieving.py @@ -43,7 +43,7 @@ def test_using_special_tokens( post1 = test_ctx.post_factory(id=1) post2 = test_ctx.post_factory(id=2) post1.favorited_by = [db.PostFavorite( - user=auth_user, time=datetime.datetime.now())] + user=auth_user, time=datetime.datetime.utcnow())] db.session.add_all([post1, post2, auth_user]) db.session.flush() result = test_ctx.list_api.get( diff --git a/server/szurubooru/tests/db/test_user.py b/server/szurubooru/tests/db/test_user.py index 6168b74..d7a2f55 100644 --- a/server/szurubooru/tests/db/test_user.py +++ b/server/szurubooru/tests/db/test_user.py @@ -56,8 +56,8 @@ def test_favorite_count(user_factory, post_factory): post1 = post_factory() post2 = post_factory() db.session.add_all([ - db.PostFavorite(post=post1, time=datetime.now(), user=user), - db.PostFavorite(post=post2, time=datetime.now(), user=user_factory()), + db.PostFavorite(post=post1, time=datetime.utcnow(), user=user), + db.PostFavorite(post=post2, time=datetime.utcnow(), user=user_factory()), ]) db.session.flush() db.session.refresh(user) @@ -72,8 +72,8 @@ def test_liked_post_count(user_factory, post_factory): post1 = post_factory() post2 = post_factory() db.session.add_all([ - db.PostScore(post=post1, time=datetime.now(), user=user, score=1), - db.PostScore(post=post2, time=datetime.now(), user=user_factory(), score=1), + db.PostScore(post=post1, time=datetime.utcnow(), user=user, score=1), + db.PostScore(post=post2, time=datetime.utcnow(), user=user_factory(), score=1), ]) db.session.flush() db.session.refresh(user) @@ -89,8 +89,8 @@ def test_disliked_post_count(user_factory, post_factory): post1 = post_factory() post2 = post_factory() db.session.add_all([ - db.PostScore(post=post1, time=datetime.now(), user=user, score=-1), - db.PostScore(post=post2, time=datetime.now(), user=user_factory(), score=1), + db.PostScore(post=post1, time=datetime.utcnow(), user=user, score=-1), + db.PostScore(post=post2, time=datetime.utcnow(), user=user_factory(), score=1), ]) db.session.flush() db.session.refresh(user) diff --git a/server/szurubooru/tests/search/configs/test_post_search_config.py b/server/szurubooru/tests/search/configs/test_post_search_config.py index 55b24a8..91b053b 100644 --- a/server/szurubooru/tests/search/configs/test_post_search_config.py +++ b/server/szurubooru/tests/search/configs/test_post_search_config.py @@ -6,7 +6,7 @@ from szurubooru import db, errors, search def fav_factory(user_factory): def factory(post, user=None): return db.PostFavorite( - post=post, user=user or user_factory(), time=datetime.datetime.now()) + post=post, user=user or user_factory(), time=datetime.datetime.utcnow()) return factory @pytest.fixture @@ -15,7 +15,7 @@ def score_factory(user_factory): return db.PostScore( post=post, user=user or user_factory(), - time=datetime.datetime.now(), + time=datetime.datetime.utcnow(), score=score) return factory @@ -32,8 +32,8 @@ def feature_factory(user_factory): def factory(post=None): if post: return db.PostFeature( - time=datetime.datetime.now(), user=user_factory(), post=post) - return db.PostFeature(time=datetime.datetime.now(), user=user_factory()) + time=datetime.datetime.utcnow(), user=user_factory(), post=post) + return db.PostFeature(time=datetime.datetime.utcnow(), user=user_factory()) return factory @pytest.fixture @@ -109,7 +109,7 @@ def test_filter_by_score( db.session.add( db.PostScore( score=post.post_id, - time=datetime.datetime.now(), + time=datetime.datetime.utcnow(), post=post, user=user_factory())) db.session.add_all([post1, post2, post3])