def context_for_create_or_check_booking(request): """ shows bookings available to be seen by logged-in user also provides a form to create a new booking appointment. return 0 if caller should redirect to another page without context """ ctx = create_standard_csrf_context(request) ctx['bookings_list'] = Booking.getAllObjectsDataNormalizedForUser(request.user) #bookings_list_for(request.user, orderedFields = True) now = datetime.datetime.now() DAYS_OF_STATS_TO_SHOW = 5 ctx['availableAppts'] = Booking.apptStats(DAYS_OF_STATS_TO_SHOW, showApptsAvailable = True) ctx['refreshTime'] = now.strftime("%b %d, %I:%M %p") if request.user.has_perm('exambookings.exam_center_view'): ctx['exam_center_view'] = True form = CreateBookingForm(initial={'testDate':datetime.datetime.now()}) if request.method == 'POST': form = CreateBookingForm(request.POST, request.FILES) if form.is_valid(): perId = form.cleaned_data['testBeginTime'] form.instance.courseTeacher = request.user form.instance.testBeginTime = Period.startTimeOfPeriodIdOnDay(perId, form.cleaned_data['testDate']) form.instance.testEndTime = milTimeAfterMinutes(form.instance.testBeginTime, form.cleaned_data['testDuration']) # double is_valid() call is required. testBeginTime model # field is hacked to support storing period IDs prior to # save() but should always be set to period start times when # save()ing if form.is_valid(): form.save() return 0 ctx['form'] = form ctx['form_fields_groups'] = form_fields_groups_for_view(request.user, form) return ctx
def dup_or_update_booking(request, pk, duplicate=False, saved=False): ctx = create_standard_csrf_context(request) ctx['bookings_list'] = Booking.getAllObjectsDataNormalizedForUser(request.user) #bookings_list_for(request.user, orderedFields = True) if request.user.has_perm('exambookings.exam_center_view'): exam_center_view = True ctx['exam_center_view'] = True else: exam_center_view = False appt = get_object_or_404(Booking, id__iexact=pk) if duplicate: appt.pk = None if request.method == 'POST': if not exam_center_view: post = request.POST.copy() post.setlist('courseTeacher', [request.user.pk]) form = UpdateBookingForm(post, instance=appt) else: form = UpdateBookingForm(request.POST, instance=appt) if form.is_valid(): perId = form.cleaned_data['testBeginTime'] formTestDate = form.cleaned_data['testDate'] periodStartTime = Period.startTimeOfPeriodIdOnDay(perId, formTestDate) form.instance.testBeginTime = periodStartTime form.instance.testEndTime = milTimeAfterMinutes(periodStartTime, form.cleaned_data['testDuration']) # double is_valid() call is required. testBeginTime model # field is hacked to support storing period IDs prior to # save() but should always be set to period start times when # save()ing if form.is_valid(): form.save() pk = form.instance.pk return HttpResponseRedirect(reverse('update_booking_success', kwargs={'pk':pk})) else: form = UpdateBookingForm(instance=appt, initial={'testBeginTime':Period.idOfPeriodStartTimeOnDay(appt.testBeginTime, appt.testDate)}) ctx['form'] = form ctx['form_fields_groups'] = form_fields_groups_for_view(request.user, form) ctx['saved'] = saved if duplicate: useTemplate = 'exambookings/duplicate_booking.html' else: useTemplate = 'exambookings/update_booking.html' return render_to_response(useTemplate, ctx)