medicine-mts/appa/tasks.py

73 lines
2.5 KiB
Python

import datetime
import requests
from constance import config
from appa.celery import app
from appa.models import *
from appa.mts_api import add_call_request
@app.task(bind=True, acks_late=True)
def send_call_request_task(self, ids=None):
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
call_requests = CallRequest.objects.filter(
date=tomorrow,
status__in=[
CallRequest.Status.PENDING,
CallRequest.Status.WITHOUT_ANSWER
],
is_active=True
)
if ids and len(ids) > 0:
call_requests = call_requests.filter(id__in=ids)
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:
call_request = CallRequest.objects.get_or_create(
defaults={
'patient_name': booking['patient']['full_name'],
'patient_phone': '7' + booking['patient']['phone_mobile'],
},
date=booking_date,
patient_id=booking['patient']['id'],
)[0]
call_request.data.append({
'booking_id': booking['id'],
'time_start': booking['time_start'],
'time_end': booking['time_end'],
'speciality': booking['user']['doctor']['speciality_display'],
})
call_request.save(update_fields=['data'])
if response['next'] is None:
break