From da549551eece071580b44acaa621d7c603b70526 Mon Sep 17 00:00:00 2001 From: baoliang Date: Wed, 18 Nov 2020 15:43:57 +0800 Subject: [PATCH] add message --- polls/apps.py | 4 ++++ polls/models.py | 2 ++ polls/tasks.py | 17 +++++++++++------ polls/urls.py | 4 +--- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/polls/apps.py b/polls/apps.py index 4f5503f..02b680e 100644 --- a/polls/apps.py +++ b/polls/apps.py @@ -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) \ No newline at end of file diff --git a/polls/models.py b/polls/models.py index d3288a7..a309939 100644 --- a/polls/models.py +++ b/polls/models.py @@ -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) diff --git a/polls/tasks.py b/polls/tasks.py index 5275e0d..76b11cc 100644 --- a/polls/tasks.py +++ b/polls/tasks.py @@ -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) diff --git a/polls/urls.py b/polls/urls.py index 2f656b0..e8f524b 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -73,6 +73,4 @@ urlpatterns = [ path('app/intro.html', views.app_intro, name='polls_app_intro'), 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) \ No newline at end of file +] \ No newline at end of file