def calendar(uid, date_str=None): resource = User.getByUrlsafeId(uid) tz = resource.getTimezoneObject() slots = resource.getAvailableSlots() limits = resource.getSlotLimits(slots, True) d = date.today() if date_str is None: if len(limits['dates']) > 0: d = limits['dates'][0] else: d = date_parser.parse(date_str).date() day_offset = d.weekday() d -= timedelta(days=day_offset) week_prev = d - timedelta(days=7) week_next = d + timedelta(days=7) date_str = d.strftime('%Y-%m-%d') date_prev = week_prev.strftime('%Y-%m-%d') date_next = week_next.strftime('%Y-%m-%d') limits['week_start'] = d week_dates = [ ] while d < week_next: if d in limits['dates']: week_dates.append(d) d += timedelta(days=1) limits['week_dates'] = week_dates return render_template('calendar.html', uid=uid, date_str=date_str, date_prev=date_prev, date_next=date_next, resource=resource, duration=resource.prefs.duration, tz=tz, slots=slots, limits=limits)
def booking(uid, date_str, time_str): resource = User.getByUrlsafeId(uid) tz = resource.getTimezoneObject() duration = resource.prefs.duration dt_str = '%s %s' % (date_str, time_str.replace('-', ':', 1)) dt_start = tz.localize(date_parser.parse(dt_str)) dt_end = dt_start + timedelta(minutes=duration) form = BookingForm(start_time=dt_start, end_time=dt_end, timezone=tz.zone) if request.method == 'POST': if form.validate_on_submit(): booking = Booking.createFromPost(resource, form.data) flash('Your booking succeeded.', 'info') return redirect(url_for('calendar', uid=uid, date_str=date_str)) else: flash_form_errors('Your booking failed.', form) return render_template('booking-new.html', uid=uid, date_str=date_str, time_str=time_str, form=form, resource=resource, dt_start=dt_start, tz=tz, duration=duration)