def open_schedule_all_week(p_key): '''Add Schedule items from Monday to Friday 9-12 and 13-17 ''' for (day_key, day_name) in time.get_days_of_the_week(): am = Schedule(day=day_key, start_time=8, end_time=12, provider=p_key) am.put() pm = Schedule(day=day_key, start_time=13, end_time=17, provider=p_key) pm.put()
def get_sorted_schedule_timeslots(provider, request_timeslot): ''' Get a provider's timeslots ordered by proximity to the requested time This method is limited to the day of the request for now. Eventually expand to multi-day search ''' # returns day of week (0=monday, 1=tuesday, etc.) request_day = request_timeslot.start.weekday() # map the number to the actual day request_day_key = get_days_of_the_week()[request_day][0] request_date = request_timeslot.start.date() scheduleQuery = Schedule.query(Schedule.provider == provider.key, Schedule.day == request_day_key) logging.debug('schedules count for %s is %s' % (request_day_key, scheduleQuery.count())) timeslots = [] for s in scheduleQuery: ts = create_one_hour_timeslots_over_range(request_date, s.start_time, s.end_time) timeslots = timeslots + ts # sort sorted_timeslots = sorted( timeslots, key=lambda t: timeslot_distance(t, request_timeslot)) return sorted_timeslots
def create_schedule_dict(schedules): sm = ScheduleMap() for (key, label) in get_days_of_the_week(): sm[key] = dict() for s in schedules: sm[s.day][s.start_time] = s logging.debug('smm %s' % sm) return sm
def _set_fields(self, form): setattr(form, 'day', SelectField(_(u'Day'), choices=get_days_of_the_week())) setattr(form, 'start_time', SelectField(_(u'Start Time'), choices=get_time_list(), coerce=int)) setattr(form, 'end_time', SelectField(_(u'End Time'), choices=get_time_list(), coerce=int, validators=[custom_validators.StartTimeAfterEndTime('start_time', message=_('End time must be after start time'))]))
def render_schedule(self, provider, schedule_form=None, **kw): sq = provider.get_schedules() schedules = sq.fetch() days = time.get_days_of_the_week() times = time.get_time_list() schedule_mapmap = util.create_schedule_dict(schedules) if not schedule_form: schedule_form = ProviderScheduleForm().get_form() self.render_template('provider/schedule.html', provider=provider, schedules=schedule_mapmap, times=times, days=days, schedule_form=schedule_form, **kw)
def get_sorted_schedule_timeslots(provider, request_timeslot): ''' Get a provider's timeslots ordered by proximity to the requested time This method is limited to the day of the request for now. Eventually expand to multi-day search ''' # returns day of week (0=monday, 1=tuesday, etc.) request_day = request_timeslot.start.weekday() # map the number to the actual day request_day_key = get_days_of_the_week()[request_day][0] request_date = request_timeslot.start.date() scheduleQuery = Schedule.query(Schedule.provider==provider.key, Schedule.day==request_day_key) logging.debug('schedules count for %s is %s' % (request_day_key, scheduleQuery.count())) timeslots = [] for s in scheduleQuery: ts = create_one_hour_timeslots_over_range(request_date, s.start_time, s.end_time) timeslots = timeslots + ts # sort sorted_timeslots = sorted(timeslots, key=lambda t: timeslot_distance(t, request_timeslot)) return sorted_timeslots
def open_schedule_all_mornings(p_key): '''Add Schedule items from Sat and Sun 9-12 and 13-17 ''' for (day_key, day_name) in time.get_days_of_the_week(): Schedule(day=day_key, start_time=8, end_time=12, provider=p_key).put() # AM
def generate_complete_datetimes_dict(schedules, start_date, period, bookings): ''' Generate a dict of all dates in the period to list of hours available ''' dtm = dict() # create a dict [date_key][start_time] = schedule sm = create_schedule_dict(schedules) logging.info('BOOKINGS %s ' % bookings) bd = create_bookings_dict(bookings) # timezone adjustment tz = None if len(bookings) > 0: booking_datetime = to_local_timezone(bookings[0].datetime) tz = booking_datetime.tzinfo end_date = start_date + period d = start_date while d < end_date: logging.info('DATE %s' % d) dtm[d] = [] # day of week weekday_int = d.weekday() # map the number to the actual day day_key = get_days_of_the_week()[weekday_int][0] # check schedules for that day days_schedules = sm[day_key] # for each schedule on that day # create a list of tuples (start, end) schedule_period_tuples = [] for hour_key in sorted(days_schedules.keys()): s = days_schedules[hour_key] start_datetime = datetime.combine(d, time(hour=s.start_time)) end_datetime = datetime.combine(d, time(hour=s.end_time)) # adjust tz to match bookings if tz: start_datetime = start_datetime.replace(tzinfo=tz) end_datetime = end_datetime.replace(tzinfo=tz) start_end_tuple = (start_datetime, end_datetime) schedule_period_tuples.append(start_end_tuple) logging.info('SCHEDULE: %s ' % schedule_period_tuples) # remove bookings #for each booking that day, create tuple (start, end) day_bookings = bd.get(d, []) logging.info('DAY BOOKINGS: %s ' % day_bookings) booking_period_tuples = [] for b in day_bookings: booking_datetime = to_local_timezone(b.datetime) booking_date = booking_datetime.date() service = b.service.get() duration = service.duration booking_datetime_end = booking_datetime + timedelta(minutes=duration) b_start_end_tuple = (booking_datetime, booking_datetime_end) booking_period_tuples.append(b_start_end_tuple) logging.info('BOOKINGS TUPLES: %s ' % booking_period_tuples) available_period_tuples = subtract_booking_from_schedule(schedule_period_tuples, booking_period_tuples) logging.info('AVAILABLE: %s ' % available_period_tuples) datetimes_list = generate_datetimes_from_tuples_list(available_period_tuples) dtm[d].extend(datetimes_list) d = d + timedelta(days=1) return dtm