newmediamonitoring/polls/tasks.py

48 lines
1.8 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
2021-03-14 07:27:44 +00:00
from polls.models import Notice, SMSNotifyRecord, VoiceNotifyRecord, MessageIndicator
2020-11-28 10:23:56 +00:00
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')
2021-03-14 06:33:56 +00:00
indicator = MessageIndicator.objects.first()
if not indicator.processing:
return
2020-11-18 07:43:57 +00:00
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()
2021-03-14 06:33:56 +00:00
profile = n.user.userprofile_set.first()
if not profile.message_status:
continue
2020-11-28 10:23:56 +00:00
phone = n.user.username
today = now.date()
hour = now.hour
2021-03-14 06:33:56 +00:00
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(
2021-03-14 06:33:56 +00:00
phone=phone, added=today).exists()
2020-11-18 07:43:57 +00:00
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)