I have a form where the user chooses between two dynamic dates. The dates needs to be automatically updated every Tuesday and Saturday at 5pm.
e.g.
choice 1 = Wednesday March 30th, 2016 choice 2 = Sunday April 3rd, 2016
On Tuesday March 29th, 2016 at 5pm, choice 1 should update to Wednesday April 6th, 2016, and on Saturday 5pm, choice 2 should update to the next Sunday.
The code I wrote for it works in the shell with the help of python-dateutil, and theoretically should work on the site too, but every time 5pm comes around on Tuesday and Saturday, the choices don't update unless I manually 'Reload' the site from the web tab. Is there an easy way to make the forms update automatically?
forms.py:
from django import forms
from Food.models import Meal, MealItem
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.core import validators
from datetime import *
from dateutil.relativedelta import *
import calendar
now = datetime.now()
today = date.today()
hour_deadline = 17 # 5pm
current_sunday = today + relativedelta(weekday=SU)
current_wednesday = today + relativedelta(weekday=WE)
next_sunday = today + relativedelta(days=3, weekday=SU)
next_wednesday = today + relativedelta(days=3, weekday=WE)
if (now.weekday() == 5 and now.hour >= hour_deadline or now.weekday() == 6):
current_sunday = next_sunday
elif (now.weekday() == 1 and now.hour >= hour_deadline or now.weekday() == 2):
current_wednesday= next_wednesday
class OrderForm(forms.Form):
DELIVERYDAY = [((current_sunday.strftime("%B %d, %Y")), (current_sunday.strftime("%B %d, %Y"))),
(((current_wednesday.strftime("%B %d, %Y")), (current_wednesday.strftime("%B %d, %Y"))))]
delivery = forms.ChoiceField(label="Which day would you like your meals delivered?", choices=DELIVERYDAY, initial=current_sunday.strftime("%B %d, %Y"), widget=forms.RadioSelect)