Exemplo n.º 1
0
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
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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'})
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def delete_performer(curr_id):
    return delete(Performer.get(curr_id))
Exemplo n.º 6
0
def update_performer(curr_id):
    v = Validator(purge_unknown=True)
    schema = {
        'name': {
            'type': 'string'
        },
        'phone': {
            'type': 'string'
            # 'regex' : REGEX_PHONE
        },
        'description': {
            'type': 'string'
        },
        'newPhoto': {},
        'services': {
            'type': 'list'
        },
        'work_beg': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'work_end': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'lunch_beg': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'lunch_end': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'non_working_days': {
            'type': 'list',
            'default': []
        }
    }
    req_data = v.validated(request.get_json(), schema)
    if not req_data:
        abort(400, v.errors)
    performer = Performer.get(curr_id)
    if not performer:
        return jsonify(
            {"msg": "{0} doesn't exist".format(type(performer).__name__)}), 400
    if 'services' in req_data.keys():
        old_services = set(list(map(lambda x: x.id, performer.services)))
        new_services = set(req_data['services'])
        to_remove = old_services.difference(new_services)
        to_add = new_services.difference(old_services)
        try:
            if len(to_remove) > 0:
                db.session.execute(performer_service.delete().where(
                    and_(performer_service.c.performer_id == curr_id,
                         performer_service.c.service_id.in_(to_remove))))
            if len(to_add) > 0:
                db.session.execute(performer_service.insert(),
                                   [{
                                       'performer_id': curr_id,
                                       'service_id': x
                                   } for x in to_add])
        except:
            db.session.rollback()
            return jsonify({"msg":
                            "Probably performers parameter is invalid"}), 400

    if 'photo' in req_data.keys():
        if performer.photo_id:
            Image.query.filter(Image.id == performer.photo_id).delete()

        if req_data['photo']:
            photo = Image(data=req_data['photo'])
            db.session.add(photo)
            db.session.commit()
            performer.photo_id = photo.id

    return update(performer, {
        key: req_data[key]
        for key in req_data if key not in ['services', 'photo']
    })
Exemplo n.º 7
0
def get_performer(curr_id):
    return get(Performer.get(curr_id))
Exemplo n.º 8
0
def create_performer():
    v = Validator(purge_unknown=True)
    schema = {
        'name': {
            'type': 'string',
            'required': True
        },
        'phone': {
            'type': 'string',
            'required': True
            # 'regex' : REGEX_PHONE
        },
        'description': {
            'type': 'string',
            'required': True
        },
        'photo': {},
        'services': {
            'type': 'list'
        },
        'work_beg': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'work_end': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'lunch_beg': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'lunch_end': {
            'coerce':
            lambda x: timedelta(hours=parse(x).hour, minutes=parse(x).minute)
        },
        'non_working_days': {
            'type': 'list',
            'default': []
        }
    }
    req_data = v.validated(request.get_json(), schema)
    if not req_data:
        abort(400, v.errors)
    performer = db.session.query(Performer).filter(
        Performer.name == req_data['name']).first()
    if performer:
        return jsonify({"msg": "Performer already exists"}), 400
    performer = Performer(business_id=Business.get(get_jwt_claims()['id']).id)

    if ('photo' in req_data.keys()) and req_data['photo']:
        photo = Image(data=req_data['photo'])
        db.session.add(photo)
        db.session.commit()
        performer.photo_id = photo.id

    msg = add(performer, {
        key: req_data[key]
        for key in req_data if key not in ['services', 'photo']
    })
    try:
        if ('services' in req_data.keys()) and (len(req_data['services']) > 0):
            db.session.execute(performer_service.insert(),
                               [{
                                   'performer_id': performer.id,
                                   'service_id': x
                               } for x in req_data['services']])
            db.session.commit()
    except:
        return jsonify({"msg": "Probably services parameter is invalid"}), 400

    return msg