add message

This commit is contained in:
baoliang 2020-11-18 15:43:57 +08:00
parent 235bdc73aa
commit da549551ee
4 changed files with 18 additions and 9 deletions

View File

@ -1,4 +1,8 @@
from django.apps import AppConfig
from . import tasks
class PollsConfig(AppConfig):
name = 'polls'
def ready(self):
tasks.process_notify_task(repeat=5)

View File

@ -222,3 +222,5 @@ class SMSNotifyRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
phone = models.CharField('phone', max_length=32)
added = models.DateField(auto_now_add=True)
notice_id = models.CharField('notice_id', max_length=64)
updated = models.DateTimeField(auto_now=True)

View File

@ -1,12 +1,14 @@
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)
@ -15,10 +17,13 @@ def process_notify_task():
phone = n.user.username
today = now.date()
hour = now.hour
exists = SMSNotifyRecord.objects.filter(phone=phone, added=today).exists()
if exists: return
if hour < 8 or hour > 17: return
print('============',phone, n.type)
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)
SMSNotifyRecord.objects.create(phone=phone)
sent_sms_notify(phone, n.type)
print('end task', time.process_time() - start)

View File

@ -74,5 +74,3 @@ urlpatterns = [
path('app/download/', views.download, name='polls_app_download'),
path('app/has/update/', views.has_update, name='polls_app_has_update')
]
tasks.process_notify_task(repeat=5)