def business_schedule_get(): v = Validator(purge_unknown=True) schema = { 'service_id': { 'default': None, 'coerce': lambda x: int(x) if x else x, 'nullable': True }, 'performer_id': { 'default': None, 'coerce': lambda x: int(x) if x else x, 'nullable': True }, 'date': { 'default': None, 'coerce': lambda x: parse(x) if x else x, 'nullable': True } } req_data = v.validated(request.args.to_dict(), schema) if not req_data: abort(400, v.errors) business = db.session.query(Business).filter( Business.user_id == get_jwt_claims()['id']).first() perf_id = req_data['performer_id'] if perf_id and Performer.get(perf_id).business_id != business.id: abort(403) serv_id = req_data['service_id'] if serv_id and Service.get(serv_id).business_id != business.id: abort(403) return jsonify(get_schedule(business_id=business.id, kwargs=req_data)), 200
def calc_vacant_hours(perf_id, serv_id, coord, xdate): local_tz = timezone('Europe/Moscow') print(local_tz) performer = Performer.get(perf_id) if performer.non_working_days and \ (xdate.isoweekday() in performer.non_working_days): return [] duration = Service.get(serv_id).duration tasks = db.session.query(Appointment).filter( Appointment.performer_id == perf_id, cast(Appointment.date, Date) == xdate ) busy = [] for task in tasks: local_date = task.date.astimezone(local_tz) time_to_location = get_travel_time((task.coordx, task.coordy), coord) if coord else timedelta(minutes=0) time_tuple = timedelta( hours=local_date.hour, minutes=local_date.minute ) - time_to_location - duration, timedelta( hours=local_date.hour, minutes=local_date.minute ) + task.service.duration + time_to_location busy.append(time_tuple) working = [performer.get_working_hours()] busy.append(performer.get_lunch_hours()) return merge_timelines(working, busy)
def update_service(curr_id): v = Validator(purge_unknown=True) schema = { 'name': { 'type': 'string' }, 'price': { 'type': 'float' }, 'description': { 'type': 'string' }, 'duration': { 'coerce': lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute), }, 'performers': { 'type': 'list' } } req_data = v.validated(request.get_json(), schema) if not req_data: abort(400, v.errors) service = Service.get(curr_id) if not service: return jsonify( {"msg": "{0} doesn't exist".format(type(service).__name__)}), 400 if 'performers' in req_data.keys(): old_performers = set(map(lambda x: x.id, service.performers)) new_performers = set(req_data['performers']) to_remove = old_performers.difference(new_performers) to_add = new_performers.difference(old_performers) try: if len(to_remove): db.session.execute(performer_service.delete().where( and_(performer_service.c.service_id == curr_id, performer_service.c.performer_id.in_(to_remove)))) if len(to_add): db.session.execute(performer_service.insert(), [{ 'performer_id': x, 'service_id': curr_id } for x in to_add]) except: db.session.rollback() return jsonify({"msg": "Probably performers parameter is invalid"}), 400 return update( service, {key: req_data[key] for key in req_data if key != 'performers'})
def create_service(): v = Validator(purge_unknown=True) schema = { 'name': { 'type': 'string', 'required': True }, 'price': { 'coerce': float, 'required': True }, 'description': { 'type': 'string', 'required': True }, 'duration': { 'required': True, 'coerce': lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute) }, 'performers': { 'type': 'list' } } req_data = v.validated(request.get_json(), schema) if not req_data: abort(400, v.errors) service = db.session.query(Service).filter( Service.name == req_data['name']).first() if service: return jsonify({"msg": "Service already exists"}), 400 service = Service(business_id=Business.get(get_jwt_claims()['id']).id) msg = add(service, {key: req_data[key] for key in req_data if key != 'performers'}) try: if 'performers' in req_data.keys() and len(req_data['performers']) > 0: db.session.execute(performer_service.insert(), [{ 'performer_id': x, 'service_id': service.id } for x in req_data['performers']]) db.session.commit() except: db.session.rollback() return jsonify({"msg": "Probably performers parameter is invalid"}), 400 return msg
def create_appointment(): v = Validator(purge_unknown=True) schema = { 'service_id': { 'coerce': int, 'required': True }, 'performer_id': { 'coerce': int, 'required': True }, 'date': { 'coerce': lambda x: parse(x).isoformat(), 'required': True }, 'user_id': { 'coerce': lambda x: int(x) if x else None, 'default': None, 'nullable': True }, 'notes': { 'type': 'string' }, 'coordx': { 'type': 'float' }, 'coordy': { 'type': 'float' } } req_data = v.validated(request.get_json(), schema) if not req_data: abort(400, v.errors) user_id = get_jwt_claims()['id'] if get_jwt_claims()['role'] == 'business': business = db.session.query(Business).filter( Business.user_id == user_id).first() if business.id != Performer.get(req_data['performer_id']).business_id: abort(403) if business.id != Service.get(req_data['service_id']).business_id: abort(403) user_id = req_data['user_id'] appointment = Appointment(user_id=user_id, is_confirmed=False) return add(appointment, {key: req_data[key] for key in req_data if key != 'user_id'})
def update_appointment(curr_id): v = Validator(purge_unknown=True) schema = { 'service_id': { 'coerce': int, }, 'performer_id': { 'coerce': int, }, 'date': { 'coerce': lambda x: parse(x).isoformat(), }, 'notes': { 'type': 'string' }, 'coordx': { 'type': 'float' }, 'coordy': { 'type': 'float' } } req_data = v.validated(request.get_json(), schema) if not req_data: abort(400, v.errors) user_id = get_jwt_claims()['id'] appointment = Appointment.get(curr_id) if not appointment: abort(404) if get_jwt_claims()['role'] == 'user': if appointment.user.id != user_id: abort(403) else: business = appointment.service.business if business.id != db.session.query(Business).filter( Business.user_id == user_id).first().id: abort(403) if 'performer_id' in req_data.keys(): if business.id != Performer.get( req_data['performer_id']).business_id: abort(403) if 'service_id' in req_data.keys(): if business.id != Service.get(req_data['service_id']).business_id: abort(403) return update(appointment, req_data)
def delete_service(curr_id): return delete(Service.get(curr_id))
def get_service(curr_id): return get(Service.get(curr_id))