Example #1
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()
Example #2
0
    def get_show_resource(cls, login_user_id):
        hp = HomePage.query_daily_resource()

        if hp:
            hp_show = Resource.get_resource_detail(hp.res_id, hp.res_type)
            if hp_show:
                # 记录用户查看状态
                if login_user_id:
                    is_exists = Action.query_action(
                        user_id=login_user_id,
                        type=BaseConfig.TYPE_ACTION_VIEW_HOME_PAGE_SHOW,
                        res_id=hp_show['res_id'],
                        res_type=hp_show['res_type']
                    )
                    if is_exists:
                        return None

                    Action.create_action(
                        user_id=login_user_id,
                        type=BaseConfig.TYPE_ACTION_VIEW_HOME_PAGE_SHOW,
                        res_id=hp_show['res_id'],
                        res_type=hp_show['res_type']
                    )

                return hp_show

        return None
Example #3
0
def update_user():
    '''修改用户信息'''
    headers = request.headers
    user_id = g.user_id
    args = request.json

    try:
        mobile = args.get('mobile')
        if mobile:
            mobile_exists = User.query_user(mobile=mobile)
            if mobile_exists:
                return http_util.return_forbidden('手机号已经绑定其他用户')

        u = User.update_user_by_id(id=user_id, **args)
        if u:
            # 记录用户操作
            Action.create_action(
                user_id=user_id,
                type=BaseConfig.TYPE_ACTION_UPDATE,
                res_id=user_id,
                res_type=BaseConfig.TYPE_USER,
                ext=args
            )

            Search.sync_index_by_id(user_id, BaseConfig.TYPE_USER)

            return return_model()
        else:
            return http_util.return_internal_server_error()
    except BaseException as e:
        app.logger.error(e)
        return http_util.return_internal_server_error()
Example #4
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)
Example #5
0
def save_image():
    user_id = g.user_id
    args = request.json
    urls = http_util.get_param(args, 'urls')

    if urls:
        for url in urls:
            image = image_db.create_image(user_id=user_id, url=url)
            # 记录用户上传图片行为
            Action.create_action(user_id=user_id,
                                 type=BaseConfig.TYPE_ACTION_UPLOAD,
                                 res_id=image.id,
                                 res_type=BaseConfig.TYPE_IMAGE)

            detail = Image.get_image_detail(image.id)
            User.update_user_by_id(id=user_id, last_upload=detail)
        return http_util.return_model()
    else:
        return http_util.return_param_not_found('urls')
Example #6
0
def save_video():
    user_id = g.user_id
    args = request.json

    url = http_util.get_param(args, 'url', "")
    poster = http_util.get_param(args, 'poster', "")

    video = Video.create_video(user_id=user_id, url=url, poster=poster)

    # 记录用户上传视频
    Action.create_action(user_id=user_id,
                         type=BaseConfig.TYPE_ACTION_UPLOAD,
                         res_id=video.id,
                         res_type=BaseConfig.TYPE_VIDEO_PLAY)

    detail = Video.get_video_detail(video.id)

    User.update_user_by_id(id=user_id, last_upload=detail)

    return http_util.return_model()
Example #7
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()
Example #8
0
def detail():
    login_user_id = g.user_id
    args = request.args
    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')

    # 记录用户观看其他用户的记录
    Action.create_action(
        user_id=login_user_id,
        type=BaseConfig.TYPE_ACTION_VIEW,
        res_id=detail_id,
        res_type=BaseConfig.TYPE_USER
    )

    user = get_user_detail(
        id=detail_id,
        login_user_id=login_user_id,
        source_include=['opens', 'view_count', 'attention_count', 'banner']
    )

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

    if not user_id:
        user['ext']['rongcloud_token'] = rc_user.getToken(
            userId=login_user_id,
            name=user['name'],
            portraitUri=user['portrait']
        ).result['token']

    return return_model(user)
Example #9
0
def get_user_detail(id, source_include=[], source_exclude=[], params=None):
    """获取用户详情"""
    user = User.query_user(id=id)

    item = User.get_detail(id)

    # 查找第三方用户
    if 'opens' in source_include:
        opens = []
        for open_user in user.opens:
            opens.append({"open_id": open_user.id, "source": open_user.source})
        item['opens'] = opens

    login_user_id = params.get('login_user_id', None)
    if login_user_id:
        # 拼装关注状态
        attention_status = get_user_attention_status(login_user_id, id)

        item['attention_status'] = attention_status

    # 观看数量
    if 'view_count' in source_include:
        views = Action.query_user_views(id)
        item['ext']['view_count'] = len(views)
    # 关注数
    if 'attention_count' in source_include:
        attens = UserAttention.query_user_attentions(to_user_id=id)
        item['ext']['attention_count'] = len(attens)

    # 获取最后一次地理位置
    item['ext']['last_location_time'] = Location.get_user_last_location_time(
        id)

    if source_exclude:
        for field in source_exclude:
            del item[field]
    return item
Example #10
0
    def get_resource_detail(cls, res_id, res_type, source_include=[], **params):
        """获取资源详情"""
        detail = None
        login_user_id = g.user_id
        if res_type == BaseConfig.TYPE_VIDEO_PLAY:
            detail = Video.get_video_detail(res_id)
        elif res_type == BaseConfig.TYPE_ARTICLE:
            detail = Article.get_article_detail(res_id)
            if not detail:
                app.logger.error('{} not found'.format(res_id))
                return None
            del detail['content']
        elif res_type == BaseConfig.TYPE_AUDIO:
            detail = Audio.get_audio_detail(res_id)
        elif res_type == BaseConfig.TYPE_IMAGE:
            detail = Image.get_image_detail(res_id)
        elif res_type == BaseConfig.TYPE_COLLECTION:
            detail = Collection.get_collection_detail(res_id)
            subs = []
            if 'items' in source_include:
                items = CollectionResource.query_items(collection_id=res_id)
                for item in items:
                    sub_id = item.res_id
                    sub_type = item.res_type

                    sub_detail = Resource.get_resource_detail(
                        res_id=sub_id,
                        res_type=sub_type,
                        source_include=['comment_count', 'view_count',
                                        'like_count'],
                        login_user_id=login_user_id
                    )
                    if sub_detail:
                        subs.append(sub_detail)
            detail['items'] = subs

        if not detail:
            return None

        # 添加评论
        if 'comments' in source_include:
            comments = get_comments(
                res_id=res_id,
                start=BaseConfig.MAX_START,
                per_page=5
            )
            detail['comments'] = comments

        # 相关视频
        if 'related_items' in source_include and res_type != BaseConfig.TYPE_COLLECTION:
            related_videos = Video.query_related_videos(res_id)
            detail['related_items'] = related_videos

        ext = detail.get('ext', {})
        # 观看数量
        if 'view_count' in source_include:
            count = Action.query_count(
                type=BaseConfig.TYPE_ACTION_VIEW,
                res_id=res_id
            )
            ext['view_count'] = count

        # 点赞数量
        if 'like_count' in source_include:
            count = LikeInfo.query_count(
                res_id=res_id,
                res_type=res_type
            )
            ext['like_count'] = count

        # 评论数量
        if 'comment_count' in source_include:
            count = Comment.query_count(
                res_id=res_id,
                res_type=res_type
            )
            ext['comment_count'] = count

        detail['ext'] = ext

        # 关注状态
        detail['is_like'] = 0
        if login_user_id:
            is_like = LikeInfo.query_like(
                user_id=login_user_id,
                res_id=res_id,
                res_type=res_type
            )
            if is_like:
                detail['is_like'] = 1
        return detail
Example #11
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()
Example #12
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()