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,{})
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,{})
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]
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]
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
def get_comment(comment_id): assert_error(type(comment_id) == types.IntType, 'ParamError') comm = Comment.query.get(comment_id) return comm.json
def is_username_exist(username): assert_error(type(username) == types.StringType, 'ParamError') return True if _check_username(username) else False
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
def get_comment(comment_id): assert_error(type(comment_id) == types.IntType,'ParamError') comm = Comment.query.get(comment_id) return comm.json
def is_email_exist(email): assert_error(type(email) == types.StringType,'ParamError') return True if _check_email(email) else False
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]
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
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]
def is_username_exist(username): assert_error(type(username) == types.StringType,'ParamError') return True if _check_username(username) else False
def is_email_exist(email): assert_error(type(email) == types.StringType, 'ParamError') return True if _check_email(email) else False