Skip to content

Instantly share code, notes, and snippets.

@ChristinaMeno
Created April 15, 2011 19:00
Show Gist options
  • Save ChristinaMeno/922260 to your computer and use it in GitHub Desktop.
Save ChristinaMeno/922260 to your computer and use it in GitHub Desktop.
I hate my self for writing this... Ideas on how to make it suck less?
@periodic_task(run_every=timedelta(hours=1), ignore_result=True )
def reset_cac_timeslots():
"""
Reset the available appointments in any CAC time slot that ended in the last hour.
Note that this code is not timezone aware and therefore may not behave
as expected if a time slot ends between 2 and 3 AM.
"""
now = datetime.now()
day_start = datetime.combine(now.date(), datetime.time(now).min)
# this is really lame and hints that the design of this model is a bit flawed
# Say you have appointments left for a particular day, say friday.
# If someone comes along on friday and makes a friday appt it will be next week.
# When this celery task comes along after that slot is ended it will reset
# the number of appts available which potentially results in there being
# max_requests + N appts: where N is the number of appts made for the next week on the day of.
next_week_today = day_start + timedelta(days=7)
slots = CACTimeSlot.objects.filter(
last_reset__lt=day_start,
end_time__lte=now.time(),
day_of_week=now.weekday(),
)
for s in slots:
fudge = CACAppointment.objects.filter(
time_slot=s,
requested_slot__gte=next_week_today,
).count()
slots.filter(pk=s.pk).update(appointments_remaining=max(F('max_requests') - fudge, 0), last_reset=now)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment