Example #1
0
def mobile_code_get():
    try:
        req = ParamAuthMobileCodeSchema().load(request.args)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter,
                                     data=err.messages))

    try:
        code = login_usecase.send_login_code(req.mobile)
    except exception.LoginException as exc:
        return jsonify(ErrorResponse(exc.error, data=str(exc)))

    # White list for debug user.
    if req.mobile in WHITE_LIST:
        status = login_usecase.get_login_code_status(req.mobile)
        bearychat = "https://hook.bearychat.com/=bwD6u/incoming/38e28a304b97b7b67335c3be1cb2fe81"
        msg = "{} 码 {}\n1小时内发送次数 {}".format(req.mobile, code,
                                            status["counter"])
        requests.post(bearychat, json={"text": msg})

    current_app.logger.warning("mobile_code_get:{}:{}".format(
        req.mobile, code))

    return jsonify(
        OKResponse({
            "mobile": req.mobile,
            "code": None,
            "retry_time": None  # 暂时没有显示发送次数的需求
        }))
Example #2
0
def mobile_login():
    try:
        req = AuthMobileLoginRequestSchema().load(request.json)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter,
                                     data=err.messages))

    if not login_usecase.verify_login_code(req.mobile, req.password):
        return jsonify(ErrorResponse(error.InvalidLoginCode))

    m_user, m_user_info = login_usecase.signup_user(g.db, req.mobile,
                                                    MOBILE_CODE_CHINA,
                                                    req.child_info)
    if not m_user:
        return jsonify(ErrorResponse(error.LoginFailed))

    if m_user.status != UserStatus.Normal.value:
        return jsonify(ErrorResponse(error.UserIsBanned))

    token = create_token(request.headers["did"], m_user.id)

    current_app.logger.warning("mobile_login:{}:{}:{}".format(
        m_user.id, m_user.mobile, token))

    return jsonify(
        OKResponse({
            "user_id": m_user.id,
            "token": token,
            "relation": m_user_info.child_relation
        }))
Example #3
0
def guardian_post():
    try:
        req = CreateGuardianRequestDataSchema().load(request.json)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter,
                                     data=err.messages))

    try:
        result = guardian_usecase.create_guardian(g.db,
                                                  request.current_user_id,
                                                  req.id_card_no, req.realname,
                                                  req.mobile)
    except exception.UserException as exc:
        return jsonify(ErrorResponse(exc.error, data=str(exc)))

    return jsonify(OKResponse({"id": result.id}))
Example #4
0
def hot_carers():
    try:
        req = ParamRecommendCarerHotSchema().load(request.args)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages))

    if req.city_id:
        req.city_id = (req.city_id // 100) * 100  # 参考GB码 -> 市GB码

    if req.lat and req.lng and req.city_id:
        hot_carer_ids = recommend_usecase.retrieve_hot_carer_id_list(
            g.db, EntityFacade.redis_cli, req.city_id, req.lat, req.lng,
            req.offset, req.limit
        )
    else:
        # TODO: KOL
        CITY_ID_BEIJINT = 110100
        hot_carer_ids = recommend_usecase.retrieve_hot_carer_id_list(
            g.db, EntityFacade.redis_cli, CITY_ID_BEIJINT, req.lat, req.lng,
            req.offset, req.limit
        )

    current_app.logger.warning(hot_carer_ids)

    carers = []
    for id_ in hot_carer_ids:
        c = _build_recommend_carer_info(id_, req.city_id, req.lat, req.lng)
        if not c:
            current_app.logger.warning("invalid user id: %s", id_)
            continue
        carers.append(c)

    data = RecommendCarerResponseDataItemSchema(many=True).dump(carers)
    return jsonify(OKResponse(data))
Example #5
0
def info_me_post():
    try:
        req = UserInfoUpdateRequestSchema().load(request.json)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages))

    try:
        result = user_info_usecase.update_user_info(
            g.db, request.current_user_id,
            req.id_card_no, req.realname,
            req.mobile,
            req.child_relation,
            req.avatar
        )
        return jsonify(OKResponse(result))

    except exception.UserException as exc:
        return jsonify(ErrorResponse(exc.error, data=str(exc)))
Example #6
0
def handle_error(exc):
    code = 500
    if isinstance(exc, HTTPException):
        code = exc.code

    traceback.print_exc()

    data = dict(error=str(exc), code=code)
    return jsonify(ErrorResponse(error.UnknownError, data=data))
Example #7
0
def child_me_post():
    try:
        req = UserChildUpdateDataSchema().load(request.json)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter,
                                     data=err.messages))

    user_id = request.current_user_id

    try:
        result = child_usecase.create_or_update_child(g.db, user_id, req.id,
                                                      req.id_card_no,
                                                      req.realname,
                                                      req.nickname)
        return jsonify(OKResponse({"id": result}))

    except exception.UserException as exc:
        return jsonify(ErrorResponse(exc.error, data=str(exc)))
Example #8
0
def carer_info():
    try:
        req = ParamUserCarerInfoSchema().load(request.args)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter,
                                     data=err.messages))

    target_user_id = req.user_id

    user_info = user_info_usecase.retrieve_user_info(g.db, target_user_id)
    if not user_info:
        return jsonify(ErrorResponse(error.NonExistentUser))

    carer_info = carer_usecase.retrieve_care_info(g.db, target_user_id)
    if not carer_info:
        return jsonify(ErrorResponse(error.NonExistentCarer))

    # Nickname
    carer_info.nickname = user_info.nickname

    # Videos
    carer_info.videos = carer_usecase.videos_dict_to_list(carer_info.videos)

    # Distance
    distance = 0
    if req.lat and req.lng and carer_info.address.lat and carer_info.address.lng:
        distance = lbs.distance_m(
            (req.lat, req.lng),
            (carer_info.address.lat, carer_info.address.lng))
    carer_info['distance'] = distance_display(distance)

    # Degree
    carer_info.degree = DEGREE_INT2CN[carer_info.degree]

    # Like & Follow
    social_info = social_usecase.retrieve_user_social(g.db,
                                                      request.current_user_id,
                                                      target_user_id)
    carer_info.update(social_info)

    result = CarerInfoGetResponseDataSchema().dump(carer_info)

    resp = OKResponse(result)
    return jsonify(resp)
Example #9
0
def preset_user_info():
    try:
        req = UserInfoPresetRequestSchema().load(request.json)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages))

    try:
        result = user_info_usecase.store_preset_user_info(
            g.db, request.current_user_id,
            req.born,
            req.nickname,
            req.relation,
            req.birth_ts,
            req.gender
        )
        return jsonify(OKResponse(result))

    except exception.UserException as exc:
        return jsonify(ErrorResponse(exc.error, data=str(exc)))
Example #10
0
def child_me_delete():
    try:
        req = UserChildDeleteRequestSchema().load(request.json)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter,
                                     data=err.messages))

    user_id = request.current_user_id

    result = child_usecase.delete_child(g.db, user_id, int(req.id))
    if not result:
        return jsonify(
            ErrorResponse(error.InvalidParameter,
                          data={
                              "action": "delete_last_child",
                              "id": req.id
                          }))

    resp = OKResponse(result)
    return jsonify(resp)
Example #11
0
def following():
    try:
        req = ParamUserFollowingSchema().load(request.args)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter,
                                     data=err.messages))

    following_user_infos = social_usecase.retrieve_user_following(
        g.db, request.current_user_id, req.offset, req.limit)

    data = UserFollowingItemSchema(many=True).dump(following_user_infos)
    return jsonify(OKResponse(data))
Example #12
0
def carer_apply_post():
    try:
        req = CarerApplyRequestDataSchema().load(request.json)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter,
                                     data=err.messages))

    try:
        result = carer_usecase.submit_carer_application(
            g.db, request.current_user_id, req)

        # TODO: Review carer application
        # carer_usecase.approve_carer_application(
        #    g.db, request.current_user_id
        # )

    except exception.CarerApplicationException as exc:
        return jsonify(ErrorResponse(exc.error, data=str(exc)))

    result = dict(qr=CARER_APPLY_QR_CODE_IMAGE_URL, wx_id=CARER_APPLY_WX_ID)

    return jsonify(OKResponse(result))
Example #13
0
def upload_done():
    try:
        req = UserUploadDoneRequestSchema().load(request.json)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter,
                                     data=err.messages))

    if req['category'] == 'video':
        pass

    result = {}

    resp = OKResponse(result)
    return jsonify(resp)
Example #14
0
def carer_apply_get():
    application = carer_usecase.retrieve_carer_application(
        g.db, request.current_user_id)
    if not application:
        # 如果没有申请记录 返回实名认证结果
        identify_result = identity_usecase.is_user_identity_verified(
            g.db, request.current_user_id)
        return jsonify(
            ErrorResponse(error.NonExistentCarerApplication,
                          data=dict(identify_result=identify_result)))

    application.result = CARER_APPLY_REJECT_REASON.get(application.result, "")

    result = CarerApplicationDataSchema().dump(application)
    return jsonify(OKResponse(result))
Example #15
0
def identify_liveness_post():
    try:
        req = UserIdentifyLivenessRequestSchema().load(request.json)
    except marshmallow.ValidationError as err:
        return jsonify(ErrorResponse(error.InvalidParameter, data=err.messages))

    result = identity_usecase.identify_liveness(
        g.db, request.current_user_id,
        req.liveness_id, req.id_card_no, req.realname,
        req.idcard_image, req.live_image,
        req.info
    )

    resp = OKResponse({'result': result})
    return jsonify(resp)
Example #16
0
def info_me_get():
    user_info = user_info_usecase.retrieve_user_info(g.db, request.current_user_id)
    if not user_info:
        return jsonify(ErrorResponse(error.InvalidParameter))

    # Resolve `is_carer`
    carer_info = carer_usecase.retrieve_care_info(g.db, request.current_user_id)
    user_info.is_carer = carer_info is not None

    # Resolve `children_nicknames`
    children = child_usecase.retrieve_children_by_user_id(g.db, request.current_user_id)
    children_nicknames = [c.nickname for c in children]
    user_info.children_nicknames = children_nicknames

    # Resolve ui text
    if user_info.mobile:
        user_info.mobile = obscure_mobile_no(user_info.mobile)
    if user_info.id_card_no:
        user_info.id_card_no = obscure_id_card_no(user_info.id_card_no)
    if user_info.degree is not None:
        user_info.degree = DEGREE_INT2CN[user_info.degree]
    if user_info.child_relation is not None:
        user_info.child_relation = CHILD_RELATION_INT2CN[user_info.child_relation]
    if user_info.avatar_oss:
        user_info.avatar_url = qiniu.url_from_path(user_info.avatar_oss)
    if user_info.realname:
        user_info.realname = user_info.realname

    # Resolve `carer_apply_status`
    carer_application = carer_usecase.retrieve_carer_application(
        g.db, request.current_user_id
    )
    if not carer_application:
        user_info.carer_apply_status = CarerApplicationStatus.NotSubmitted.value
    else:
        user_info.carer_apply_status = carer_application.status

    data = UserInfoSchema().dump(user_info)
    resp = OKResponse(data)
    return jsonify(resp)
Example #17
0
 def wrapper(*args, **kwargs):
     yes = request.current_user_id
     if not yes:
         return jsonify(ErrorResponse(error.Unauthorized))
     return wrapped(*args, **kwargs)