medicine-mts/appa/tasks.py

73 lines
2.6 KiB
Python

import datetime
import requests
from constance import config
from appa.celery import app
from appa.models import *
from appa.call_api import add_call_request
@app.task(bind=True, acks_late=True)
def send_call_request_task(self, ids=None):
call_requests = CallRequest.objects.filter(is_active=True)
if ids and len(ids) > 0:
call_requests = call_requests.filter(id__in=ids)
else:
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
call_requests = CallRequest.objects.filter(
date=tomorrow,
status__in=[
CallRequest.Status.PENDING,
CallRequest.Status.WITHOUT_ANSWER
]
)
for call_request in call_requests:
add_call_request(call_request)
@app.task(bind=True, acks_late=True)
def update_call_requests(self):
for add_days in [1, 2]:
booking_date = datetime.date.today() + datetime.timedelta(days=add_days)
CallRequest.objects.filter(date=booking_date).delete()
for page in range(1):
r = requests.get(
f'{config.MEDICINE_HOST}api/1/booking/',
params={
'date_start__date': booking_date.strftime('%d.%m.%Y'),
'patient__phone_mobile__isnull': False,
'patient__isnull': False,
'page': page,
'status': 1,
'limit': 50
},
headers={
'Authorization': f'Token {config.MEDICINE_TOKEN}'
}
)
if r.status_code == 200:
response = r.json()
results = response['results']
for booking in results:
CallRequest.objects.update_or_create(
defaults={
'date': booking['date'],
'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': '',
'doctor_speciality': '',
'service_name': '',
'address': ''
},
booking_id=booking['id'],
)
if response['next'] is None:
break