Reputation:
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()
Upvotes: 0
Views: 558
Reputation: 194
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.
Upvotes: 0