def appointments_check_args(args): def check_start_date(val): try: date = util.from_ISO_string( val) # .replace(hour=0, minute=0, second=0, microsecond=0) except: return defaults['start_date'] return date def check_end_date(val): try: end_date = util.from_ISO_string(val) except: end_date = defaults['end_date'] if end_date <= checked_arguments['start_date']: end_date = defaults['end_date'] return end_date arguments = { "start_date": check_start_date, "end_date": check_end_date, } checked_arguments = {} default_start_date = util.datetime_now_local().replace(hour=0, minute=0, second=0, microsecond=0) defaults = { "start_date": default_start_date, "end_date": checked_arguments.get('start_date', default_start_date) + datetime.timedelta(days=7), } for (item, check_func) in arguments.items(): val = args.get(item) if val is None: checked_arguments[item] = defaults[item] else: action = arguments.get(item) checked_arguments[item] = action(val) return checked_arguments
def delete(self, id): try: appointment = db.Appointment.objects(id=id).get() except: return {'error': 'Appointment does not exist.'}, 404 if not get_jwt_identity()['role'] == 'admin' and not get_jwt_identity( )['id'] == str(appointment['user']): return {'message': 'Forbidden'}, 403 if appointment.date <= util.to_ISO_string(util.datetime_now_local( )) and get_jwt_identity()['role'] != 'admin': return {'error': 'Appointment cannot be deleted.'}, 400 appointment.delete() return {'message': 'Appointment deleted successfully.'}
def create_available_hours_obj_base(start_date, end_date, start_hour, end_hour, interval, allow_past=False): available_hours_obj = {} now_date = util.datetime_now_local() current_date = start_date.replace(hour=start_hour, minute=0, second=0, microsecond=0) while current_date < end_date: # filter out slots before today if allow_past: available = True else: available = True if current_date > now_date else False current_slot = { current_date.strftime('%Y-%m-%d'): { current_date.strftime('%H'): { current_date.strftime('%M'): available } } } available_hours_obj = util.merge_dicts( [available_hours_obj, current_slot]) current_date = current_date + datetime.timedelta(minutes=interval) if int(current_date.strftime('%H')) >= end_hour: current_date = current_date.replace( hour=start_hour, minute=0, second=0, microsecond=0) + datetime.timedelta(days=1) return available_hours_obj
def put(self): if not get_jwt_identity()['role'] == 'admin': return {'message': 'Forbidden'}, 403 # get settings data settings = db.Settings.objects().first() # delete appointments db.Appointment.objects().delete() db.Service.objects().delete() # delete users other than User1 and User2 db.User.objects(username__nin=['User1', 'User2']).delete() # create new users users_no = 50 users = [] for i in range(users_no): new_user = db.User.generate_random_user() new_user.password = db.User.generate_hash(new_user.password) users.append(new_user) # add new users to database db.User.objects.insert(users) # create new services service_no = 10 services = [] for i in range(service_no): new_service = db.Service( name=f"Service {i}", duration=random.randint(1, 6) * settings.time_interval, price=random.randint(5, 30) * 10, ) services.append(new_service) # add new services to database db.Service.objects.insert(services) # create appointments for new users (4 weeks, starting from current, monday to sunday) now = util.datetime_now_local() start_date = (now - datetime.timedelta(days=now.weekday())) start_date = start_date.replace(hour=settings.start_hour, minute=0, second=0, microsecond=0) end_date = start_date + datetime.timedelta(days=28) query_params = { "date__gt": start_date.isoformat(), "date__lt": end_date.isoformat() } scheduled_appointments = db.Appointment.objects(**query_params).order_by('date') # generate appointments while current date is before specified end date appointments = [] current_date = start_date i = 0 while util.to_ISO_string(current_date) < util.to_ISO_string(end_date): service = random.choice(services) user = users[i] new_appointment = db.Appointment( user=str(user.id), service=str(service.id), service_name=str(service.name), date=util.to_ISO_string(current_date), duration=service.duration ) # validate if new appointments fits in to available slots result = validate_new_appointment(new_appointment, scheduled_appointments, settings, service, allow_past=True) if result: appointments.append(new_appointment) # calculate date for next appointment slot_count = math.ceil(int(new_appointment.duration) / int(settings.time_interval)) delay = random.choice([1, 2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) * 15 current_date = current_date + datetime.timedelta(minutes=slot_count * settings.time_interval + delay) if settings.start_hour > int(current_date.strftime('%H')) or int( current_date.strftime('%H')) >= settings.end_hour: current_date = (current_date + datetime.timedelta(days=1)).replace(hour=settings.start_hour, minute=0) # get next user number i = (i + 1) % users_no # add new appointments to database if len(appointments) > 0: db.Appointment.objects.insert(appointments) return {'message': f'{users_no} new users with {len(appointments)} new appointments created successfully.'}
def get(self): date_local = util.datetime_now_local() date_local_2 = util.datetime_new_local('2021-01-01', '10', '30') return {'local_date': date_local.strftime('%Y-%m-%d %H:%M'), 'local_date_@': date_local_2.strftime('%Y-%m-%d %H:%M')}, 200