Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

django - How to count new data before executing a function again

I have this function that do automatic execution of send_sms function when certain conditions are met. That it will execute if the count for specific levels are detected.

I'm using celery to automatically execute it when needed. But my problem now is like this.

if the responses.count() >= 50: then it will execute send_sms function. but when it reaches 51 after the first execution it is still executing and it must not. I want it to recount again another NEW 50 for another execution.

How can I do it? Thanks!

def auto_sms(request):
    responses = Rainfall.objects.filter(
        level='Torrential' or 'Intense',
        timestamp__gt=now() - timedelta(days=3),
    )
    if responses.count() >= 50:
        send_sms(request)

    return HttpResponse(200)
question from:https://stackoverflow.com/questions/65617284/how-to-count-new-data-before-executing-a-function-again

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, for that you could just simply use the modulo operator, like this:

def auto_sms(request):
    responses = Rainfall.objects.filter(
        level='Torrential' or 'Intense',
        timestamp__gt=now() - timedelta(days=3),
    )

    count = responses.count()
    if not (count % 50) and count > 0:
        send_sms(request)

    return HttpResponse(200)

The main changes were at if not (count % 50) and count > 0:, where there are 2 logical operations.

The first one not (count % 50) means that every number that is divisible by 50 will return True, which are 50, 100, 150, etc.

However there's a catch, 0 will also return True for the first operation and that's not something that you want. So that's the reason for the second operation count > 0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...