newmediamonitoring/polls/tasks.py

30 lines
1.0 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-17 00:45:44 +00:00
from polls.models import Notice, SMSNotifyRecord
from polls.utils import sent_sms_notify
@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:
two_hours_later = n.added + timedelta(minutes=2)
now = datetime.now()
if two_hours_later < now:
phone = n.user.username
today = now.date()
hour = now.hour
2020-11-18 07:43:57 +00:00
exists = SMSNotifyRecord.objects.filter(
phone=phone, added=today, notice_id=n.id).exists()
if exists:
return
if hour < 8 or hour > 20:
return
print('============', phone, n.type)
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-18 07:43:57 +00:00
print('end task', time.process_time() - start)