コード例 #1
0
ファイル: api.py プロジェクト: pumpkinsuu/api-service
def login():
    moodle = request.headers['moodle']
    wstoken = g.wstoken

    if 'username' not in request.json:
        raise ErrorAPI(400, 'missing "username"')
    if 'password' not in request.json:
        raise ErrorAPI(400, 'missing "password"')
    username = request.json['username']
    password = request.json['password']

    user = moodle_sv.user_info(
        moodle=moodle,
        wstoken=wstoken,
        username=username
    )
    if not user:
        raise ErrorAPI(404, 'userinfo not found')

    user['token'] = moodle_sv.login(
        moodle=moodle,
        username=username,
        password=password
    )
    return response(200, 'success', user)
コード例 #2
0
ファイル: face.py プロジェクト: pumpkinsuu/api-service
def update_user():
    moodle = request.headers['moodle']
    wstoken = g.wstoken
    key = g.key
    username = g.username

    front = request.form.get('front')
    if not front:
        raise ErrorAPI(400, 'missing front')
    left = request.form.get('left')
    if not left:
        raise ErrorAPI(400, 'missing left')
    right = request.form.get('right')
    if not right:
        raise ErrorAPI(400, 'missing right')
    replace = request.form.get('replace')
    if not replace:
        raise ErrorAPI(400, 'missing replace')

    if not moodle_sv.user_info(
            moodle=moodle, wstoken=wstoken, username=username):
        raise ErrorAPI(404, 'user not found')

    exist = face_sv.exist(key, username)
    val_t = time()

    user = {'id': username, 'front': front, 'left': left, 'right': right}
    if not exist:
        code = 201
        message = 'created'
        face = face_sv.create(key, user)
    else:
        code = 200
        message = 'success'
        face = face_sv.update(key, user)
    core_t = time()

    res = moodle_sv.create_image(moodle=moodle,
                                 wstoken=wstoken,
                                 username=user['id'],
                                 image_front=user['front'],
                                 image_left=user['left'],
                                 image_right=user['right'],
                                 replace=replace)
    if not res:
        code = 500
        message = 'failed to create image'
        face_sv.remove(key, username)

    t = {
        'face': face,
        'valid': val_t - g.start,
        'core': core_t - val_t,
        'moodle': time() - core_t,
        'total': time() - g.start
    }
    return response(code, message, t=t)
コード例 #3
0
ファイル: api.py プロジェクト: pumpkinsuu/api-service
def get_student(username):
    moodle = request.headers['moodle']
    wstoken = g.wstoken

    user = moodle_sv.user_info(
        moodle=moodle,
        wstoken=wstoken,
        username=username
    )
    if not user:
        raise ErrorAPI(404, 'userinfo not found')
    return response(200, 'success', user)
コード例 #4
0
ファイル: face.py プロジェクト: pumpkinsuu/api-service
def find():
    moodle = request.headers['moodle']
    wstoken = g.wstoken
    key = g.key

    if 'images' not in request.json:
        raise ErrorAPI(400, 'missing images')
    images = request.json['images']
    if not isinstance(images, list):
        raise ErrorAPI(400, 'images not list')
    val_t = time()

    data = face_sv.find(key, images)
    core_t = time()
    usernames = data['users']

    if not usernames:
        raise ErrorAPI(400, 'no user registered')

    users = []
    for username in usernames:
        if not username:
            users.append({'status': 404, 'message': 'not registered'})
            continue

        user = moodle_sv.user_info(moodle=moodle,
                                   wstoken=wstoken,
                                   username=username)
        user['status'] = 200
        user['message'] = 'registered'
        users.append(user)

    t = {
        'face': data,
        'valid': val_t - g.start,
        'core': core_t - val_t,
        'moodle': time() - core_t,
        'total': time() - g.start
    }
    return response(200, 'success', users, t=t)
コード例 #5
0
def check_request():
    g.start = time()

    if 'moodle' not in request.headers:
        raise ErrorAPI(400, 'no moodle provided')
    moodle = request.headers['moodle']
    # Check moodle
    data = key_db.get_data(moodle)
    if not data:
        raise ErrorAPI(400, 'invalid moodle')
    g.key = data['key']
    g.wstoken = data['wstoken']

    if '/api/login' == request.path:
        return

    if 'Authorization' not in request.headers:
        raise ErrorAPI(400, 'no token provided')
    token = request.headers['Authorization']
    # Check token
    res = token_info(moodle=moodle, token=token)
    g.username = res['username']
    g.userid = res['userid']

    user = user_info(moodle=moodle,
                     wstoken=g.wstoken,
                     username=res['username'])
    if not user:
        raise ErrorAPI(404, 'userinfo not found')
    # Check role
    if request.path in USER_ROUTES:
        return
    if user['isadmin']:
        return
    if user['roleid'] not in ROLE:
        raise ErrorAPI(401, 'no permission')