Exemple #1
0
def timeline():
    login_user_id = get_login_user_id(request)
    args = request.args
    start = http_util.get_param_int(args, 'start', BaseConfig.MAX_START)
    per_page = http_util.get_param_int(args, 'per_page',
                                       BaseConfig.DEFAULT_PER_PAGE)
    user_id = args.get('user_id', None)
    detail_id = None
    if user_id:
        detail_id = user_id
    else:
        detail_id = login_user_id

    if not detail_id:
        return http_util.return_404('not found user')

    res = Action.query_actions(
        user_id=detail_id,
        type=BaseConfig.TYPE_ACTION_UPLOAD,
    )

    items = []
    for item in res:
        res_type = item.res_type
        res_id = item.res_id

        item = Resource.get_resource_detail(
            res_id=res_id,
            res_type=res_type,
            login_user_id=login_user_id
        )
        if item:
            items.append(item)

    return http_util.return_model(items)
Exemple #2
0
def resource_delete():
    """资源详情"""
    args = request.json
    key = http_util.check_params(args, 'res_id', 'res_type')
    if key:
        return http_util.return_param_not_found(key)

    login_user_id = http_util.get_login_user_id(request)

    if not login_user_id:
        return http_util.return_no_authorization()

    res_id = http_util.get_param(args, 'res_id')
    res_type = http_util.get_param_int(args, 'res_type')

    try:
        is_del = Resource.delete_resource(res_id, res_type)

        if not is_del:
            return http_util.return_internal_server_error("删除失败")

        # 记录用户行为
        Action.create_action(
            user_id=login_user_id,
            type=BaseConfig.TYPE_ACTION_DELETE,
            res_id=res_id,
            res_type=res_type
        )

        return http_util.return_model()
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()
Exemple #3
0
def user_attentions():
    login_user_id = get_login_user_id(request)
    args = request.args
    key = http_util.check_params(args, 'attention_status')
    if key:
        return http_util.return_param_not_found(key)

    user_id = http_util.get_param(args, 'user_id')

    attention_status = http_util.get_param_int(args, 'attention_status')
    page = http_util.get_param_int(args, 'page', BaseConfig.DEFAULT_PAGE)
    per_page = http_util.get_param_int(args, 'per_page',
                                       BaseConfig.DEFAULT_PER_PAGE)

    detail_id = None
    if user_id:
        detail_id = user_id
    else:
        detail_id = login_user_id

    if not detail_id:
        return return_no_authorization()

    try:
        res = get_user_attentions(
            user_id=detail_id,
            attention_status=attention_status,
            page=page,
            per_page=per_page,
            login_user_id=login_user_id
        )
        return http_util.return_model(res)
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()
Exemple #4
0
def open_unbinding():
    '''绑定第三方用户'''
    user_id = get_login_user_id(request)
    if not user_id:
        return return_no_authorization()

    login_user = User.query_user(
        id=user_id,
        status=BaseConfig.TYPE_USER_ANONYMOUS
    )
    if login_user:
        return http_util.return_forbidden('请先登录,在进行解绑')

    args = request.json
    need_keys = ['open_id']
    key = check_params(args, *need_keys)
    if key:
        return return_not_found(key)

    open_id = args['open_id']

    open_user = UserOpen.query_open_user(id=open_id)
    if open_user:
        if open_user.user_id != user_id:
            return http_util.return_forbidden('bound not found')
        else:
            UserOpen.update_open_user_by_id(
                id=open_id,
                user_id=""
            )
            return http_util.return_model()
    else:
        return http_util.return_forbidden('bound not found')
def init_request():
    g.user = None
    g.user_id = None
    user_id = get_login_user_id(request)
    if user_id:
        g.user = User.query_user(id=user_id)
        if g.user:
            g.user_id = g.user.id
Exemple #6
0
def create_comment():
    '''评论'''
    args = request.json
    key = check_params(args, 'res_id', 'content')
    if key:
        return return_not_found(key)

    # 获取用户id
    user_id = get_login_user_id(request)
    if not user_id:
        return return_no_authorization()

    res_id = args['res_id']
    content = args['content']

    comt_db.create_comment(user_id=user_id, res_id=res_id, content=content)

    return http_util.return_model()
Exemple #7
0
def open_binding():
    '''解绑第三方用户'''
    user_id = get_login_user_id(request)
    if not user_id:
        return return_no_authorization()

    login_user = User.query_user(
        id=user_id,
        status=BaseConfig.TYPE_USER_ANONYMOUS
    )
    if login_user:
        return http_util.return_forbidden('请先登录,在进行绑定')

    args = request.json
    need_keys = ['open_id', 'source', 'name', 'portrait']
    key = check_params(args, *need_keys)
    if key:
        return http_util.return_param_not_found(key)

    is_exists = UserOpen.query_open_user(
        user_id=user_id,
        source=args.get('source')
    )
    if is_exists:
        return http_util.return_forbidden('该第三方已经绑定过')

    open_id = args['open_id']
    open_user = UserOpen.query_open_user(
        id=open_id
    )

    if open_user:
        if open_user.user_id:
            return http_util.return_forbidden('该账户已被绑定')

        UserOpen.update_open_user_by_id(
            id=open_id,
            user_id=user_id
        )
    else:
        args['user_id'] = user_id
        UserOpen.create_open_user(**args)

    return return_model()
Exemple #8
0
def create_location():
    '''创建坐标'''
    user_id = http_util.get_login_user_id(request)
    if not user_id:
        return http_util.return_no_authorization()
    args = request.json
    key = http_util.check_params(args, 'lat', 'lng')
    if key:
        return http_util.return_param_not_found(key)

    user = User.query_user(id=user_id)
    if not user:
        return http_util.return_forbidden("this user_id can't found user ")

    lat = args['lat']
    lng = args['lng']
    la = models.create_location(
        user_id=user_id,
        lat=lat,
        lng=lng
    )

    delete_poi(
        geo_id=env_config.GEOTABLE_ID,
        user_id=user_id
    )
    create_poi(
        geo_id=env_config.GEOTABLE_ID,
        lat=lat,
        lng=lng,
        user_id=user_id,
        status=user.status
    )

    User.update_user_by_id(
        id=user_id,
        lat=lat,
        lng=lng
    )

    if la:
        return http_util.return_model()
    else:
        return http_util.return_internal_server_error()
Exemple #9
0
def near():
    '''附近坐标'''
    login_user_id = http_util.get_login_user_id(request)
    if not login_user_id:
        return http_util.return_no_authorization()
    args = request.args
    page_size = http_util.get_param_int(args, 'per_page', 50)
    key = http_util.check_params(args, 'radius', 'lat', 'lng')
    if key:
        return http_util.return_param_not_found(key)

    lat = http_util.get_param(args, 'lat')
    lng = http_util.get_param(args, 'lng')
    radius = http_util.get_param_int(args, 'radius', 1000)
    items, total = search_near(
        geo_id=env_config.GEOTABLE_ID,
        lat=lat,
        lng=lng,
        radius=radius,
        page_size=50
    )

    users = []
    for item in items:
        user_id = item['user_id']
        location = item['location']
        lat = location[1]
        lng = location[0]
        user = get_user_detail(id=user_id)

        if not user:
            continue
        user['ext']['lat'] = lat
        user['ext']['lng'] = lng
        if login_user_id != user_id:
            users.append(user)

    res = http_util.make_page_response(users, total, 1, page_size)

    return http_util.return_model(
        data=res
    )
Exemple #10
0
def attention(action):
    user_id = get_login_user_id(request)
    if not user_id:
        return return_no_authorization()

    args = request.json
    key = check_params(args, 'to_user_id')
    if key:
        return return_not_found(key)

    to_user_id = args['to_user_id']

    if action == 'on':
        user_db.on_attontion(user_id=user_id, to_user_id=to_user_id)
    elif action == 'off':
        user_db.off_attontion(user_id=user_id, to_user_id=to_user_id)
    else:
        return return_404()

    return return_model()
Exemple #11
0
def resource_detail():
    """资源详情"""
    args = request.args
    key = http_util.check_params(args, 'res_id', 'res_type')
    if key:
        return http_util.return_param_not_found(key)

    login_user_id = http_util.get_login_user_id(request)

    res_id = http_util.get_param(args, 'res_id')
    res_type = http_util.get_param_int(args, 'res_type')

    try:
        detail = Resource.get_resource_detail(
            res_id=res_id,
            res_type=res_type,
            source_include=['comments', 'related_items', 'view_count',
                            'like_count', 'items',
                            'comment_count'],
            login_user_id=login_user_id
        )

        if not detail:
            return http_util.return_404('res_id not found')

        # 记录用户行为
        Action.create_action(
            user_id=login_user_id,
            type=BaseConfig.TYPE_ACTION_VIEW,
            res_id=res_id,
            res_type=res_type
        )

        return http_util.return_model(
            data=detail
        )
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()
Exemple #12
0
def signin():
    args = request.json

    key = check_params(args, 'mobile', 'code')

    if key:
        return return_not_found(key)
    mobile = args['mobile']
    code = args['code']
    is_exist = sms.verify_code(
        mobile=mobile,
        code=code
    )

    if mobile.startswith('110') and code == '0000':
        is_exist = True

    if mobile == '15890687745' and code == '0000':
        is_exist = True

    if not is_exist:
        return return_model(message='code {} is not found '.format(code),
                            status=404)

    try:
        user = User.query_user(mobile=mobile)

        # 如果用户不存在则创建
        if not user:
            # 获取token携带用户
            login_user_id = get_login_user_id(request)
            login_user = User.query_user(id=login_user_id)
            # 如果登陆用户不存在,则用手机创建用户
            if not login_user or login_user.mobile:
                user = User.create_user(
                    mobile=mobile,
                    status=BaseConfig.TYPE_USER_NORMAL
                )
            else:
                user = user_db.update_user_by_id(
                    user_id=login_user_id,
                    mobile=mobile,
                    status=BaseConfig.TYPE_USER_NORMAL
                )

        authorization = generate_authorization(user.id)

        # 记录用户登录操作
        Action.create_action(
            user_id=user.id,
            type=BaseConfig.TYPE_ACTION_LOGIN,
            res_id=user.id,
            res_type=BaseConfig.TYPE_USER,
            ext=args
        )

        return return_model(
            header={"authorization": authorization}
        )
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()
Exemple #13
0
def signin_open():
    '''第三方登录'''
    args = request.json

    need_keys = ['open_id', 'source', 'name', 'portrait']

    key = check_params(args, *need_keys)

    if key:
        return http_util.return_param_not_found(key)

    login_user_id = get_login_user_id(request)

    try:
        open_id = args['open_id']

        if login_user_id:
            luo = UserOpen.query_open_user(
                id=open_id,
                user_id=login_user_id
            )
            if luo:
                return http_util.return_forbidden('当前用户已登录,不能重复登录')

            login_user = User.query_user(
                id=login_user_id,
                status=BaseConfig.TYPE_USER_NORMAL
            )

            if login_user:
                return http_util.return_forbidden('当前用户已登录,不能重复登录')

        open_user = UserOpen.query_open_user(id=open_id)

        if open_user:

            # 如果第三方用户没有绑定用户,则生成用户并绑定
            if not open_user.user_id:
                binding_user_id = ""
                if login_user_id:
                    binding_user_id = login_user_id
                    User.update_user_by_id(
                        id=login_user_id,
                        name=open_user.name,
                        portrait=open_user.portrait,
                        status=BaseConfig.TYPE_USER_NORMAL
                    )
                else:
                    user = User.create_user(
                        name=args.get('name'),
                        portrait=args.get('portrait')
                    )
                    binding_user_id = user.id

                open_user = UserOpen.update_open_user_by_id(
                    id=open_id,
                    user_id=binding_user_id
                )
        else:
            # 创建第三方用户
            if login_user_id:
                args['user_id'] = login_user_id
                open_user = UserOpen.create_open_user(**args)
                User.update_user_by_id(
                    id=login_user_id,
                    name=open_user.name,
                    portrait=open_user.portrait,
                    status=BaseConfig.TYPE_USER_NORMAL
                )
            else:
                open_user = UserOpen.create_open_user_and_user(**args)

        user_id = open_user.user_id

        authorization = generate_authorization(user_id)

        # 记录用户登录操作
        Action.create_action(
            user_id=user_id,
            type=BaseConfig.TYPE_ACTION_LOGIN,
            res_id=user_id,
            res_type=BaseConfig.TYPE_USER,
            ext=args
        )

        return return_model(
            header={"authorization": authorization}
        )
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()