diff --git a/AuthApp/funcs.py b/AuthApp/funcs.py index aa77bc8..ab412bd 100644 --- a/AuthApp/funcs.py +++ b/AuthApp/funcs.py @@ -1,5 +1,19 @@ from django.template.loader import render_to_string + +def get_user_timezone_Dict(user, request=None): + tz = None + if request: + tz = request.COOKIES.get("user_tz") + if not tz and user.is_authenticated: + tz = user.user_profile.get_timezone() + + if not tz: + from django.conf import settings + tz = settings.TIME_ZONE + + return {'user_tz': tz} + def get_dashboard_page_content_html(request): from ChatServiceApp.funcs import get_unanswered_msgs_count_for_user diff --git a/AuthApp/js_views.py b/AuthApp/js_views.py index 1aa7683..d064ff9 100644 --- a/AuthApp/js_views.py +++ b/AuthApp/js_views.py @@ -232,6 +232,7 @@ def chats_ajax(request): 'receivers': receivers, # 'messages': cur_chat_msgs } + Dict.update(get_user_timezone_Dict(request.user, request=request)) html = render_to_string('blocks/profile/b_chats.html', Dict, request=request) return JsonResponse({'html': html}, status=200) @@ -444,7 +445,7 @@ def login_ajax(request): -def send_registration_mail(data_Dict): +def send_registration_mail(data_Dict, user): try: @@ -463,7 +464,7 @@ def send_registration_mail(data_Dict): html = render_to_string('mail/m_registration.html', Dict) from BaseModels.mailSender import admin_send_mail_by_SMTPlib mail_sets = get_mail_send_options() - to = [mail_sets['sender_email'], 'web@syncsystems.net', 'sa@a3-global.com', 'sysadmin.hax@gmail.com'] + to = [user.email, 'web@syncsystems.net', 'sa@a3-global.com', 'sysadmin.hax@gmail.com'] res = admin_send_mail_by_SMTPlib( mail_sets, subject=subject, @@ -518,7 +519,7 @@ def registration_ajax(request): 'user': user, 'pass': form.data['password'] } - res = send_registration_mail(mail_Dict) + res = send_registration_mail(mail_Dict, user) res_Dict = { 'redirect_url': reverse('profile_page', args=['dashboard']) diff --git a/AuthApp/models.py b/AuthApp/models.py index 3ed08e8..56e0d9b 100644 --- a/AuthApp/models.py +++ b/AuthApp/models.py @@ -53,6 +53,12 @@ class UserProfile(BaseModel): mailing_on = models.BooleanField(default=False, verbose_name=_('Рассылка')) + def get_timezone(self): + tz = None + if 'user_timezone' in self.json_data: + tz = self.json_data['user_timezone'] + return tz + def save_user_alerts_to_session(self, request): for_save_to_session = self.get_node_by_name('for_save_to_session') if for_save_to_session: diff --git a/ChatServiceApp/funcs.py b/ChatServiceApp/funcs.py index ea5c475..4df1ded 100644 --- a/ChatServiceApp/funcs.py +++ b/ChatServiceApp/funcs.py @@ -13,7 +13,7 @@ from django.urls import reverse import json from datetime import datetime, time from django.conf import settings - +from AuthApp.funcs import get_user_timezone_Dict def get_unanswered_msgs_count_for_user(user): @@ -56,7 +56,6 @@ def get_update_chat_Dict(data): if data['sender'] == data['cur_user']: user = copy.copy(sender) cur_receiver = copy.copy(receiver) - # context_Dict.update({'user': sender}) else: user = copy.copy(receiver) cur_receiver = copy.copy(sender) @@ -65,8 +64,9 @@ def get_update_chat_Dict(data): # context_Dict.update({'cur_receiver': receiver}) context_Dict.update({ 'cur_receiver': cur_receiver, - 'user': user + 'user': user, }) + context_Dict.update(get_user_timezone_Dict(user)) if sender == receiver: print('!') @@ -112,7 +112,7 @@ def get_update_chat_Dict(data): context_Dict.update({ 'messages': msgs, - 'MEDIA_URL': settings.MEDIA_URL + 'MEDIA_URL': settings.MEDIA_URL, }) html = render_to_string(tpl_name, context_Dict) if required_full_support_chat_html: @@ -288,6 +288,7 @@ def send_msg(data): def get_chat_page_content_html(request, receiver_id=None): from AuthApp.models import User + from AuthApp.funcs import get_user_timezone_Dict msgs = [] try: @@ -307,6 +308,7 @@ def get_chat_page_content_html(request, receiver_id=None): 'receivers': receivers, 'page': 'chat', } + Dict.update(get_user_timezone_Dict(request.user, request=request)) tpl_name = 'blocks/profile/b_chats.html' html = render_to_string(tpl_name, Dict, request=request) diff --git a/ChatServiceApp/js_views.py b/ChatServiceApp/js_views.py index 1a13962..c896c34 100644 --- a/ChatServiceApp/js_views.py +++ b/ChatServiceApp/js_views.py @@ -53,6 +53,7 @@ def show_chat_w_user_ajax(request): data = json.loads(request.body) Dict = get_chat_page_content_Dict(request, data['user_id']) + Dict.update(get_user_timezone_Dict(request.user, request=request)) tpl_name = 'blocks/profile/b_chats.html' @@ -99,7 +100,7 @@ def update_chat_ajax2(request): receiver = User.objects.get(id=data['receiver']) context_Dict.update({'cur_receiver': receiver}) - + context_Dict.update(get_user_timezone_Dict(request.user, request=request)) if not ticket: @@ -190,6 +191,7 @@ def update_chat_ajax(request): receiver = User.objects.get(id=data['receiver']) context_Dict.update({'cur_receiver': receiver}) + context_Dict.update(get_user_timezone_Dict(request.user, request=request)) if ticket: diff --git a/TWB/settings.py b/TWB/settings.py index 5e3738b..cf81397 100644 --- a/TWB/settings.py +++ b/TWB/settings.py @@ -126,6 +126,8 @@ MIDDLEWARE = [ 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'AuthApp.middleware.ResponseInterceptionMiddleware', + 'TWB.tz_middelware.TimezoneMiddleware', + # 'tz_detect.middleware.TimezoneMiddleware', "allauth.account.middleware.AccountMiddleware", ] diff --git a/TWB/tz_middelware.py b/TWB/tz_middelware.py new file mode 100644 index 0000000..5c22305 --- /dev/null +++ b/TWB/tz_middelware.py @@ -0,0 +1,22 @@ +import zoneinfo +from django.utils import timezone +from django.shortcuts import render + +class TimezoneMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + tz = request.COOKIES.get("user_tz") + if tz: + if request.user.is_authenticated: + if not 'user_timezone' in request.user.user_profile.json_data or request.user.user_profile.json_data['user_timezone'] != tz: + request.user.user_profile.json_data['user_timezone'] = tz + request.user.user_profile.save(update_fields=['json_data']) + + msg = f'user={str(request.user.id)} tz={str(tz)}' + print(msg) + timezone.activate(zoneinfo.ZoneInfo(tz)) + else: + timezone.activate(zoneinfo.ZoneInfo("UTC")) + return self.get_response(request) \ No newline at end of file diff --git a/TWB/urls.py b/TWB/urls.py index 45731c3..cd3a843 100644 --- a/TWB/urls.py +++ b/TWB/urls.py @@ -13,6 +13,8 @@ urlpatterns = [ path('ckeditor/', include('ckeditor_uploader.urls')), path('i18n/', include('django.conf.urls.i18n')), + # path('tz_detect/', include('tz_detect.urls')), + path('accounts/signup/', login_View, name='signup'), path('accounts/login/cancelled/', login_View), diff --git a/requirements.pip b/requirements.pip index 75932d7..80e15ca 100644 --- a/requirements.pip +++ b/requirements.pip @@ -12,4 +12,6 @@ channels-redis==4.1.0 django-colorfield django-webpush==0.3.5 django-allauth==0.60.0 +pytz==2024.1 +#django-tz-detect==0.4.0 diff --git a/templates/blocks/profile/b_messages_container.html b/templates/blocks/profile/b_messages_container.html index 1af33bd..f7aab90 100644 --- a/templates/blocks/profile/b_messages_container.html +++ b/templates/blocks/profile/b_messages_container.html @@ -1,5 +1,5 @@ {% load static %} - +{% load tz %} {#{% include "widgets/w_file.html" %}#} {% if not messages and ticket %} @@ -8,7 +8,9 @@ {% for msg in messages %} {% include "widgets/w_message.html" %} {% if forloop.first %} - + {% timezone user_tz %} + + {% endtimezone %} {% endif %} {% endfor %} {% endif %} diff --git a/templates/tb_base.html b/templates/tb_base.html index c55556b..23b9958 100644 --- a/templates/tb_base.html +++ b/templates/tb_base.html @@ -134,6 +134,14 @@ {% include 'blocks/b_footer.html' %} + + diff --git a/templates/widgets/w_file.html b/templates/widgets/w_file.html index e0fb852..bf11392 100644 --- a/templates/widgets/w_file.html +++ b/templates/widgets/w_file.html @@ -1,5 +1,6 @@ {% load static %} {% load tt_chat %} +{% load tz %}
@@ -20,7 +21,9 @@
- {% if msg %}{{ msg.modifiedDT }}{% else %}{{ ticket.modifiedDT }}{% endif %} + {% timezone user_tz %} + {% if msg %}{{ msg.modifiedDT }}{% else %}{{ ticket.modifiedDT }}{% endif %} + {% endtimezone %}
diff --git a/templates/widgets/w_message.html b/templates/widgets/w_message.html index d5244aa..cf3525b 100644 --- a/templates/widgets/w_message.html +++ b/templates/widgets/w_message.html @@ -1,5 +1,7 @@ {% load static %} {% load tt_chat %} +{% load tz %} +
@@ -34,12 +36,17 @@ {% endif %}
- {% if msg %}{{ msg.modifiedDT }}{% else %}{{ ticket.modifiedDT }}{% endif %} + {% timezone user_tz %} + + {% if msg %}{{ msg.modifiedDT|localtime }}{% else %}{{ ticket.modifiedDT|localtime }}{% endif %} + + {% endtimezone %}
+{#{% tz_detect %}#} {#
#} {#
#} diff --git a/templates/widgets/w_request_tech_support.html b/templates/widgets/w_request_tech_support.html index 5d94897..bdd56de 100644 --- a/templates/widgets/w_request_tech_support.html +++ b/templates/widgets/w_request_tech_support.html @@ -1,5 +1,8 @@ {% load static %} {% load i18n %} +{% load tz %} + +
@@ -10,8 +13,9 @@ {# #} {# 10.02.2023#} - {{ ticket.modifiedDT|date:"d.m.Y" }} - + {% timezone user_tz %} + {{ ticket.modifiedDT|date:"d.m.Y" }} + {% endtimezone %}