30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
from background_task import background
|
|
from datetime import date, datetime, timedelta
|
|
import time
|
|
from polls.models import Notice, SMSNotifyRecord
|
|
from polls.utils import sent_sms_notify
|
|
|
|
|
|
@background(remove_existing_tasks=True)
|
|
def process_notify_task():
|
|
print('start task')
|
|
start = time.process_time()
|
|
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
|
|
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, n.id)
|
|
SMSNotifyRecord.objects.create(phone=phone, notice_id=n.id)
|
|
sent_sms_notify(phone, n.type)
|
|
print('end task', time.process_time() - start)
|