Add the timezone property to app

It will make it easier to get a default time zone.
This commit is contained in:
2018-07-08 22:28:29 +02:00
parent ba7c766316
commit 943021e152
2 changed files with 35 additions and 8 deletions

View File

@@ -111,19 +111,18 @@ class User(db.Model, UserMixin):
@property
def timezone(self):
from flask import current_app
from pytz import timezone, UTC
from pytz import timezone
from pytz.exceptions import UnknownTimeZoneError
timezone_str = self.settings['timezone']
if not timezone_str:
timezone_str = current_app.settings.get('DEFAULT_TIMEZONE', 'UTC')
if timezone_str:
try:
return timezone(timezone_str)
except UnknownTimeZoneError:
warn(f'Timezone of {self} (or the default timezone) "{timezone_str}" is invalid')
try:
return timezone(timezone_str)
except pytz.exceptions.UnknownTimeZoneError:
warn(f'Timezone of {user} (or the default timezone) "{timezone_str}" is invalid')
return UTC
return current_app.timezone
def __repr__(self):
return f'<User {self.id}({self.username})>'