예제 #1
0
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