server/general: use UTC time stamps

This commit is contained in:
rr- 2016-07-03 14:46:15 +02:00
parent 1f413763bd
commit ac23067fdf
17 changed files with 31 additions and 31 deletions

View file

@ -42,7 +42,7 @@ class CommentDetailApi(BaseApi):
infix = 'own' if ctx.user.user_id == comment.user_id else 'any' infix = 'own' if ctx.user.user_id == comment.user_id else 'any'
text = ctx.get_param_as_string('text', required=True) text = ctx.get_param_as_string('text', required=True)
auth.verify_privilege(ctx.user, 'comments:edit:%s' % infix) 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) comments.update_comment_text(comment, text)
ctx.session.commit() ctx.session.commit()
return _serialize(ctx, comment) return _serialize(ctx, comment)

View file

@ -20,7 +20,7 @@ class InfoApi(BaseApi):
'featuringTime': post_feature.time if post_feature else None, 'featuringTime': post_feature.time if post_feature else None,
'featuringUser': users.serialize_user(post_feature.user, ctx.user) \ 'featuringUser': users.serialize_user(post_feature.user, ctx.user) \
if post_feature else None, if post_feature else None,
'serverTime': datetime.datetime.now(), 'serverTime': datetime.datetime.utcnow(),
'config': { 'config': {
'userNameRegex': config.config['user_name_regex'], 'userNameRegex': config.config['user_name_regex'],
'passwordRegex': config.config['password_regex'], 'passwordRegex': config.config['password_regex'],
@ -34,7 +34,7 @@ class InfoApi(BaseApi):
def _get_disk_usage(self): def _get_disk_usage(self):
threshold = datetime.timedelta(hours=1) threshold = datetime.timedelta(hours=1)
now = datetime.datetime.now() now = datetime.datetime.utcnow()
if self._cache_time and self._cache_time > now - threshold: if self._cache_time and self._cache_time > now - threshold:
return self._cache_result return self._cache_result
total_size = 0 total_size = 0

View file

@ -86,7 +86,7 @@ class PostDetailApi(BaseApi):
if ctx.has_file('thumbnail'): if ctx.has_file('thumbnail'):
auth.verify_privilege(ctx.user, 'posts:edit:thumbnail') auth.verify_privilege(ctx.user, 'posts:edit:thumbnail')
posts.update_post_thumbnail(post, ctx.get_file('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() ctx.session.flush()
snapshots.save_entity_modification(post, ctx.user) snapshots.save_entity_modification(post, ctx.user)
ctx.session.commit() ctx.session.commit()

View file

@ -81,7 +81,7 @@ class TagDetailApi(BaseApi):
implications = ctx.get_param_as_list('implications') implications = ctx.get_param_as_list('implications')
_create_if_needed(implications, ctx.user) _create_if_needed(implications, ctx.user)
tags.update_tag_implications(tag, implications) tags.update_tag_implications(tag, implications)
tag.last_edit_time = datetime.datetime.now() tag.last_edit_time = datetime.datetime.utcnow()
ctx.session.flush() ctx.session.flush()
snapshots.save_entity_modification(tag, ctx.user) snapshots.save_entity_modification(tag, ctx.user)
ctx.session.commit() ctx.session.commit()

View file

@ -4,7 +4,7 @@ class LruCacheItem(object):
def __init__(self, key, value): def __init__(self, key, value):
self.key = key self.key = key
self.value = value self.value = value
self.timestamp = datetime.now() self.timestamp = datetime.utcnow()
class LruCache(object): class LruCache(object):
def __init__(self, length, delta=None): def __init__(self, length, delta=None):

View file

@ -37,7 +37,7 @@ def create_comment(user, post, text):
comment.user = user comment.user = user
comment.post = post comment.post = post
update_comment_text(comment, text) update_comment_text(comment, text)
comment.creation_time = datetime.datetime.now() comment.creation_time = datetime.datetime.utcnow()
return comment return comment
def update_comment_text(comment, text): def update_comment_text(comment, text):

View file

@ -32,5 +32,5 @@ def set_favorite(entity, user):
fav_entity = table() fav_entity = table()
setattr(fav_entity, get_column(table).name, get_column(entity)) setattr(fav_entity, get_column(table).name, get_column(entity))
fav_entity.user = user fav_entity.user = user
fav_entity.time = datetime.datetime.now() fav_entity.time = datetime.datetime.utcnow()
db.session.add(fav_entity) db.session.add(fav_entity)

View file

@ -153,7 +153,7 @@ def create_post(content, tag_names, user):
post = db.Post() post = db.Post()
post.safety = db.Post.SAFETY_SAFE post.safety = db.Post.SAFETY_SAFE
post.user = user post.user = user
post.creation_time = datetime.datetime.now() post.creation_time = datetime.datetime.utcnow()
post.flags = [] post.flags = []
# we'll need post ID # we'll need post ID
@ -293,7 +293,7 @@ def update_post_flags(post, flags):
def feature_post(post, user): def feature_post(post, user):
post_feature = db.PostFeature() post_feature = db.PostFeature()
post_feature.time = datetime.datetime.now() post_feature.time = datetime.datetime.utcnow()
post_feature.post = post post_feature.post = post
post_feature.user = user post_feature.user = user
db.session.add(post_feature) db.session.add(post_feature)

View file

@ -55,5 +55,5 @@ def set_score(entity, user, score):
setattr(score_entity, get_column(table).name, get_column(entity)) setattr(score_entity, get_column(table).name, get_column(entity))
score_entity.score = score score_entity.score = score
score_entity.user = user score_entity.user = user
score_entity.time = datetime.datetime.now() score_entity.time = datetime.datetime.utcnow()
db.session.add(score_entity) db.session.add(score_entity)

View file

@ -82,7 +82,7 @@ def get_serialized_history(entity):
def _save(operation, entity, auth_user): def _save(operation, entity, auth_user):
resource_type, resource_id, resource_repr = db.util.get_resource_info(entity) resource_type, resource_id, resource_repr = db.util.get_resource_info(entity)
now = datetime.datetime.now() now = datetime.datetime.utcnow()
snapshot = db.Snapshot() snapshot = db.Snapshot()
snapshot.creation_time = now snapshot.creation_time = now

View file

@ -165,7 +165,7 @@ def merge_tags(source_tag, target_tag):
def create_tag(names, category_name, suggestions, implications): def create_tag(names, category_name, suggestions, implications):
tag = db.Tag() tag = db.Tag()
tag.creation_time = datetime.datetime.now() tag.creation_time = datetime.datetime.utcnow()
update_tag_names(tag, names) update_tag_names(tag, names)
update_tag_category_name(tag, category_name) update_tag_category_name(tag, category_name)
update_tag_suggestions(tag, suggestions) update_tag_suggestions(tag, suggestions)

View file

@ -107,7 +107,7 @@ def create_user(name, password, email):
user.rank = util.flip(auth.RANK_MAP)[config.config['default_rank']] user.rank = util.flip(auth.RANK_MAP)[config.config['default_rank']]
else: else:
user.rank = db.User.RANK_ADMINISTRATOR 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 user.avatar_style = db.User.AVATAR_GRAVATAR
return user return user
@ -185,7 +185,7 @@ def update_user_avatar(user, avatar_style, avatar_content):
avatar_style, ['gravatar', 'manual'])) avatar_style, ['gravatar', 'manual']))
def bump_user_login_time(user): 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): def reset_user_password(user):
password = auth.create_password() password = auth.create_password()

View file

@ -74,7 +74,7 @@ class dotdict(dict): # pylint: disable=invalid-name
__setattr__ = dict.__setitem__ __setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__ __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. ''' ''' Return tuple containing min/max time for given text representation. '''
one_day = datetime.timedelta(days=1) one_day = datetime.timedelta(days=1)
one_second = datetime.timedelta(seconds=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.') raise errors.ValidationError('Empty date format.')
if value == 'today': if value == 'today':
now = datetime.datetime.now(tz=timezone) now = datetime.datetime.utcnow()
return ( return (
datetime.datetime(now.year, now.month, now.day, 0, 0, 0), datetime.datetime(now.year, now.month, now.day, 0, 0, 0),
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) + one_day - one_second)
if value == 'yesterday': if value == 'yesterday':
now = datetime.datetime.now(tz=timezone) now = datetime.datetime.utcnow()
return ( 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) - one_day,
datetime.datetime(now.year, now.month, now.day, 0, 0, 0) \ datetime.datetime(now.year, now.month, now.day, 0, 0, 0) \

View file

@ -6,7 +6,7 @@ import falcon
def json_serializer(obj): def json_serializer(obj):
''' JSON serializer for objects not serializable by default JSON code ''' ''' JSON serializer for objects not serializable by default JSON code '''
if isinstance(obj, datetime.datetime): if isinstance(obj, datetime.datetime):
serial = obj.isoformat() serial = obj.isoformat('T') + 'Z'
return serial return serial
raise TypeError('Type not serializable') raise TypeError('Type not serializable')

View file

@ -43,7 +43,7 @@ def test_using_special_tokens(
post1 = test_ctx.post_factory(id=1) post1 = test_ctx.post_factory(id=1)
post2 = test_ctx.post_factory(id=2) post2 = test_ctx.post_factory(id=2)
post1.favorited_by = [db.PostFavorite( 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.add_all([post1, post2, auth_user])
db.session.flush() db.session.flush()
result = test_ctx.list_api.get( result = test_ctx.list_api.get(

View file

@ -56,8 +56,8 @@ def test_favorite_count(user_factory, post_factory):
post1 = post_factory() post1 = post_factory()
post2 = post_factory() post2 = post_factory()
db.session.add_all([ db.session.add_all([
db.PostFavorite(post=post1, time=datetime.now(), user=user), db.PostFavorite(post=post1, time=datetime.utcnow(), user=user),
db.PostFavorite(post=post2, time=datetime.now(), user=user_factory()), db.PostFavorite(post=post2, time=datetime.utcnow(), user=user_factory()),
]) ])
db.session.flush() db.session.flush()
db.session.refresh(user) db.session.refresh(user)
@ -72,8 +72,8 @@ def test_liked_post_count(user_factory, post_factory):
post1 = post_factory() post1 = post_factory()
post2 = post_factory() post2 = post_factory()
db.session.add_all([ db.session.add_all([
db.PostScore(post=post1, time=datetime.now(), user=user, score=1), db.PostScore(post=post1, time=datetime.utcnow(), user=user, score=1),
db.PostScore(post=post2, time=datetime.now(), user=user_factory(), score=1), db.PostScore(post=post2, time=datetime.utcnow(), user=user_factory(), score=1),
]) ])
db.session.flush() db.session.flush()
db.session.refresh(user) db.session.refresh(user)
@ -89,8 +89,8 @@ def test_disliked_post_count(user_factory, post_factory):
post1 = post_factory() post1 = post_factory()
post2 = post_factory() post2 = post_factory()
db.session.add_all([ db.session.add_all([
db.PostScore(post=post1, time=datetime.now(), user=user, score=-1), db.PostScore(post=post1, time=datetime.utcnow(), user=user, score=-1),
db.PostScore(post=post2, time=datetime.now(), user=user_factory(), score=1), db.PostScore(post=post2, time=datetime.utcnow(), user=user_factory(), score=1),
]) ])
db.session.flush() db.session.flush()
db.session.refresh(user) db.session.refresh(user)

View file

@ -6,7 +6,7 @@ from szurubooru import db, errors, search
def fav_factory(user_factory): def fav_factory(user_factory):
def factory(post, user=None): def factory(post, user=None):
return db.PostFavorite( 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 return factory
@pytest.fixture @pytest.fixture
@ -15,7 +15,7 @@ def score_factory(user_factory):
return db.PostScore( return db.PostScore(
post=post, post=post,
user=user or user_factory(), user=user or user_factory(),
time=datetime.datetime.now(), time=datetime.datetime.utcnow(),
score=score) score=score)
return factory return factory
@ -32,8 +32,8 @@ def feature_factory(user_factory):
def factory(post=None): def factory(post=None):
if post: if post:
return db.PostFeature( return db.PostFeature(
time=datetime.datetime.now(), user=user_factory(), post=post) time=datetime.datetime.utcnow(), user=user_factory(), post=post)
return db.PostFeature(time=datetime.datetime.now(), user=user_factory()) return db.PostFeature(time=datetime.datetime.utcnow(), user=user_factory())
return factory return factory
@pytest.fixture @pytest.fixture
@ -109,7 +109,7 @@ def test_filter_by_score(
db.session.add( db.session.add(
db.PostScore( db.PostScore(
score=post.post_id, score=post.post_id,
time=datetime.datetime.now(), time=datetime.datetime.utcnow(),
post=post, post=post,
user=user_factory())) user=user_factory()))
db.session.add_all([post1, post2, post3]) db.session.add_all([post1, post2, post3])