newmediamonitoring1/polls/tasks.py

41 lines
1.6 KiB
Python
Raw Normal View History

2020-09-20 11:14:54 +00:00
from background_task import background
2020-11-17 00:45:44 +00:00
from datetime import date, datetime, timedelta
2020-11-18 07:43:57 +00:00
import time
2020-11-28 10:23:56 +00:00
from polls.models import Notice, SMSNotifyRecord, VoiceNotifyRecord
from polls.utils import send_voice_notify, sent_sms_notify
2020-11-17 00:45:44 +00:00
@background(remove_existing_tasks=True)
def process_notify_task():
2020-11-18 07:43:57 +00:00
print('start task')
start = time.process_time()
2020-11-17 00:45:44 +00:00
notices = Notice.objects.filter(is_read=False)
for n in notices:
2021-01-10 07:21:51 +00:00
two_hours_later = n.added + timedelta(minutes=120)
four_hours_later = n.added + timedelta(minutes=240)
2020-11-17 00:45:44 +00:00
now = datetime.now()
2020-11-28 10:23:56 +00:00
phone = n.user.username
today = now.date()
hour = now.hour
2020-11-17 00:45:44 +00:00
if two_hours_later < now:
2020-11-18 07:43:57 +00:00
exists = SMSNotifyRecord.objects.filter(
phone=phone, added=today, notice_id=n.id).exists()
if exists:
2020-11-18 15:37:22 +00:00
break
2020-11-18 16:34:52 +00:00
if hour < 8 or hour > 20:
2020-11-18 15:37:22 +00:00
break
2020-11-28 10:23:56 +00:00
print('============', "sms ", phone, n.type, n.id)
2020-11-18 08:32:23 +00:00
SMSNotifyRecord.objects.create(phone=phone, notice_id=n.id)
2020-11-17 00:45:44 +00:00
sent_sms_notify(phone, n.type)
2020-11-28 10:23:56 +00:00
if four_hours_later < now:
exists = VoiceNotifyRecord.objects.filter(
phone=phone, added=today, notice_id=n.id).exists()
if exists:
break
if hour < 8 or hour > 20:
break
print('============', "voice ", phone, n.type, n.id)
VoiceNotifyRecord.objects.create(phone=phone, notice_id=n.id)
send_voice_notify(phone, n.type)
2020-11-18 07:43:57 +00:00
print('end task', time.process_time() - start)