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
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…