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 set_item(item_id, idict): fd_list = ('title', 'content', 'author_id', 'atype', 'url', 'post_id', 'show', 'status', 'date_create', 'date_update') cset = set(idict.keys()) if not cset.issubset(fd_list): raise BackendError('ParamError', '更新的字段不允许') i = Item.query.get(item_id) for k, v in idict.items(): if v is not None: setattr(i, k, v) try: db.session.commit() except: db.session.rollback() raise return i.json
def set_post(post_id,pdict): fd_list = ('title','content','pic_small','pic_big','author_id','show', 'recommended','date_create','date_update') cset = set(pdict.keys()) if not cset.issubset(fd_list): raise BackendError('ParamError','更新的字段不允许') post = Post.query.get(post_id) for k,v in pdict.items(): if v is not None: setattr(post,k,v) try: db.session.commit() except: db.session.rollback() raise return post.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_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