import re import pytz import datetime from django.contrib.sessions.models import Session from django.utils import timezone from appa.celery import app from appa.models import * from appa.call_api import api as call_api from appa.medicine_api import api as medicine_api @app.task(bind=True, acks_late=True) def send_call_request_task(self, ids=None): call_requests = CallRequest.objects.filter( id__in=ids, is_active=True, request_status=CallRequest.RequestStatus.NOT_SENT ) for call_request in call_requests: try: call_api.add_call_request(call_request) except Exception as e: print(e) @app.task(bind=True, acks_late=True) def send_daily_call_request_task(self): tomorrow = datetime.date.today() + datetime.timedelta(days=1) call_requests = CallRequest.objects.filter( date=tomorrow, status=CallRequest.Status.PENDING, is_active=True ) for call_request in call_requests: call_api.add_call_request(call_request) @app.task(bind=True, acks_late=True) def update_call_requests_task(self): call_lpu = {call_lpu.lpu_id: call_lpu for call_lpu in CallLPU.objects.all()} call_specialities = {medical_speciality.speciality_id: medical_speciality for medical_speciality in CallMedicalSpeciality.objects.all()} call_services = { r'A29\.004.+': 'На узи', r'A29\.011.+': 'На эндоскопию' } now = datetime.datetime.now().astimezone(pytz.timezone('Asia/Vladivostok')) bookings = medicine_api.get_bookings(now.date() + datetime.timedelta(days=1)) for booking in bookings: lpu = None if booking['lpu']['code'] in call_lpu: lpu = call_lpu[booking['lpu']['code']] if booking['user'] is None: continue service_name = None if booking['user']['doctor']['speciality'] in call_specialities: service_name = call_specialities[booking['user']['doctor']['speciality']].name for booking_service in booking['services']: service_code = booking_service['service']['code'] for regex, bot_talking in call_services.items(): if re.match(regex, service_code.strip()): service_name = bot_talking break if lpu is None or service_name is None: continue if not booking['patient']['phone_mobile']: continue CallRequest.objects.update_or_create( defaults=dict( date=datetime.datetime.strptime(booking['date_start'].split('T')[0], '%Y-%m-%d').date(), booking_date=booking['date_start'], patient_id=booking['patient']['id'], patient_first_name=booking['patient']['first_name'], patient_last_name=booking['patient']['last_name'], patient_middle_name=booking['patient']['middle_name'], patient_phone='7' + booking['patient']['phone_mobile'], doctor_name=booking['user']['display'], doctor_speciality=booking['user']['doctor']['speciality_display'], service_name=service_name, organization=lpu.name, address=lpu.address, ), booking_id=booking['id'], ) @app.task(bind=True, acks_late=True) def check_call_requests_task(self): for call_request in CallRequest.objects.filter( request_status=CallRequest.RequestStatus.SENT, date__in=[datetime.date.today(), datetime.date.today() + datetime.timedelta(days=1)] ): call_api.get_record(call_request) @app.task(bind=True, acks_late=True) def cleanup_task(self): tomorrow = timezone.now() + datetime.timedelta(days=1) RequestLog.objects.filter(created__lte=datetime.datetime.now() - datetime.timedelta(days=3)).delete() Session.objects.filter(expire_date__lte=tomorrow).delete()