Sending django signals from django-admin command?

I have an unrealistic problem. In my django application, I use signals to send emails. All signals work, with the exception of one that runs from the django-admin command - django.core.management.base.NoArgsCommand (which runs through manage.py).

I checked my signal in different places, it works, except for this place.

Here is the code where I send the signal:

from django.core.management.base import NoArgsCommand

class Command(NoArgsCommand):
    help = "Send email advertisement expiration reminder to users"

    def handle_noargs(self, **options):
        from app.models import Advertisement, User
        from app.signals import ad_expires
        from datetime import datetime
        start=datetime(datetime.now().year, datetime.now().month, datetime.now().day+4,0,0)
        end=datetime(datetime.now().year,datetime.now().month,datetime.now().day+4,23,59)
        ads=Advertisement.objects.filter(visible_till__gte=start).filter(visible_till__lte=end)
        for ad in ads:
            ad_expires.send(self,ad=ad, user=ad.user)
        print "Expiration reminders sent to %s users" % len(ads)

Am I doing something wrong?

Also, is there an easier way to check the date for one day?

Thanks for any advice.

+3
source share
2 answers

Label:

start = datetime.now() + timedelta(days=4)
end = start + timedelta(days=1)
ads=Advertisement.objects.filter(visible_till__gte=start).filter(visible_till__lt=end)

Can you post your project structure here? Your code looks good to me.

+2
source

, , , django-admin. , print .

app/__init__.py. app, __init__.py , .

+1

Source: https://habr.com/ru/post/1796584/


All Articles