Example #1
0
def auth_user(email,passstr):
    assert_error(type(email) == types.StringType,'ParamError')
    assert_error(type(passstr) == types.StringType,'ParamError')
    
    user = User.query.filter(User.email == email).first()
    return (True,user.json) if user and check_password_hash(user.password,passstr) \
                            else (False,{})
Example #2
0
def auth_user(email, passstr):
    assert_error(type(email) == types.StringType, 'ParamError')
    assert_error(type(passstr) == types.StringType, 'ParamError')

    user = User.query.filter(User.email == email).first()
    return (True,user.json) if user and check_password_hash(user.password,passstr) \
                            else (False,{})
Example #3
0
def get_post_comment(post_id,limit=50,offset=0,show=True):
    assert_error(type(post_id) == types.IntType,'ParamError')
    _ans = [Comment.show == True,] if show else []
    _ans.append(Comment.post_id == post_id)
    q = reduce(db.and_,_ans)
    comms = Comment.query.filter(q).order_by(Comment.date_create.desc()).\
            limit(limit).offset(offset).all()
    return [c.json for c in comms]
Example #4
0
def get_post_comment(post_id, limit=50, offset=0, show=True):
    assert_error(type(post_id) == types.IntType, 'ParamError')
    _ans = [
        Comment.show == True,
    ] if show else []
    _ans.append(Comment.post_id == post_id)
    q = reduce(db.and_, _ans)
    comms = Comment.query.filter(q).order_by(Comment.date_create.desc()).\
            limit(limit).offset(offset).all()
    return [c.json for c in comms]
Example #5
0
def follow_user(fid,tid):
    assert_error(all([type(_id) == types.IntType for _id in [fid,tid]]),'ParamError')
    try:
        asso = UserFollowAsso(user_id=fid,user_id_to=tid)
        db.session.add(asso)
        db.session.commit()
    except:
        db.session.rollback()
        raise
    else:
        return asso.id
Example #6
0
def follow_user(fid, tid):
    assert_error(all([type(_id) == types.IntType for _id in [fid, tid]]),
                 'ParamError')
    try:
        asso = UserFollowAsso(user_id=fid, user_id_to=tid)
        db.session.add(asso)
        db.session.commit()
    except:
        db.session.rollback()
        raise
    else:
        return asso.id
Example #7
0
def set_user(user_id, info_d):
    assert_error(type(user_id) == types.IntType, 'ParamError')
    user = User.query.get(user_id)
    try:
        for k, v in info_d.items():
            if v is not None:
                setattr(user, k, v)
        db.session.commit()
    except:
        db.session.rollback()
        raise
    else:
        return user.json
Example #8
0
def set_user(user_id,info_d):
    assert_error(type(user_id) == types.IntType,'ParamError')
    user = User.query.get(user_id)
    try:
        for k,v in info_d.items():
            if v is not None:
                setattr(user,k,v)
        db.session.commit()
    except:
        db.session.rollback()
        raise
    else:
        return user.json
Example #9
0
def unfollow_user(fid,tid):
    assert_error(all([type(_id) == types.IntType for _id in [fid,tid]]),'ParamError')
    asso = UserFollowAsso.query.filter(db.and_(UserFollowAsso.user_id==fid,UserFollowAsso.user_id_to==tid)).\
            first()
    if asso is None:
        return
    try:
        db.session.delete(asso)
        db.session.commit()
    except:
        db.session.rollback()
        raise
    else:
        return True
Example #10
0
def unfollow_user(fid, tid):
    assert_error(all([type(_id) == types.IntType for _id in [fid, tid]]),
                 'ParamError')
    asso = UserFollowAsso.query.filter(db.and_(UserFollowAsso.user_id==fid,UserFollowAsso.user_id_to==tid)).\
            first()
    if asso is None:
        return
    try:
        db.session.delete(asso)
        db.session.commit()
    except:
        db.session.rollback()
        raise
    else:
        return True
Example #11
0
def get_item(item_id):
    assert_error(type(item_id) in (types.IntType,types.ListType),'ParamError')
    multi = False
    if type(item_id) == types.ListType:
        assert_error(all([type(i) == types.IntType for i in item_id]),'ParamError')
        multi = True
    else:
        item_id = item_id,

    items = Item.query.filter(Item.id.in_(item_id)).all()
    if len(items) == 0:
        raise BackendError('EmptyError','item不存在')

    if multi:
        return dict([(i.id,i.json) for i in items])
    else:
        return items[0].json
Example #12
0
def get_user(user_id):
    multi = False
    if type(user_id) == types.ListType:
        assert_error(all([type(u) == types.IntType for u in user_id]),'ParamError')
        multi = True
    else:
        assert_error(type(user_id) == types.IntType,'ParamError')
        user_id = user_id,

    users = User.query.filter(User.id.in_(user_id)).all()
    if not users:
        raise BackendError('EmptyError','用户不存在')

    if multi:
        return [u.json for u in users]
    else:
        return users[0].json
Example #13
0
def get_user(user_id):
    multi = False
    if type(user_id) == types.ListType:
        assert_error(all([type(u) == types.IntType for u in user_id]),
                     'ParamError')
        multi = True
    else:
        assert_error(type(user_id) == types.IntType, 'ParamError')
        user_id = user_id,

    users = User.query.filter(User.id.in_(user_id)).all()
    if not users:
        raise BackendError('EmptyError', '用户不存在')

    if multi:
        return [u.json for u in users]
    else:
        return users[0].json
Example #14
0
def get_item(item_id):
    assert_error(
        type(item_id) in (types.IntType, types.ListType), 'ParamError')
    multi = False
    if type(item_id) == types.ListType:
        assert_error(all([type(i) == types.IntType for i in item_id]),
                     'ParamError')
        multi = True
    else:
        item_id = item_id,

    items = Item.query.filter(Item.id.in_(item_id)).all()
    if len(items) == 0:
        raise BackendError('EmptyError', 'item不存在')

    if multi:
        return dict([(i.id, i.json) for i in items])
    else:
        return items[0].json
Example #15
0
def add_post(title,author_id,content=None,pic_small='',pic_big='',show=True,recommended=False):
    assert_error(type(title) == types.StringType,'ParamError')
    assert_error(type(author_id) == types.IntType,'ParamError')
    
    qd = {
            'title':title,
            'author_id':author_id,
            'content':content,
            'pic_small':pic_small,
            'pic_big':pic_big,
            'show':show,
            'recommended':recommended,
            }
    try:
        p = Post(**qd)
        db.session.add(p)
        db.session.commit()
    except:
        db.session.rollback()
        raise BackendError('InternalError',traceback.format_exc())
    return p.json
Example #16
0
def add_post(title, author_id, content=None, pic_small="", pic_big="", show=True, recommended=False):
    assert_error(type(title) == types.StringType, "ParamError")
    assert_error(type(author_id) == types.IntType, "ParamError")

    qd = {
        "title": title,
        "author_id": author_id,
        "content": content,
        "pic_small": pic_small,
        "pic_big": pic_big,
        "show": show,
        "recommended": recommended,
    }
    try:
        p = Post(**qd)
        db.session.add(p)
        db.session.commit()
    except:
        db.session.rollback()
        raise BackendError("InternalError", traceback.format_exc())
    return p.json
Example #17
0
def add_user(username,email,passstr):
    assert_error(type(email)==types.StringType,'ParamError','邮箱应该为字符串')
    assert_error(type(passstr)==types.StringType,'ParamError','密码应该为字符串')
    assert_error(type(username)==types.StringType,'ParamError','用户昵称应该为字符串')
    
    if _check_username(username):
        raise BackendError('UsernameError','用户名已经存在')

    if _check_email(email):
        raise BackendError('EmailError','邮箱已经存在')

    try:
        passstr = generate_password_hash(passstr)
        user = User(email=email,username=username,password=passstr)
        db.session.add(user)
        db.session.commit()
    except:
        db.session.rollback()
        raise BackendError('InternalError',traceback.format_exc())

    return user.json
Example #18
0
def add_item(title,author_id,post_id,atype,url=None,content=None):
    assert_error(type(title) == types.StringType,'ParamError')
    assert_error(type(author_id) == types.IntType,'ParamError')
    assert_error(type(post_id) == types.IntType,'ParamError')
    
    qd = {
            'title':title,
            'author_id':author_id,
            'post_id':post_id,
            'content':content,
            'atype':atype,
            'url':url,
            }
    try:
        i = Item(**qd)
        db.session.add(i)
        db.session.commit()
    except:
        db.session.rollback()
        raise BackendError('InternalError',traceback.format_exc())
    return i.json
Example #19
0
def add_item(title, author_id, post_id, atype, url=None, content=None):
    assert_error(type(title) == types.StringType, 'ParamError')
    assert_error(type(author_id) == types.IntType, 'ParamError')
    assert_error(type(post_id) == types.IntType, 'ParamError')

    qd = {
        'title': title,
        'author_id': author_id,
        'post_id': post_id,
        'content': content,
        'atype': atype,
        'url': url,
    }
    try:
        i = Item(**qd)
        db.session.add(i)
        db.session.commit()
    except:
        db.session.rollback()
        raise BackendError('InternalError', traceback.format_exc())
    return i.json
Example #20
0
def add_user(username, email, passstr):
    assert_error(type(email) == types.StringType, 'ParamError', '邮箱应该为字符串')
    assert_error(type(passstr) == types.StringType, 'ParamError', '密码应该为字符串')
    assert_error(
        type(username) == types.StringType, 'ParamError', '用户昵称应该为字符串')

    if _check_username(username):
        raise BackendError('UsernameError', '用户名已经存在')

    if _check_email(email):
        raise BackendError('EmailError', '邮箱已经存在')

    try:
        passstr = generate_password_hash(passstr)
        user = User(email=email, username=username, password=passstr)
        db.session.add(user)
        db.session.commit()
    except:
        db.session.rollback()
        raise BackendError('InternalError', traceback.format_exc())

    return user.json
Example #21
0
def get_comment(comment_id):
    assert_error(type(comment_id) == types.IntType, 'ParamError')
    comm = Comment.query.get(comment_id)
    return comm.json
Example #22
0
def is_username_exist(username):
    assert_error(type(username) == types.StringType, 'ParamError')
    return True if _check_username(username) else False
Example #23
0
def is_following_user(fid, tid):
    assert_error(all([type(_id) == types.IntType for _id in [fid, tid]]),
                 'ParamError')
    _count = UserFollowAsso.query.filter(UserFollowAsso.user_id == fid).\
            filter(UserFollowAsso.user_id_to == tid).count()
    return True if _count > 0 else False
Example #24
0
def get_comment(comment_id):
    assert_error(type(comment_id) == types.IntType,'ParamError')
    comm = Comment.query.get(comment_id)
    return comm.json
Example #25
0
def is_email_exist(email):
    assert_error(type(email) == types.StringType,'ParamError')
    return True if _check_email(email) else False
Example #26
0
def get_user_follower(user_id, limit=50, offset=0):
    assert_error(type(user_id) == types.IntType, 'ParamError')
    follows = User.query.join(UserFollowAsso,User.id == UserFollowAsso.user_id).\
            filter(UserFollowAsso.user_id_to == user_id).limit(limit).offset(offset).all()
    return [u.json for u in follows]
Example #27
0
def is_following_user(fid,tid):
    assert_error(all([type(_id) == types.IntType for _id in [fid,tid]]),'ParamError')
    _count = UserFollowAsso.query.filter(UserFollowAsso.user_id == fid).\
            filter(UserFollowAsso.user_id_to == tid).count()
    return True if _count > 0 else False
Example #28
0
def get_user_follower(user_id,limit=50,offset=0):
    assert_error(type(user_id) == types.IntType,'ParamError')
    follows = User.query.join(UserFollowAsso,User.id == UserFollowAsso.user_id).\
            filter(UserFollowAsso.user_id_to == user_id).limit(limit).offset(offset).all()
    return [u.json for u in follows]
Example #29
0
def is_username_exist(username):
    assert_error(type(username) == types.StringType,'ParamError')
    return True if _check_username(username) else False
Example #30
0
def is_email_exist(email):
    assert_error(type(email) == types.StringType, 'ParamError')
    return True if _check_email(email) else False