From 17024d7350f211345ce52e3116134f8b99f3bd20 Mon Sep 17 00:00:00 2001 From: SDE Date: Wed, 29 May 2024 12:25:27 +0300 Subject: [PATCH] 1.1.1 autosubscribe to null price subscribe --- AuthApp/funcs.py | 7 +- AuthApp/views.py | 6 +- PushMessages/views.py | 4 + RoutesApp/js_views.py | 13 +- RoutesApp/search_matches.py | 41 +++-- SubscribesApp/funcs.py | 19 +++ SubscribesApp/models.py | 13 +- SubscribesApp/templatetags/__init__.py | 1 + .../templatetags/subscribes_tags_extra.py | 157 ++++++++++++++++++ .../profile/b_buttons_menu_profile.html | 16 +- templates/widgets/w_carrier_card.html | 68 ++++---- 11 files changed, 289 insertions(+), 56 deletions(-) create mode 100644 SubscribesApp/templatetags/__init__.py create mode 100644 SubscribesApp/templatetags/subscribes_tags_extra.py diff --git a/AuthApp/funcs.py b/AuthApp/funcs.py index 6389e84..4d0baa7 100644 --- a/AuthApp/funcs.py +++ b/AuthApp/funcs.py @@ -1,4 +1,5 @@ from django.template.loader import render_to_string +from SubscribesApp.funcs import check_option_in_cur_user_subscribe def get_user_timezone_Dict(user, request=None): @@ -30,10 +31,12 @@ def get_profile_page_content_html(request, page_name, data): if page_name == 'chat': from ChatServiceApp.funcs import get_chat_page_content_html return get_chat_page_content_html(request, data) - elif page_name == 'create_route_for_customer': + elif (page_name == 'create_route_for_customer' and + check_option_in_cur_user_subscribe(request.user, 'размещение заявок')): from RoutesApp.funcs import get_profile_new_route_page_html return get_profile_new_route_page_html(request, {'owner_type': 'customer'}) - elif page_name == 'create_route_for_mover': + elif (page_name == 'create_route_for_mover' and + check_option_in_cur_user_subscribe(request.user, 'размещение заявок')): from RoutesApp.funcs import get_profile_new_route_page_html return get_profile_new_route_page_html(request, {'owner_type': 'mover'}) elif page_name == 'my_routes': diff --git a/AuthApp/views.py b/AuthApp/views.py index 6eac317..6a499b8 100644 --- a/AuthApp/views.py +++ b/AuthApp/views.py @@ -47,8 +47,12 @@ def profile_page_View(request, page_name, id=None): lang = get_and_set_lang(request) + page_html = get_profile_page_content_html(request, page_name, id) + if not page_html: + raise Http404 + Dict = { - 'page_html': get_profile_page_content_html(request, page_name, id), + 'page_html': page_html, 'page_name': page_name, 'page_type': 'profile' } diff --git a/PushMessages/views.py b/PushMessages/views.py index 8f20032..36eec81 100644 --- a/PushMessages/views.py +++ b/PushMessages/views.py @@ -7,6 +7,7 @@ import json from django.shortcuts import render, get_object_or_404 from django.conf import settings from django.utils.translation import gettext as _ +from SubscribesApp.funcs import check_option_in_cur_user_subscribe def get_key_Dict(): @@ -18,6 +19,9 @@ def get_key_Dict(): return Dict def send_push(user, title, text, url=None, button_name=None, img=None): + if not check_option_in_cur_user_subscribe(user, 'push уведомления'): + return False + try: # body = request.body # data = json.loads(body) diff --git a/RoutesApp/js_views.py b/RoutesApp/js_views.py index 01530ae..a5b9a09 100644 --- a/RoutesApp/js_views.py +++ b/RoutesApp/js_views.py @@ -16,13 +16,16 @@ from django.urls import reverse from .forms import * from .funcs import * from GeneralApp.funcs import get_and_set_lang - +from SubscribesApp.funcs import check_option_in_cur_user_subscribe def del_route_ajax(request): if request.method != 'POST': raise Http404 + if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'): + return JsonResponse({'html': 'нет доступа'}, status=403) + lang = get_and_set_lang(request) try: @@ -58,6 +61,9 @@ def edit_route_ajax(request): if request.method != 'POST': raise Http404 + if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'): + return JsonResponse({'html': 'нет доступа'}, status=403) + lang = get_and_set_lang(request) data = json.loads(request.body) @@ -108,6 +114,8 @@ def edit_route_ajax(request): def new_route_view_ajax(request): if request.method != 'POST': raise Http404 + if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'): + return JsonResponse({'html': 'нет доступа'}, status=403) lang = get_and_set_lang(request) @@ -222,6 +230,9 @@ def create_or_change_route_ajax(request, route_id=None): if request.method != 'POST': raise Http404 + if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'): + return JsonResponse({'html': 'нет доступа'}, status=403) + lang = get_and_set_lang(request) Dict = {} diff --git a/RoutesApp/search_matches.py b/RoutesApp/search_matches.py index 3e9751f..4342804 100644 --- a/RoutesApp/search_matches.py +++ b/RoutesApp/search_matches.py @@ -4,7 +4,7 @@ from django.utils.translation import gettext as _ from django.template.loader import render_to_string from GeneralApp.funcs_options import get_options_by_opt_types, get_mail_send_options from BaseModels.mailSender import admin_send_mail_by_SMTPlib, techSendMail - +from SubscribesApp.funcs import check_option_in_cur_user_subscribe def get_Dict_for_send_msgs(kwargs, search_owner_type): @@ -36,6 +36,9 @@ def send_push_message_for_found_matches_routes(route, data_Dict): if not route.owner.is_authenticated: return None + if not check_option_in_cur_user_subscribe(route.owner, 'push уведомления'): + return False + from PushMessages.views import send_push title = 'Мы нашли исполнителя по Вашему объявлению!' text = 'Для просмотра результата нажмите на кнопку ниже' @@ -126,24 +129,38 @@ def search_matches(for_routes=None): if found_routes: msg = f'found routes for send messages = {found_routes.count()}' + data_Dict = None try: data_Dict = get_Dict_for_send_msgs(params, found_routes[0].owner_type) - msg = send_push_message_for_found_matches_routes(route, data_Dict) - if msg: - log += msg except Exception as e: - msg = f'
\n! search_matches Error send_push_message_for_found_matches_routes = {str(e)}' + msg = f'
\n! search_matches Error get_Dict_for_send_msgs = {str(e)}' print(msg) log += msg - try: - msg = send_mail_found_matches_routes(route, data_Dict) - if msg: + if data_Dict and check_option_in_cur_user_subscribe( + route.owner, 'push уведомления' + ): + try: + msg = send_push_message_for_found_matches_routes(route, data_Dict) + if msg: + log += msg + except Exception as e: + msg = f'
\n! search_matches Error send_push_message_for_found_matches_routes = {str(e)}' + print(msg) + log += msg + + if data_Dict and check_option_in_cur_user_subscribe( + route.owner, + 'уведомление на e-mail о появлении перевозчика по заданным критериям' + ): + try: + msg = send_mail_found_matches_routes(route, data_Dict) + if msg: + log += msg + except Exception as e: + msg = f'
\n! search_matches Error send_mail_found_matches_routes = {str(e)}' + print(msg) log += msg - except Exception as e: - msg = f'
\n! search_matches Error send_mail_found_matches_routes = {str(e)}' - print(msg) - log += msg except Exception as e: msg = f'
\n! search_matches Error = {str(e)}' diff --git a/SubscribesApp/funcs.py b/SubscribesApp/funcs.py index 57db3dd..4e4e37e 100644 --- a/SubscribesApp/funcs.py +++ b/SubscribesApp/funcs.py @@ -4,6 +4,22 @@ from django.utils.translation import get_language, activate from datetime import datetime, timedelta import json +def check_option_in_cur_user_subscribe(user, option_name): + if not user or not user.is_active or not user.is_authenticated: + return False + + user_subscribe = get_cur_user_subscribe(user) + try: + option = SubscribeOption.objects.get( + rel_subscribes_for_option=user_subscribe.subscribe, + name_ru__iexact=option_name + ) + return True + except SubscribeOption.DoesNotExist: + return False + + + def get_null_price_subscribe(): subscribes_null_price = Subscribe.objects.filter( enable=True, @@ -31,6 +47,9 @@ def subscribe_user_to_null_price_subscribe(user): def get_cur_user_subscribe(user): + if not user or not user.is_active or not user.is_authenticated: + return None + try: user_subscribe = SubscribeForUser.objects.get(enable=True, user=user) except SubscribeForUser.DoesNotExist: diff --git a/SubscribesApp/models.py b/SubscribesApp/models.py index 57572df..b687196 100644 --- a/SubscribesApp/models.py +++ b/SubscribesApp/models.py @@ -3,6 +3,16 @@ from BaseModels.base_models import BaseModel from django.utils.translation import gettext_lazy as _ from colorfield.fields import ColorField + +# options_list 29.05.2024 +# СМС уведомления +# push уведомления +# выделение заявки цветом (20 заявок) + 70 поднятий +# выделение заявок цветом (3 заявки) + 5 поднятий +# уведомление на e-mail о появлении перевозчика по заданным критериям +# размещение заявок +# просмотр контактов + class SubscribeOption(BaseModel): class Meta: @@ -16,7 +26,8 @@ class Subscribe(BaseModel): price = models.FloatField(verbose_name='Стоимость', default=0) options = models.ManyToManyField( - SubscribeOption, verbose_name=_('Подключенные опции'), blank=True, related_name='rel_subscribes_for_option' + SubscribeOption, verbose_name=_('Подключенные опции'), blank=True, + related_name='rel_subscribes_for_option' ) period_name = models.CharField(max_length=250, verbose_name=_('Название периода')) period = models.IntegerField(default=0, verbose_name=_('Длительность подписки в часах')) diff --git a/SubscribesApp/templatetags/__init__.py b/SubscribesApp/templatetags/__init__.py new file mode 100644 index 0000000..14c7ff2 --- /dev/null +++ b/SubscribesApp/templatetags/__init__.py @@ -0,0 +1 @@ +__author__ = 'SDE' diff --git a/SubscribesApp/templatetags/subscribes_tags_extra.py b/SubscribesApp/templatetags/subscribes_tags_extra.py new file mode 100644 index 0000000..a091c43 --- /dev/null +++ b/SubscribesApp/templatetags/subscribes_tags_extra.py @@ -0,0 +1,157 @@ +__author__ = 'SDE' + +from django import template +from django.template.defaultfilters import stringfilter + +register = template.Library() + +from django.core.serializers import serialize +from django.db.models.query import QuerySet +# import simplejson +from django.template import Library +from django.utils.html import mark_safe + +# @register.filter('get_value_from_dict') +# def get_value_from_dict(dict_data, key): +# """ +# usage example {{ your_dict|get_value_from_dict:your_key }} +# """ +# +# if key in dict_data: +# res = dict_data[key] +# return res +# +# return False +# +# +# +# @register.filter() +# def get_rows_count_by_cols_count(data, cols_count): +# rows_count = len(data) // cols_count +# if len(data) % cols_count: +# rows_count += 1 +# return rows_count +# +# @register.filter() +# def get_numbers_list(from_el, to_el): +# res = range(from_el, to_el+1) +# return res +# +# +# def val_type(value): +# res = type(value) +# return res.__name__ +# register.filter('val_type', val_type) +# +# @register.filter() +# def get_cols_table_data_for_row_when_cols3(value, row): +# el_count = 3 +# from_el = (row-1) * el_count +# to_el = row * el_count +# part = list(value)[from_el:to_el] +# return part +# # register.filter('val_type', val_type) +# +# +# @register.filter +# @stringfilter +# def correct_for_tables(value): +# if value in ['None', '0.0']: +# return '-' +# return value +# +# +# @register.filter +# @stringfilter +# def del_bad_symbols(value): +# from BaseModels.functions import del_bad_symbols +# return del_bad_symbols(value) +# +# +# @register.filter +# @stringfilter +# def del_amp_symbols(value): +# from BaseModels.functions import del_nbsp +# return del_nbsp(value) +# +# @register.filter +@register.simple_tag() +def check_subscribe_option(s_user, option_name): + from ..funcs import check_option_in_cur_user_subscribe + res = check_option_in_cur_user_subscribe(s_user, option_name) + return res +# +# @register.filter +# @stringfilter +# def get_color_by_number(value, arg=None): +# +# color = None +# try: +# val = float(value) +# +# if not color and arg == u'%': +# +# color = u'black' +# if val > 50: +# color = u'green' +# elif val <= 50 and val >= 25: +# color = u'#6c8107' +# elif val <= 25 and val >= 10: +# color = u'#a89803' +# elif val <= 10 and val >= 5: +# color = u'#e6a707' +# elif val <= 5 and val >= 0: +# color = u'#e67307' +# elif val <= 0: +# color = u'red' +# +# +# # val_range = val_max - val_min +# # # val_percent = (val_range * 100 / val) - 100 +# # offset = -(val_min + -(val)) +# # if val <0: +# # val = offset +# # if val > val_max: +# # val = val_max +# # elif val < 0: +# # val = 0 +# # +# # color_range = 16711680 - 1211136 +# # val_1unit = float(color_range) / float(val_range) +# # dec_color = 16711680 - int(val_1unit * val) +# +# if not color: +# color = u'black' +# if val > 1000: +# color = u'green' +# elif val <= 1000 and val >= 500: +# color = u'#6c8107' +# elif val <= 500 and val >= 250: +# color = u'#a89803' +# elif val <= 250 and val >= 125: +# color = u'#e6a707' +# elif val <= 125 and val >= 50: +# color = u'#e67307' +# elif val <= 50: +# color = u'red' +# +# # s = u'style="color: #{0}12;"'.format(str(hex(dec_color))[2:6]) +# s = u'style="color: {0};"'.format(color) +# return s +# except: +# return u'' +# +# +# # @register.filter +# # @stringfilter +# # def check_aprox_compare_strings(search_phrase, txt): +# # from ProductApp.search import get_highlight_string +# # +# # s = get_highlight_string(search_phrase, txt) +# # +# # return s + + + + + diff --git a/templates/blocks/profile/b_buttons_menu_profile.html b/templates/blocks/profile/b_buttons_menu_profile.html index 560c3af..cd2f46f 100644 --- a/templates/blocks/profile/b_buttons_menu_profile.html +++ b/templates/blocks/profile/b_buttons_menu_profile.html @@ -1,6 +1,7 @@ {% load static %} {% csrf_token %} {% load i18n %} +{% load subscribes_tags_extra %} {%trans "Профиль" as t_prof %} {%trans "Разместить объявление как отправитель" as t_customer %} @@ -11,6 +12,7 @@ {%trans "Моя подписка" as t_subscribe %} {%trans "Изменить профиль" as t_change_profile %} {%trans "Выход" as t_logout %} +{% check_subscribe_option user 'размещение заявок' as create_routes_allow %} + {% check_subscribe_option request.user 'просмотр контактов' as show_contacts_allow %}
- {% if user.is_authenticated %} -
- + {% if user.is_authenticated and show_contacts_allow %} +
+ - {% if user.is_authenticated %} - {# {{ route.owner.last_name }} {{ route.owner.first_name }}#} - {{ route.owner.last_name }} {{ route.owner.first_name }} + {% if user.is_authenticated %} + {# {{ route.owner.last_name }} {{ route.owner.first_name }}#} + {{ route.owner.last_name }} {{ route.owner.first_name }} - {% endif %} - {% if not user.is_authenticated %} - - {% endif %} + {% endif %} + {% if not user.is_authenticated %} + + {% endif %} - - - {{ route.phone }} - {# #} -
-
- -
+ + + {{ route.phone }} + {# #} +
+
+ +
{% endif %} - {% if not user.is_authenticated %} + {% if not user.is_authenticated or not show_contacts_allow %}