0.10.2 mail alert by new routes
This commit is contained in:
101
RoutesApp/search_matches.py
Normal file
101
RoutesApp/search_matches.py
Normal file
@@ -0,0 +1,101 @@
|
||||
from .models import *
|
||||
from datetime import datetime, timedelta
|
||||
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
|
||||
|
||||
def send_mail_found_matches_routes(route, 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',})
|
||||
|
||||
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})
|
||||
|
||||
html = render_to_string('mail/m_found_matched_routes.html', Dict)
|
||||
|
||||
|
||||
mail_sets = get_mail_send_options()
|
||||
to = [route.owner.email, 'web@syncsystems.net']
|
||||
subject = _('Мы нашли исполнителя по Вашему объявлению!')
|
||||
res = admin_send_mail_by_SMTPlib(
|
||||
mail_sets,
|
||||
subject=subject,
|
||||
from_email=mail_sets['sender_email'], to=to,
|
||||
html_content=html
|
||||
)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def search_matches(for_routes=None):
|
||||
|
||||
log = ''
|
||||
|
||||
try:
|
||||
if not for_routes:
|
||||
for_routes = Route.objects.filter(
|
||||
createDT__gte=datetime.now() - timedelta(hours=1),
|
||||
receive_msg_by_email=True
|
||||
)
|
||||
|
||||
check_fields = [
|
||||
'type_transport', 'departure_DT', 'arrival_DT', 'from_address_point', 'to_address_point',
|
||||
'from_place', 'to_place', 'cargo_type', 'weight'
|
||||
]
|
||||
|
||||
|
||||
for route in for_routes:
|
||||
kwargs = {}
|
||||
params = {}
|
||||
|
||||
for field_name in check_fields:
|
||||
field_val = getattr(route, field_name, None)
|
||||
if field_val:
|
||||
if type(field_val) == datetime:
|
||||
params.update({f"{field_name}": f'{field_val.strftime("%d.%m.%Y")} - {field_val.strftime("%d.%m.%Y")}'})
|
||||
kwargs.update({f"{field_name}__date": field_val.date()})
|
||||
elif field_name == 'weight':
|
||||
# print(field_name)
|
||||
params.update({f"{field_name}": field_val})
|
||||
if route.owner_type == 'mover':
|
||||
kwargs.update({f"{field_name}__lte": field_val})
|
||||
else:
|
||||
kwargs.update({f"{field_name}__gte": field_val})
|
||||
else:
|
||||
kwargs.update({field_name: field_val})
|
||||
params.update({field_name: field_val})
|
||||
|
||||
found_routes = Route.objects.exclude(
|
||||
id=route.id,
|
||||
).exclude(
|
||||
owner=route.owner
|
||||
).exclude(
|
||||
owner_type=route.owner_type
|
||||
).filter(
|
||||
**kwargs
|
||||
# ).count(
|
||||
)
|
||||
|
||||
if found_routes:
|
||||
msg = send_mail_found_matches_routes(route, params, found_routes[0].owner_type)
|
||||
if msg:
|
||||
log += msg
|
||||
|
||||
except Exception as e:
|
||||
msg = f'<br>\n! search_matches Error = {str(e)}'
|
||||
log += msg
|
||||
|
||||
mail_sets = get_mail_send_options()
|
||||
techSendMail(mail_sets, log, msg)
|
||||
|
||||
return log
|
||||
Reference in New Issue
Block a user