简体   繁体   中英

How to send notification before 30 days from expire date in django?

How do i schedule notification for this program? i am using channels to create notification and use "crontab" for scheduling but it doesn't work.

def my_schedule_job():

vehicle_objs = Vehicle.objects.all()


for vehicle_obj in vehicle_objs:
    insurance_expiry = vehicle_obj.insurance_expiry
    insurance_expiry_date = insurance_expiry - timedelta(days=5)
    today = date.today()

    print('insurance_expiry_date',insurance_expiry_date)
    print('today',today)

    if insurance_expiry_date == today:
        notification_obj = Notification(user_id=vehicle_obj.user_id,notification="Your insurance for {} will expire on {}".format(vehicle_obj.vehicle,insurance_expiry) ,is_seen=False)
        notification_obj.save()
    elif insurance_expiry_date <= today:
        notification_obj = Notification(user_id=vehicle_obj.user_id,notification=vehicle_obj.vehicle + " insurance is going to expire on " + str(insurance_expiry),is_seen=False)
        notification_obj.save()

you'll want to create a custom manage.py command to solve your problem. To do so, you'll want to create an <appname>/management/commands directory structure and put your code in a file in that directory with the file name for the code bearing the name of the command you want to run. For example, emails.py .

After that, there are functions that Django expects to see present in your custom management command. You'll want to place your code into the respective functions.

Review this URL and let me know if you run into issues?

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM