From bf39639acc1f1c519dc1c69f9108a3d7972c930b Mon Sep 17 00:00:00 2001 From: SDE Date: Fri, 28 Jun 2024 18:02:57 +0300 Subject: [PATCH] 1.3.2 send_mail_for_user_subscribes_that_is_going_to_finish --- .../management/commands/every_1hour_start.py | 48 ++++++++++++ GeneralApp/views.py | 60 +------------- .../management/commands/every_1hour_start.py | 36 --------- SubscribesApp/funcs.py | 11 +++ SubscribesApp/reports.py | 49 ++++++++++++ ...er_subscribes_that_is_going_to_finish.html | 78 +++++++++++++++++++ 6 files changed, 188 insertions(+), 94 deletions(-) create mode 100644 GeneralApp/management/commands/every_1hour_start.py delete mode 100644 RoutesApp/management/commands/every_1hour_start.py create mode 100644 SubscribesApp/reports.py create mode 100644 templates/mail/m_user_subscribes_that_is_going_to_finish.html diff --git a/GeneralApp/management/commands/every_1hour_start.py b/GeneralApp/management/commands/every_1hour_start.py new file mode 100644 index 0000000..95c056f --- /dev/null +++ b/GeneralApp/management/commands/every_1hour_start.py @@ -0,0 +1,48 @@ +from django.core.management.base import BaseCommand +from datetime import datetime +from BaseModels.mailSender import techSendMail +from ...funcs_options import get_options_by_opt_types, get_mail_send_options + +class Command(BaseCommand): + + def handle(self, *args, **options): + mail_sets = get_mail_send_options() + + log = '' + log_begin_DT = datetime.now() + msg = str(log_begin_DT) + print('-------------') + print(msg) + + try: + from RoutesApp.search_matches import search_matches + msg = search_matches() + if msg: + print(msg) + log += f'\n{msg}' + except Exception as e: + msg = f'every_1hour_start search_matches fail = {str(e)}' + print(msg) + techSendMail(mail_sets, msg, title='every_1hour_start search_matches') + + + if datetime.now().hour == 19: + try: + from SubscribesApp.reports import send_mail_for_user_subscribes_that_is_going_to_finish + msg = send_mail_for_user_subscribes_that_is_going_to_finish() + if msg: + print(msg) + log += f'\n{msg}' + except Exception as e: + msg = f'send_mail_for_user_subscribes_that_is_going_to_finish search_matches fail = {str(e)}' + print(msg) + techSendMail(mail_sets, msg, title='every_1hour_start send_mail_for_user_subscribes_that_is_going_to_finish') + + if log: + techSendMail(mail_sets, str(msg), title='every_1hour_start get_competitors_prices') + + + + print(f'- processing time = {str(datetime.now() - log_begin_DT)} -') + + diff --git a/GeneralApp/views.py b/GeneralApp/views.py index fd96945..84900b6 100644 --- a/GeneralApp/views.py +++ b/GeneralApp/views.py @@ -22,65 +22,9 @@ def test_code(request): res = None - # import allauth - # from allauth.socialaccount.models import SocialApp - # apps = SocialApp.objects.all() - # apps.delete() + from SubscribesApp.reports import send_mail_for_user_subscribes_that_is_going_to_finish + send_mail_for_user_subscribes_that_is_going_to_finish() - # from PushMessages.views import send_push - # send_push(request.user, 'test_title', 'test_content') - - # from BaseModels.pay_systems.DVL_Group_kaz.api.funcs import create_order - # create_order() - - # from AuthApp.models import User - # from SubscribesApp.models import SubscribeForUser, Subscribe - # subscribes_null_price = Subscribe.objects.filter(price=0) - # if not subscribes_null_price: - # res = 'Subscribe not found' - # else: - # subscribe = subscribes_null_price[0] - # users_wo_subscribe = User.objects.filter(rel_userSubscribes_for_user=None) - # for user in users_wo_subscribe: - # u_sub = SubscribeForUser.objects.create( - # user=user, - # subscribe=subscribe, - # paid_period_from_DT=datetime.now(), - # paid_period_to_DT=datetime.now() + timedelta(hours=subscribe.period) - # ) - - # from RoutesApp.search_matches import search_matches - # search_matches() - - # try: - # # body = request.body - # # data = json.loads(body) - # # if 'head' not in data or 'body' not in data or 'id' not in data: - # # return JsonResponse(status=400, data={"message": "Invalid data format"}) - # # user_id = data['id'] - # user = request.user - # payload = {'head': '123', 'body': 'qwerty'} - # send_user_notification(user=user, payload=payload, ttl=1000) - # return JsonResponse(status=200, data={"message": "Web push successful"}) - # except TypeError: - # return JsonResponse(status=500, data={"message": "An error occurred"}) - - # routes = Route.objects.all() - # - # for route in routes: - # print(route.id) - # required_save = False - # if not route.from_city: - # route.from_city = get_city_by_type_transport_and_address_point(route.type_transport, route.from_address_point) - # required_save = True - # - # if not route.to_city: - # route.to_city = get_city_by_type_transport_and_address_point(route.type_transport, - # route.to_address_point) - # required_save = True - # - # if required_save: - # route.save() if res: if type(res) == str: return HttpResponse(res) diff --git a/RoutesApp/management/commands/every_1hour_start.py b/RoutesApp/management/commands/every_1hour_start.py deleted file mode 100644 index fc5bc8f..0000000 --- a/RoutesApp/management/commands/every_1hour_start.py +++ /dev/null @@ -1,36 +0,0 @@ -from django.core.management.base import BaseCommand -from datetime import datetime -from BaseModels.mailSender import techSendMail -from GeneralApp.funcs_options import get_options_by_opt_types, get_mail_send_options - -class Command(BaseCommand): - - def handle(self, *args, **options): - mail_sets = get_mail_send_options() - - log = '' - log_begin_DT = datetime.now() - msg = str(log_begin_DT) - print('-------------') - print(msg) - - try: - from ...search_matches import search_matches - msg = search_matches() - if msg: - print(msg) - except Exception as e: - msg = f'every_1hour_start search_matches fail = {str(e)}' - print(msg) - techSendMail(mail_sets, msg, title='every_1hour_start search_matches') - - - - if msg: - techSendMail(mail_sets, str(msg), title='every_1hour_start get_competitors_prices') - - - - print(f'- processing time = {str(datetime.now() - log_begin_DT)} -') - - diff --git a/SubscribesApp/funcs.py b/SubscribesApp/funcs.py index 216ee76..2f508fa 100644 --- a/SubscribesApp/funcs.py +++ b/SubscribesApp/funcs.py @@ -5,6 +5,17 @@ from datetime import datetime, timedelta import json +def get_user_subscribes_that_is_going_to_finish(): + user_subscribes = SubscribeForUser.objects.filter( + enable=True, + subscribe__price__gt=0, + paid_period_to_DT__lt=datetime.now() + timedelta(days=3), + paid_period_to_DT__gt=datetime.now() + ) + + return user_subscribes + + def extension_free_subscribes(): subscribe = get_null_price_subscribe() if not subscribe: diff --git a/SubscribesApp/reports.py b/SubscribesApp/reports.py new file mode 100644 index 0000000..885daa5 --- /dev/null +++ b/SubscribesApp/reports.py @@ -0,0 +1,49 @@ +from .models import * +from datetime import datetime, timedelta +from .funcs import * +from django.utils.translation import gettext as _ + +def send_mail_for_user_subscribes_that_is_going_to_finish(): + + log = '' + + user_subscribes = get_user_subscribes_that_is_going_to_finish() + + for user_subscribe in user_subscribes: + try: + + from GeneralApp.funcs_options import get_options_by_opt_types, get_mail_send_options + sets = get_options_by_opt_types(['domain', 'project_name'], only_vals=True) + + subject = _('tripwb.com - Уведомление о скором окончании срока подписки') + + Dict = { + 'logo': f'{sets["domain"]}/static/img/svg/LogoMobile.svg', + 'project_name': sets['project_name'], + 'message_title': subject, + 'user_subscribe': user_subscribe + } + + html = render_to_string('mail/m_user_subscribes_that_is_going_to_finish.html', Dict) + from BaseModels.mailSender import admin_send_mail_by_SMTPlib + mail_sets = get_mail_send_options() + to = [user_subscribe.user.email, 'web@syncsystems.net', 'sa@a3-global.com', 'sysadmin.hax@gmail.com'] + res = admin_send_mail_by_SMTPlib( + mail_sets, + subject=subject, + from_email=mail_sets['sender_email'], to=to, + html_content=html + ) + if res and type(res) == str: + print(res) + # log += f'\n{res}' + + + except Exception as e: + msg = (f'send_mail_for_user_subscribes_that_is_going_to_finish ' + f'for user {user_subscribe.user} ' + f'Exception = {str(e)}') + print(msg) + log += f'\n{msg}' + + return log \ No newline at end of file diff --git a/templates/mail/m_user_subscribes_that_is_going_to_finish.html b/templates/mail/m_user_subscribes_that_is_going_to_finish.html new file mode 100644 index 0000000..fe1a65b --- /dev/null +++ b/templates/mail/m_user_subscribes_that_is_going_to_finish.html @@ -0,0 +1,78 @@ +{% load i18n %} + +
+
+
+ {{ project_name }} +
+ +

+ {% translate 'Уважаемый' %}, {{ user_subscribe.user }}! +

+
+

{% translate 'Ваша подписка на сайте' %} TripWithBonus истекает {{ user_subscribe.paid_period_to_DT|date }}

+

{% translate 'Вы можете продлить подписку через свой личный кабинет. Пройдите по ссылке ниже для входа на сайт.' %}

+

Моя подписка на TripWithBonus

+

+ {% translate 'Спасибо за то, что вы с нами!' %}
+ {% translate 'С уважением,' %}
+ {% translate 'Команда Trip With Bonus.' %}
+

+
+ {% for button in message_buttons %} +
+ +
+ {{ button.caption }} +
+
+
+ {% endfor %} + + +{#
#} +{#
#} +{#
#} +{# Адрес:#} +{#
#} +{# График работы: пн-вс 12:00 - 24:00#} +{#
#} +{# Телефоны:#} +{#
#} +{#
#} +{##} +{#
#} +{# #} +{#
О ДОСТАВКЕ#} +{#
#} +{#
#} +{# #} +{#
О#} +{#
#} +{#
#} +{#
#} + +
+
\ No newline at end of file