def make_reservations(year=2012, month=2, cnt=10):
    global random_words
    iso_day_of_week, num_days_in_month = calendar.monthrange(year, month)
    conflict_checker = ConflictChecker()

    for x in range(1, cnt+1):

        desc_idx_start = random.randint(0, len(random_words)-5)
        rand_desc = ' '.join(random_words[desc_idx_start:desc_idx_start+5])
        
        start_datetime = None
        end_datetime = None
        time_conflicts = True
        while time_conflicts: 
            day_of_week = random.randint(1, num_days_in_month)    
            start_hr = random.randint(0, 23)
            start_min = [0, 20, 40][random.randint(0, 2)]
            start_datetime = datetime(year, month, day_of_week, start_hr, start_min)
            end_datetime = start_datetime + timedelta(minutes=20)
            if not conflict_checker.does_timeslot_conflict(TimeSlot(start_datetime, end_datetime)):
                time_conflicts = False
                
        evt = CalendarEvent(start_datetime=start_datetime\
                    , display_name=get_random_initials()
                    , end_datetime=end_datetime\
                    )
        #            , display_name=rand_desc )
        evt.save()
        print '(%s) evt saved: %s' % (x, evt)
    def calculate_time_slots(self):
        
        if self.selected_date < self.current_date:        
             self.ERR_signup_day_has_passed = True
             return
        
        self.reservation_type = TimeSlotChecker.choose_reservation_type_by_date(self.selected_date, self.current_date)
        if self.reservation_type is None:
            self.ERR_reservation_type_not_found = True
            return
            
        if self.current_datetime >= datetime.combine(self.selected_date, self.reservation_type.closing_time):
            self.ERR_facility_closed = True
            return
                
        # set initial time slot
        start_datetime = datetime.combine(self.selected_date, self.reservation_type.opening_time)
        end_datetime = start_datetime + timedelta(minutes=self.reservation_type.time_block)
        
        conflict_checker = ConflictChecker()    # helps check against all existing CalendarEvents
        while end_datetime.time() <= self.reservation_type.closing_time:
            timeslot = TimeSlot(start_datetime, end_datetime)
            
            if not conflict_checker.does_timeslot_conflict(timeslot):
                # make sure the time slot hasn't passed--if it's the current day
                if start_datetime >= self.current_datetime:
                    self.open_timeslots.append(timeslot)
            # set next timeslot
            start_datetime = end_datetime
            end_datetime = start_datetime + timedelta(minutes=self.reservation_type.time_block)
        
        self.num_timeslots = len(self.open_timeslots)

        if self.num_timeslots == 0:
            self.ERR_no_reservation_times_left_today = True
    def clean(self):
        """Make sure the slot is still available"""
        time_slot = self.cleaned_data.get('time_slot', None)
        session_length = self.cleaned_data.get('session_length', None)

        if time_slot is None:
            self._errors['time_slot'] = self.error_class(['This field is required.'])
            raise forms.ValidationError('Please choose an available time')

        if session_length is None:
            self._errors['session_length'] = self.error_class(['This field is required.'])
            raise forms.ValidationError('Please enter a start time')

        ts_obj = self.get_time_slot_object()
        if ts_obj is None:
            raise forms.ValidationError('The time slot is invalid.  Please try again.')
        
        conflict_checker = ConflictChecker()
        if conflict_checker.does_timeslot_conflict(ts_obj):
             self._errors['time_slot'] = self.error_class(['Please choose a different time.'])
             raise forms.ValidationError('Sorry!  Someone reserved that time slot!  Please choose another one.')
        
        """If poster tube chosen, make sure color is chosen"""
        poster_tube_type = self.cleaned_data.get('poster_tube_type')
        poster_tube_color = self.cleaned_data.get('poster_tube_color')
        if poster_tube_type:
            if not poster_tube_color:
                self._errors['poster_tube_color'] = self.error_class(['Please choose a color.'])
                raise forms.ValidationError('If you choose a poster tube, please choose a color.')

        return self.cleaned_data
    def clean(self):
        """Make sure the slot is still available"""
        start_time = self.cleaned_data.get('start_time', None)
        end_time = self.cleaned_data.get('end_time', None)

        if start_time is None:
            self._errors['start_time'] = self.error_class(['This field is required.'])
            raise forms.ValidationError('Please choose a start time')

        if end_time is None:
            self._errors['end_time'] = self.error_class(['This field is required.'])
            raise forms.ValidationError('Please choose an end time')

        if end_time <= start_time:
            self._errors['end_time'] = self.error_class(['This field is required.'])
            raise forms.ValidationError('The end time must be AFTER the start time')
            
        conflict_checker = ConflictChecker()
        if conflict_checker.does_timeslot_conflict(TimeSlot(start_time, end_time)):
             self._errors['end_time'] = self.error_class(['Please choose a different time.'])
             raise forms.ValidationError('Sorry!  That time conflicts with another event reservation or message!  Please choose another one.')
            
        return self.cleaned_data
    def clean(self):
        """Make sure the slot is still available"""
        start_date = self.cleaned_data.get('start_date', None)
        end_date = self.cleaned_data.get('end_date', None)

        if start_date is None:
            self._errors['start_date'] = self.error_class(['This field is required.'])
            raise forms.ValidationError('Please choose a start date')

        if end_date is None:
            self._errors['end_date'] = self.error_class(['This field is required.'])
            raise forms.ValidationError('Please choose an end date')

        if start_date > end_date:
            self._errors['end_date'] = self.error_class(['This field is required.'])
            raise forms.ValidationError('The end date must be AFTER the start date')

        # Limit to 10 days
        days_limit = 7
        time_diff = end_date - start_date
        
        if time_diff.days > days_limit:
            self._errors['end_date'] = self.error_class(['This field is required.'])
            raise forms.ValidationError('You may only block off %s days at a time' % days_limit)
            
        
        
        start_time = datetime.combine(start_date, time.min)
        end_time = datetime.combine(end_date, time.max)

        conflict_checker = ConflictChecker()
        if conflict_checker.does_timeslot_conflict(TimeSlot(start_time, end_time)):
             self._errors['end_date'] = self.error_class(['Please choose a different date.'])
             raise forms.ValidationError('Sorry!  The start/end dates conflict with another calendar event!  Please choose other dates.')

        return self.cleaned_data