def get(self, vanity_url=None, book_date=None, book_hour=None, book_minutes=None): provider = db.get_provider_from_vanity_url(vanity_url) booking_datetime_string = "%s %s:%s" % (book_date, book_hour, book_minutes) booking_datetime_utc = to_utc( datetime.strptime(booking_datetime_string, '%Y-%m-%d %H:%M')) if not provider.is_available(booking_datetime_utc): logging.warn( "Trying to book a time not available in the schedule...") self.redirect("/" + vanity_url + "/book") else: booking_form = None user = self.get_current_user() if user: booking_form = AppointmentDetailsForLoggedInUser().get_form( provider=provider) else: # no user logged in, ask for email and stuff booking_form = AppointmentDetails().get_form(provider=provider) booking_form['booking_date'].data = book_date booking_form['booking_time'].data = book_hour + ":" + book_minutes self.render_template('provider/public/booking_details.html', provider=provider, booking_form=booking_form)
def test_to_utc(self): i18n.get_i18n().set_timezone('US/Eastern') format = '%Y-%m-%d %H:%M:%S' # Test datetime with timezone set base = datetime.datetime(2002, 10, 27, 6, 0, 0, tzinfo=pytz.UTC) localtime = i18n.to_utc(base) result = localtime.strftime(format) self.assertEqual(result, '2002-10-27 06:00:00') # Test naive datetime - no timezone set base = datetime.datetime(2002, 10, 27, 6, 0, 0) localtime = i18n.to_utc(base) result = localtime.strftime(format) self.assertEqual(result, '2002-10-27 11:00:00')
def string_to_time(string_time): if string_time: if isinstance( string_time, int ): parse_format = "%H" elif string_time.find(':') != -1: parse_format = "%H:%M" else: parse_format = "%H" else: return None return to_utc(datetime.strptime(str(string_time), parse_format))
def process_formdata(self, valuelist): if valuelist: date_str = u' '.join(valuelist) if not date_str: self.data = None raise ValidationError(self.gettext(u'Please input a date/time value')) parse_kwargs = self.parse_kwargs.copy() if 'default' not in parse_kwargs: try: parse_kwargs['default'] = self.default() except TypeError: parse_kwargs['default'] = self.default try: local_datetime = parser.parse(date_str, **parse_kwargs) self.data = to_utc(local_datetime) except ValueError: self.data = None raise ValidationError(self.gettext(u'Invalid date/time input'))
def process_formdata(self, valuelist): if valuelist: date_str = u' '.join(valuelist) if not date_str: self.data = None raise ValidationError( self.gettext(u'Please input a date/time value')) parse_kwargs = self.parse_kwargs.copy() if 'default' not in parse_kwargs: try: parse_kwargs['default'] = self.default() except TypeError: parse_kwargs['default'] = self.default try: local_datetime = parser.parse(date_str, **parse_kwargs) self.data = to_utc(local_datetime) except ValueError: self.data = None raise ValidationError(self.gettext(u'Invalid date/time input'))
def get(self, vanity_url=None, book_date=None, book_hour=None, book_minutes=None): provider = db.get_provider_from_vanity_url(vanity_url) booking_datetime_string = "%s %s:%s" % (book_date, book_hour, book_minutes) booking_datetime_utc = to_utc(datetime.strptime(booking_datetime_string, '%Y-%m-%d %H:%M')) if not provider.is_available(booking_datetime_utc): logging.warn("Trying to book a time not available in the schedule...") self.redirect("/" + vanity_url + "/book") else: booking_form = None user = self.get_current_user() if user: booking_form = AppointmentDetailsForLoggedInUser().get_form(provider=provider) else: # no user logged in, ask for email and stuff booking_form = AppointmentDetails().get_form(provider=provider) booking_form['booking_date'].data = book_date booking_form['booking_time'].data = book_hour + ":" + book_minutes self.render_template('provider/public/booking_details.html', provider=provider, booking_form=booking_form)
def string_to_datetime(string_date): return to_utc(datetime.strptime(string_date, "%Y-%m-%d"))
def post(self, vanity_url=None): ''' Booking process from public profile ''' appointment_details_form = None provider = db.get_provider_from_vanity_url(vanity_url) user = self.get_current_user() if user: appointment_details_form = AppointmentDetailsForLoggedInUser( ).get_form(self.request.POST, provider=provider) else: appointment_details_form = AppointmentDetails().get_form( self.request.POST, provider=provider) provider = db.get_provider_from_vanity_url(vanity_url) if appointment_details_form.validate(): # create the booking object booking = Booking() booking.provider = provider.key booking.booking_source = 'profile' booking_date = appointment_details_form['booking_date'].data booking_time = appointment_details_form['booking_time'].data booking.datetime = to_utc( datetime.strptime(booking_date + " " + booking_time, '%Y-%m-%d %H:%M')) booking.comments = appointment_details_form['comments'].data schedule = db.get_schedule_for_date_time(provider, booking_date, booking_time) booking.schedule = schedule.key if appointment_details_form.__contains__('service'): service_key_from_form = appointment_details_form[ 'service'].data if service_key_from_form: service_key = ndb.Key(urlsafe=service_key_from_form) provider_service = service_key.get() if provider_service: booking.service = service_key if user: # user is logged in, is this a patient? existing_patient = db.get_patient_from_user(user) if existing_patient: booking.patient = existing_patient.key else: self.link_user_to_new_patient(appointment_details_form, user, booking) # confirm the booking since it is a "known" user booking.confirmed = True booking.email_sent_to_patient = False booking.email_sent_to_provider = False # save booking booking.put() # already logged in so go directly to bookings list self.redirect('/patient/bookings/' + booking.patient.urlsafe()) # mail it to the patient mail.email_booking_to_patient(self, booking) # mail it to the provider mail.email_booking_to_provider(self, booking) else: # no user is logged in, check if the email address exists, this means they just didn't log in email = appointment_details_form['email'].data existing_user = db.get_user_from_email(email) if existing_user: existing_patient = db.get_patient_from_user(existing_user) if existing_patient: # email is in datastore, but not logged in # link booking to existing patient booking.patient = existing_patient.key booking.put() else: self.link_user_to_new_patient(appointment_details_form, user, booking) # confirm the booking since it is a "known" user booking.confirmed = True booking.email_sent_to_patient = False booking.email_sent_to_provider = False # save booking booking.put() # get the user to login key = booking.key.urlsafe() self.redirect('/login/booking/' + key) else: # no patient, no user. get them to fill a profile in the form booking.put() patient_form = RegistrationDetailsForNewPatient().get_form( ) patient_form['terms_agreement'].data = True patient_form['booking_date'].data = booking_date patient_form['booking_time'].data = booking_time patient_form['booking_key'].data = booking.key.urlsafe() patient_form['email'].data = email self.render_template( 'provider/public/booking_new_patient.html', provider=provider, patient_form=patient_form) logging.info('Created booking from public profile: %s' % booking) else: self.render_template('provider/public/booking_details.html', provider=provider, booking_form=appointment_details_form)
def post(self, vanity_url=None): ''' Booking process from public profile ''' appointment_details_form = None provider = db.get_provider_from_vanity_url(vanity_url) user = self.get_current_user() if user: appointment_details_form = AppointmentDetailsForLoggedInUser().get_form(self.request.POST, provider=provider) else: appointment_details_form = AppointmentDetails().get_form(self.request.POST, provider=provider) provider = db.get_provider_from_vanity_url(vanity_url) if appointment_details_form.validate(): # create the booking object booking = Booking() booking.provider = provider.key booking.booking_source = 'profile' booking_date = appointment_details_form['booking_date'].data booking_time = appointment_details_form['booking_time'].data booking.datetime = to_utc(datetime.strptime(booking_date + " " + booking_time, '%Y-%m-%d %H:%M')) booking.comments = appointment_details_form['comments'].data schedule = db.get_schedule_for_date_time(provider, booking_date, booking_time) booking.schedule = schedule.key if appointment_details_form.__contains__('service'): service_key_from_form = appointment_details_form['service'].data if service_key_from_form: service_key = ndb.Key(urlsafe=service_key_from_form) provider_service = service_key.get() if provider_service: booking.service = service_key if user: # user is logged in, is this a patient? existing_patient = db.get_patient_from_user(user) if existing_patient: booking.patient = existing_patient.key else: self.link_user_to_new_patient(appointment_details_form, user, booking) # confirm the booking since it is a "known" user booking.confirmed = True booking.email_sent_to_patient = False booking.email_sent_to_provider = False # save booking booking.put() # already logged in so go directly to bookings list self.redirect('/patient/bookings/' + booking.patient.urlsafe()) # mail it to the patient mail.email_booking_to_patient(self, booking) # mail it to the provider mail.email_booking_to_provider(self, booking) else: # no user is logged in, check if the email address exists, this means they just didn't log in email = appointment_details_form['email'].data existing_user = db.get_user_from_email(email) if existing_user: existing_patient = db.get_patient_from_user(existing_user) if existing_patient: # email is in datastore, but not logged in # link booking to existing patient booking.patient = existing_patient.key booking.put() else: self.link_user_to_new_patient(appointment_details_form, user, booking) # confirm the booking since it is a "known" user booking.confirmed = True booking.email_sent_to_patient = False booking.email_sent_to_provider = False # save booking booking.put() # get the user to login key = booking.key.urlsafe() self.redirect('/login/booking/' + key) else: # no patient, no user. get them to fill a profile in the form booking.put() patient_form = RegistrationDetailsForNewPatient().get_form() patient_form['terms_agreement'].data = True patient_form['booking_date'].data = booking_date patient_form['booking_time'].data = booking_time patient_form['booking_key'].data = booking.key.urlsafe() patient_form['email'].data = email self.render_template('provider/public/booking_new_patient.html', provider=provider, patient_form=patient_form) logging.info('Created booking from public profile: %s' % booking) else: self.render_template('provider/public/booking_details.html', provider=provider, booking_form=appointment_details_form)