48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
from background_task import background
|
|
from datetime import date, datetime, timedelta
|
|
import time
|
|
from polls.models import Notice, SMSNotifyRecord, VoiceNotifyRecord, MessageIndicator
|
|
from polls.utils import send_voice_notify, sent_sms_notify
|
|
|
|
|
|
@background(remove_existing_tasks=True)
|
|
def process_notify_task():
|
|
print('start task')
|
|
indicator = MessageIndicator.objects.first()
|
|
if not indicator.processing:
|
|
return
|
|
start = time.process_time()
|
|
notices = Notice.objects.filter(is_read=False)
|
|
for n in notices:
|
|
two_hours_later = n.added + timedelta(minutes=120)
|
|
four_hours_later = n.added + timedelta(minutes=240)
|
|
now = datetime.now()
|
|
profile = n.user.userprofile_set.first()
|
|
if not profile.message_status:
|
|
continue
|
|
phone = n.user.username
|
|
today = now.date()
|
|
hour = now.hour
|
|
|
|
if two_hours_later < now:
|
|
exists = SMSNotifyRecord.objects.filter(
|
|
phone=phone, added=today).exists()
|
|
if exists:
|
|
break
|
|
if hour < 8 or hour > 20:
|
|
break
|
|
print('============', "sms ", phone, n.type, n.id)
|
|
SMSNotifyRecord.objects.create(phone=phone, notice_id=n.id)
|
|
sent_sms_notify(phone, n.type)
|
|
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)
|
|
print('end task', time.process_time() - start)
|