0.11.20 push w link

This commit is contained in:
SDE
2024-01-16 16:53:01 +03:00
parent 793d283d97
commit 87fbe7852c
9 changed files with 118 additions and 35 deletions

View File

@@ -415,7 +415,7 @@ def registration_ajax(request):
user = User.objects.create_user(username=form.data['email'], email=form.data['email'], password=form.data['password'])
# user = auth.authenticate(username=new_user_Dict['name'], password=new_user_Dict['pass'])
if user:
auth.login(request, user)
auth.login(request, user, backend='django.contrib.auth.backends.ModelBackend')
user.last_name = form.data['lastname']
user.first_name = form.data['firstname']

View File

@@ -266,19 +266,22 @@ def techSendMail(sets, html_content, title=None, add_emails=None):
# return msg
print('techSendMail')
project_name = ''
if 'project_name' in sets:
project_name = sets['project_name']
try:
# subject = u'truEnergy Data техническое оповещение'
from_email = 'support@truenergy.by'
from_email = sets['sender_email']
to = ['web@syncsystems.net']
if add_emails:
to.extend(add_emails)
text_content = 'Technical message from truEnergy.'
text_content = f'Technical message from {project_name}'
if title:
subject = title
else:
subject = u'truEnergy Data техническое оповещение'
subject = f'{project_name} - техническое оповещение'
res = admin_send_mail_by_SMTPlib(sets, subject, from_email, to, html_content)

View File

@@ -13,6 +13,16 @@ def get_options_by_opt_types(opt_types, only_vals=False):
res = {}
opts = opts.values('opt_type', 'value', 'prefix')
for item in opts:
if item['opt_type'] == 'domain':
try:
from django.contrib.sites.models import Site
current_site = Site.objects.get_current()
res.update({item['opt_type']: current_site.domain})
continue
except Exception as e:
print(str(e))
if item['prefix']:
res.update({item['opt_type']: f"{item['prefix']}{item['value']}"})
else:
@@ -29,6 +39,6 @@ def get_first_option_value_by_opt_type(opt_type):
return None
def get_mail_send_options():
opt_types = ['mail_server_url', 'mail_server_smtp_port', 'sender_mail_login', 'sender_mail_password', 'sender_email']
opt_types = ['mail_server_url', 'mail_server_smtp_port', 'sender_mail_login', 'sender_mail_password', 'sender_email', 'project_name']
return get_options_by_opt_types(opt_types, only_vals=True)

View File

@@ -19,13 +19,13 @@ def test_code(request):
from RoutesApp.models import Route
from ReferenceDataApp.models import Airport, City
import allauth
from allauth.socialaccount.models import SocialApp
apps = SocialApp.objects.all()
apps.delete()
# import allauth
# from allauth.socialaccount.models import SocialApp
# apps = SocialApp.objects.all()
# apps.delete()
# from RoutesApp.search_matches import search_matches
# search_matches()
from RoutesApp.search_matches import search_matches
search_matches()
# try:
# # body = request.body

View File

@@ -6,6 +6,7 @@ from webpush import send_user_notification
import json
from django.shortcuts import render, get_object_or_404
from django.conf import settings
from django.utils.translation import gettext as _
def get_key_Dict():
@@ -16,7 +17,7 @@ def get_key_Dict():
}
return Dict
def send_push(user, title, text, img=None):
def send_push(user, title, text, url=None, button_name=None, img=None):
try:
# body = request.body
# data = json.loads(body)
@@ -28,8 +29,17 @@ def send_push(user, title, text, img=None):
# user = get_object_or_404(User, pk=user_id)
Dict = {
'head': title,
'body': text
'body': text,
}
if url:
Dict['url'] = url
if button_name:
Dict['button_name'] = button_name
else:
Dict['button_name'] = _('Перейти'),
if img:
Dict['img'] = img
# payload = {'head': data['head'], 'body': data['body']}
send_user_notification(user=user, payload=Dict, ttl=1000)

View File

@@ -150,6 +150,16 @@ def get_routes_Dict(user=None, data=None):
res_Dict = {}
if data:
type_transport = None
if 'type_transport' in data and data['type_transport']:
items_list = data['type_transport'].split(',')
kwargs.update({f'type_transport__in': items_list})
if len(items_list) == 1:
type_transport = items_list[0]
for key, val in data.items():
if val:
if key == 'weight':
@@ -162,9 +172,9 @@ def get_routes_Dict(user=None, data=None):
else:
kwargs.update({f'{key}__lte': int(weight_list[0])})
if key == 'type_transport':
items_list = val.split(',')
kwargs.update({f'{key}__in': items_list})
# if key == 'type_transport':
# items_list = val.split(',')
# kwargs.update({f'{key}__in': items_list})
if key in (
@@ -184,19 +194,18 @@ def get_routes_Dict(user=None, data=None):
kwargs.update({key: val})
if key == 'from_address_point':
kwargs.update({f'from_city__id': val})
city = get_city_by_type_transport_and_address_point(type_transport, val)
kwargs.update({f'from_city': city})
res_Dict.update({
'from_address_point_txt': get_country_n_city_str_by_type_transport_and_address_point(
'road', val
)
'from_address_point_txt': city.get_country_n_city_str()
})
if key == 'to_address_point':
kwargs.update({f'to_city__id': val})
city = get_city_by_type_transport_and_address_point(type_transport, val)
kwargs.update({f'to_city': city})
res_Dict.update({
'to_address_point_txt': get_country_n_city_str_by_type_transport_and_address_point(
'road', val
)
'to_address_point_txt': city.get_country_n_city_str()
})
if key == 'from_el':

View File

@@ -5,22 +5,49 @@ 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
def send_mail_found_matches_routes(route, kwargs, search_owner_type):
def get_Dict_for_send_msgs(kwargs, search_owner_type):
Dict = {
'route': route,
'search_owner_type': search_owner_type
}
sets = get_options_by_opt_types(['domain', 'project_name'], only_vals=True)
Dict.update(sets)
Dict.update({'logo': f'{sets["domain"]}/static/img/svg/LogoMobile.svg',})
Dict.update({'logo': f'{sets["domain"]}/static/img/svg/LogoMobile.svg', })
find_routes_page_url = f'{sets["domain"]}/routes/route_search_results/?'
kwargs_list = [f'{key}={value}' for key, value in kwargs.items()]
kwargs_list.append(f'owner_type={search_owner_type}')
find_routes_page_url += f'{"&".join(kwargs_list)}'
Dict.update({'find_routes_page_url': find_routes_page_url})
return Dict
def send_push_message_for_found_matches_routes(route, data_Dict):
if not route.owner.is_authenticated:
return None
from PushMessages.views import send_push
title = 'Мы нашли исполнителя по Вашему объявлению!'
text = 'Для просмотра результата нажмите на кнопку ниже'
send_push(route.owner, title, text, url=data_Dict['find_routes_page_url'], button_name=_('Перейти к найденному'))
return None
def send_mail_found_matches_routes(route, data_Dict):
Dict = {
'route': route,
}
Dict.update(data_Dict)
html = render_to_string('mail/m_found_matched_routes.html', Dict)
@@ -87,7 +114,11 @@ def search_matches(for_routes=None):
)
if found_routes:
msg = send_mail_found_matches_routes(route, params, found_routes[0].owner_type)
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
msg = send_mail_found_matches_routes(route, data_Dict)
if msg:
log += msg
@@ -96,6 +127,6 @@ def search_matches(for_routes=None):
log += msg
mail_sets = get_mail_send_options()
techSendMail(mail_sets, log, msg)
techSendMail(mail_sets, log, title='search_matches fail')
return log

View File

@@ -7,12 +7,32 @@ self.addEventListener('push', function (event) {
const data = JSON.parse(eventInfo);
const head = data.head || 'New Notification 🕺🕺';
const body = data.body || 'This is default content. Your notification didn\'t have one 🙄🙄';
const icon = data.img || 'static/img/svg/Logo.svg';
let notificationOptions = {
body: body,
icon: icon,
};
if ('url' in data){
notificationOptions['data'] = { url: data.url };
notificationOptions['actions'] = [{action: "open_url", title: data.button_name}]
}
// Keep the service worker alive until the notification is created.
event.waitUntil(
self.registration.showNotification(head, {
body: body,
icon: 'static/img/svg/Logo.svg'
})
self.registration.showNotification(head, notificationOptions)
);
});
self.addEventListener('notificationclick', function(event) {
switch(event.action){
case 'open_url':
clients.openWindow(event.notification.data.url);
break;
case 'any_other_action':
clients.openWindow("https://www.example.com");
break;
}
}
, false);

View File

@@ -28,7 +28,7 @@
<script>
{#var user_id = {{ user.id }}#}
ws_url = `wss://{% get_ws_address %}/ws/socket-server/?user_id={{ user.id }}`;
ws_url = `ws://{% get_ws_address %}/ws/socket-server/?user_id={{ user.id }}`;
var chatSocket;
init_ws()
const beep = new Audio('/static/sounds/beep_2.mp3')