Example #1
0
def create_image(params=None):
    user_id = get_param(params, 'user_id', 0)
    url = get_param(params, 'url', '')

    v = Image(
        id=snowflake.generate(),
        user_id=user_id,
        url=url,
        is_del=BaseConfig.DEFAULT_IS_DEL,
    )

    db.session.add(v)
    db.session.commit()
    return v
Example #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()
Example #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()
Example #4
0
def update_open_user_by_id(open_id, params=None):
    uo = UserOpen.query.get(open_id)
    uo.user_id = get_param(params, 'user_id', uo.user_id)

    res = db.session.commit()

    print(res)
    return uo
Example #5
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
    )
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 create_user(**params):
    '''创建用户'''
    mobile = get_param(params, 'mobile', '')
    name = get_param(params, 'name', '')
    status = get_param(params, 'status', BaseConfig.DEFAULT_USER_STATUS)
    new_id = snowflake.generate()

    ext = {"location": "", "sign": "", "lat": 0, "lng": 0}

    u = User(id=new_id,
             mobile=mobile,
             name=name,
             portrait=BaseConfig.DEFAULT_PORTRAIT,
             age=BaseConfig.DEFAULT_AGE,
             gender=BaseConfig.DEFAULT_GENDER,
             status=status,
             ext=ext,
             is_del=BaseConfig.DEFAULT_IS_DEL)
    db.session.add(u)
    db.session.commit()
    return u
Example #8
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 #9
0
def resource_delete_json():
    '''首页列表'''
    args = request.form

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

    if not res_id or not res_type:
        return http_util.return_forbidden('res_id or res_type is error')

    check_use = Resource.check_resource_use_status(res_id, res_type)
    if check_use:
        return http_util.return_forbidden("{},不能删除".format(check_use))

    res = Resource.delete_resource(res_id, res_type)

    if res:
        return return_model()
    else:
        return http_util.return_internal_server_error()
Example #10
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 #11
0
def make_user_by_id(user_id, params=None):
    '''根据user_id生成用户对象'''
    user = query_user_by_id(user_id)

    if not user:
        return None
    item = user.to_json()

    item['create_ts'] = utils.make_timestamp_for_sql_time(item['create_ts'])
    item['user_id'] = item['id']

    del item['update_ts']
    del item['id']
    del item['is_del']

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

    return item
Example #12
0
def update_user_by_id(user_id, **params):
    '''根据用户id修改信息'''
    u = User.query.filter_by(id=user_id).first()
    u.name = get_param(params, 'name', u.name)
    u.mobile = get_param(params, 'mobile', u.mobile)
    u.portrait = get_param(params, 'portrait', u.portrait)
    u.gender = get_param(params, 'gender', u.gender)
    u.age = get_param(params, 'age', u.age)
    u.status = get_param(params, 'status', u.status)

    ext = u.ext
    print(ext)
    location = get_param(params, 'location', ext['location'])
    sign = get_param(params, 'sign', ext['sign'])
    sign = get_param(params, 'sign', ext['sign'])
    lat = get_param(params, 'lat', ext['lat'])
    lng = get_param(params, 'lng', ext['lng'])

    json_ext = {
        "location": location,
        "sign": sign,
        "lat": lat,
        "lng": lng,
    }
    u.ext = json_ext
    db.session.commit()
    return u
Example #13
0
    def update_user_by_id(cls, id, **params):
        '''根据用户id修改信息'''
        u = User.query.get(id)
        u.name = get_param(params, 'name', u.name)
        u.mobile = get_param(params, 'mobile', u.mobile)
        u.portrait = get_param(params, 'portrait', u.portrait)
        u.gender = get_param(params, 'gender', u.gender)
        u.age = get_param(params, 'age', u.age)
        u.status = get_param(params, 'status', u.status)
        u.is_del = get_param(params, 'is_del', u.is_del)

        ext = u.ext
        location = get_param(params, 'location', ext['location'])
        sign = get_param(params, 'sign', ext['sign'])
        lat = get_param(params, 'lat', ext['lat'])
        lng = get_param(params, 'lng', ext['lng'])

        json_ext = {
            "location": location,
            "sign": sign,
            "lat": lat,
            "lng": lng,
        }
        u.ext = json_ext
        u.banner = params.get('banner', u.banner)
        u.last_upload = params.get('last_upload', u.last_upload)
        db.session.commit()
        return u