def view_signup_page_success(request, id_hash):
    if id_hash is None:
        raise Http404('Reservation not found.')
    
    if not request.user.is_authenticated():
        lu.update({ 'ERR_found' : True, 'ERR_not_authenticated' : True })
        return render_to_response('reservation_signup/signup_success.html', lu, context_instance=RequestContext(request))
    
    try:
        reservation = Reservation.objects.get(id_hash=id_hash, is_visible=True)
    except Reservation.DoesNotExist:
        raise Http404('Reservation not found.')
    
    
    lu = get_common_lookup(request)
    lu.update({'reservation' : reservation
            , 'selected_date' : reservation.start_datetime.date() })
    
    timeslot_checker = TimeSlotChecker(selected_date=reservation.start_datetime.date())
    
    is_last_minute_reservation = timeslot_checker.is_last_minute_reservation(reservation)
    #print 
    if is_last_minute_reservation:
        notify_staff_of_last_minute_reservation(reservation)
    
    lu.update({ 'calendar_events' : timeslot_checker.calendar_events\
                , 'is_last_minute_reservation' :  is_last_minute_reservation })
    
    return render_to_response('reservation_signup/signup_success.html', lu, context_instance=RequestContext(request))
def get_valid_end_times(request, selected_date, selected_time=None):
    """Given a start_datetime, return an ajax list of end times"""
    if selected_date is None:
        return get_json_str_as_http_response2(request, False, "The start date was not found")
            
    if selected_time is None:
        return get_json_str_as_http_response2(request, False, "The start time was not found")
        
    try:
        time_str = '%s %s' % (selected_date, selected_time)
        selected_datetime = datetime.strptime(time_str, '%Y-%m-%d %H-%M')
    except:
        return get_json_str_as_http_response2(request, False, "The start time was not valid")
        

    timeslot_checker = TimeSlotChecker(selected_date=selected_datetime.date())
    end_time_options = timeslot_checker.get_end_times_for_ajax_call(selected_datetime)
    if end_time_options is None:
        return get_json_str_as_http_response2(request, False, "Sorry!  No end times are available")
    
    options_html = render_to_string_remove_spaces('admin_signup/ajax_end_time_options.html'\
                                , { 'end_time_options' : end_time_options})
    options_html_json = simplejson.dumps(options_html)
    
    return get_json_str_as_http_response2(request, True, msg=''\
                                , json_str=',"options_html" : %s' % options_html_json)
def view_admin_signup_page_success(request, id_hash):
    if id_hash is None:
        raise Http404('Reservation not found.')

    lu = get_common_lookup(request)
    lu.update({ 'admin_signup' : True })

    if not request.user.is_authenticated():
        lu.update({ 'ERR_found' : True, 'ERR_not_authenticated' : True })
        return render_to_response('admin_signup/admin_signup_success.html', lu, context_instance=RequestContext(request))

    cal_user = lu.get('calendar_user', None)
    if cal_user is None or not cal_user.is_calendar_admin:
        lu.update({ 'ERR_found' : True, 'ERR_no_permission_to_reserve_as_admin' : True })
        return render_to_response('admin_signup/admin_signup_success.html', lu, context_instance=RequestContext(request))
    
    try:
        reservation = Reservation.objects.get(id_hash=id_hash, is_visible=True)
    except Reservation.DoesNotExist:
        raise Http404('Reservation not found.')
    
    lu.update({'reservation' : reservation
            , 'selected_date' : reservation.start_datetime.date() })
    
    timeslot_checker = TimeSlotChecker(selected_date=reservation.start_datetime.date())
    lu.update({ 'calendar_events' : timeslot_checker.calendar_events
    , 'is_last_minute_reservation' : timeslot_checker.is_last_minute_reservation(reservation) })
    
    return render_to_response('admin_signup/admin_signup_success.html', lu, context_instance=RequestContext(request))
def view_adjust_reservation_type(request, selected_date, new_reservation_time_set=False):
    if selected_date is None:
        raise Http404('selected_date is None.')

    lu = get_common_lookup(request)
    lu.update({'success_new_reservation_time_set' : new_reservation_time_set})

    try:
        selected_date = datetime.datetime.strptime(selected_date, '%Y-%m-%d').date()
        lu.update({'selected_date' : selected_date })
    except:
        raise Http404('selected_date date not found.')
    
    lu.update({ 'change_hours' : True })

    if not request.user.is_authenticated():
        lu.update({ 'ERR_found' : True, 'ERR_not_authenticated' : True })
        return render_to_response('admin_signup/change_time_page.html', lu, context_instance=RequestContext(request))

    cal_user = lu.get('calendar_user', None)
    if not cal_user.is_calendar_admin:
        lu.update({ 'ERR_found' : True, 'ERR_no_permission_to_change_time' : True })
        return render_to_response('admin_signup/change_time_page.html', lu, context_instance=RequestContext(request))


    timeslot_checker = TimeSlotChecker(selected_date=selected_date)
    lu.update(timeslot_checker.get_lookup_for_template())

    if request.method == 'POST': # If the form has been submitted...
        change_time_form = AvailableHoursForm(request.POST) # A form bound to the POST data
        if change_time_form.is_valid(): # All validation rules pass
            if change_time_form.make_new_reservation_type():
                success_url = reverse('view_adjust_reservation_type_success'\
                                , kwargs={ 'selected_date' : selected_date.strftime('%Y-%m-%d') })
                return HttpResponseRedirect(success_url) # Redirect after POST
            else:
                change_time_form.init(selected_date)
    else:
        change_time_form = AvailableHoursForm()
        change_time_form.init(selected_date)

    lu.update({ 'change_time_form' : change_time_form})
    

    return render_to_response('admin_signup/change_time_page.html', lu, context_instance=RequestContext(request))
    
    
    
    
    
    
    
    
    
    
    
def view_blackout_days_signup_page(request, selected_date):
    if selected_date is None:
        raise Http404('Signup date not found.')

    lu = get_common_lookup(request)
    lu.update({ 'admin_blackout_days' : True })
    
    try:
        selected_datetime = datetime.strptime(selected_date, '%Y-%m-%d')
    except:
        raise Http404('Signup date not found.')
        
    
    selected_date = selected_datetime.date()
    lu.update({ 'selected_date' : selected_date})

    cal_user = lu.get('calendar_user', None)
    if cal_user is None or not cal_user.is_calendar_admin:
        lu.update({ 'ERR_found' : True, 'ERR_no_permission_to_reserve_as_admin' : True })
        return render_to_response('admin_signup/blackout_days_signup_page.html', lu, context_instance=RequestContext(request))

    timeslot_checker = TimeSlotChecker(selected_date=selected_date)
    if timeslot_checker.err_found():
        lu.update({ 'ERR_found' : True })
        lu.update(timeslot_checker.get_lookup_for_template())
        return render_to_response('admin_signup/blackout_days_signup_page.html', lu, context_instance=RequestContext(request))
        
    lu.update(timeslot_checker.get_lookup_for_template())
    if request.method == 'POST': # If the form has been submitted...
        signup_form = AdminBlackoutDaysForm(request.POST) # A form bound to the POST data
        if signup_form.is_valid(): # All validation rules pass
            new_signup = signup_form.get_calendar_event()
            #print 'new_signup', new_signup
            success_url = reverse('view_blackout_days_signup_success'\
                            , kwargs={  'id_hash' : new_signup.id_hash }\
                            )
            return HttpResponseRedirect(success_url) # Redirect after POST
        else:
            signup_form.init(selected_date)
    else:
        signup_form = AdminBlackoutDaysForm()
        signup_form.init(selected_date)
            
    lu.update({ 'signup_form' : signup_form})
    
    return render_to_response('admin_signup/blackout_days_signup_page.html', lu, context_instance=RequestContext(request))
    
    

     
def view_admin_signup_page(request, selected_date):
    if selected_date is None:
        raise Http404('Signup date not found.')

    lu = get_common_lookup(request)
    lu.update({ 'admin_signup' : True })

    try:
        selected_datetime = datetime.strptime(selected_date, '%Y-%m-%d')
    except:
        raise Http404('Signup date not found.')

    selected_date = selected_datetime.date()
    lu.update({ 'selected_date' : selected_date})

    cal_user = lu.get('calendar_user', None)
    if cal_user is None or not cal_user.is_calendar_admin:
        lu.update({ 'ERR_found' : True, 'ERR_no_permission_to_reserve_as_admin' : True })
        return render_to_response('admin_signup/reservation_signup_page.html', lu, context_instance=RequestContext(request))
            

    if not request.user.is_authenticated():
        lu.update({ 'ERR_found' : True, 'ERR_not_authenticated' : True })
        return render_to_response('admin_signup/reservation_signup_page.html', lu, context_instance=RequestContext(request))

    timeslot_checker = TimeSlotChecker(selected_date=selected_date)
    if timeslot_checker.err_found():
        lu.update({ 'ERR_found' : True })
        lu.update(timeslot_checker.get_lookup_for_template())
        return render_to_response('admin_signup/reservation_signup_page.html', lu, context_instance=RequestContext(request))
        
    lu.update(timeslot_checker.get_lookup_for_template())
    if request.method == 'POST': # If the form has been submitted...
        signup_form = AdminSignupForm(request.POST) # A form bound to the POST data
        if signup_form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            new_signup = signup_form.get_reservation()
            
            success_url = reverse('view_admin_signup_page_success'\
                            , kwargs={  'id_hash' : new_signup.id_hash }\
                            )
            return HttpResponseRedirect(success_url) # Redirect after POST
        else:
            signup_form.init(timeslot_checker.get_timeslot_choices_for_form()\
                            , timeslot_checker.get_reservation_time_block()
                            , cal_user )
    else:
        signup_form = AdminSignupForm()
        signup_form.init(timeslot_checker.get_timeslot_choices_for_form()\
                        , timeslot_checker.get_reservation_time_block()
                        , cal_user)
            
    lu.update({ 'signup_form' : signup_form
                , 'poster_tube_types' : PosterTubeType.objects.filter(available=True)
                , 'show_open_slot_links' : True})
    
    return render_to_response('admin_signup/reservation_signup_page.html', lu, context_instance=RequestContext(request))
 def make_new_reservation_type(self):
     if not self.is_valid():
         return False
     
     selected_day = self.cleaned_data['selected_day']
 
     print 'get_earliest_time', self.get_earliest_time()
     print 'get_latest_time', self.get_latest_time()
     print 'SLOT1_TIMES', self.SLOT1_TIMES
     print self.cleaned_data
     
     new_rt = ReservationType(name='time for %s' % selected_day.strftime('%Y-%m-%d')\
                         , start_date=selected_day\
                         , end_date=selected_day\
                         , time_block=DEFAULT_TIME_BLOCK
                         , opening_time=self.get_earliest_time()\
                         , closing_time=self.get_latest_time()\
                         , is_active=True
                         )
     new_rt.save()
     
     for d in DayOfWeek.objects.all():
         new_rt.days_allowed.add(d)
     new_rt.save()
     new_rt.save()
     
     ## Turn off other Reservation Types for this day
     l = TimeSlotChecker.get_potential_reservation_types(selected_day)
     for rt in l:
         if not (rt.is_default or new_rt.id==rt.id):
             rt.is_active=False
             rt.save()
     
     return True