diff --git a/account/middleware.py b/account/middleware.py index 29e1ea77..9bc6be4c 100644 --- a/account/middleware.py +++ b/account/middleware.py @@ -1,6 +1,8 @@ import time +import pytz from django.contrib import auth +from django.utils import timezone from django.utils.translation import ugettext as _ from utils.api import JSONResponse @@ -26,3 +28,9 @@ class AdminRequiredMiddleware(object): if path.startswith("/admin/") or path.startswith("/api/admin/"): if not(request.user.is_authenticated() and request.user.is_admin()): return JSONResponse.response({"error": "login-required", "data": _("Please login in first")}) + + +class TimezoneMiddleware(object): + def process_request(self, request): + if request.user.is_authenticated(): + timezone.activate(pytz.timezone(request.user.userprofile.time_zone)) diff --git a/deploy/requirements.txt b/deploy/requirements.txt index 47f3b9f2..4cacff32 100644 --- a/deploy/requirements.txt +++ b/deploy/requirements.txt @@ -4,3 +4,4 @@ pillow jsonfield otpauth flake8-quotes +pytz diff --git a/utils/management/commands/initadmin.py b/utils/management/commands/initadmin.py index e33359e8..59cdf4df 100644 --- a/utils/management/commands/initadmin.py +++ b/utils/management/commands/initadmin.py @@ -32,7 +32,7 @@ class Command(BaseCommand): rand_password = "rootroot" user.set_password(rand_password) user.save() - UserProfile.objects.create(user=user) + UserProfile.objects.create(user=user, time_zone="Asia/Shanghai") self.stdout.write(self.style.SUCCESS("Successfully created super admin user.\n" "Username: root\nPassword: %s\n" "Remember to change password and turn on two factors auth "