def get_tag_post(tag_id, limit=10, offset=0): assert_error(offset >= 0, 'ParamError') posts = Post.query.join(Tagging,Post.id == Tagging.taggable_id).\ filter(Tagging.tag_id == tag_id).\ filter(Post.show == True).\ order_by(Tagging.date_create.desc()).\ limit(limit).offset(offset).all() return [p.json for p in posts]
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 add_play(user_id, post_id): assert_error(all([type(x) for x in [user_id, post_id]]), 'ParamError') ura = UserPlayAsso() ura.user_id = user_id ura.post_id = post_id try: db.session.add(ura) db.session.commit() except: db.session.rollback() raise BackendError('InternalError', traceback.format_exc())
def get_tag_post(tag_id, limit=10, offset=0): assert_error(offset >= 0, "ParamError") posts = ( Post.query.join(Tagging, Post.id == Tagging.taggable_id) .filter(Tagging.tag_id == tag_id) .filter(Post.show == True) .order_by(Tagging.date_create.desc()) .limit(limit) .offset(offset) .all() ) return [p.json for p in posts]
def add_play(user_id,post_id): assert_error(all([type(x) for x in [user_id,post_id]]),'ParamError') ura = UserPlayAsso() ura.user_id = user_id ura.post_id = post_id try: db.session.add(ura) db.session.commit() except: db.session.rollback() raise BackendError('InternalError',traceback.format_exc())
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 get_user_post(user_id,offset=0,limit=20): assert_error(offset >= 0,'ParamError') posts = Post.query.filter(Post.author_id == user_id).\ order_by(Post.date_create.desc()).\ limit(limit).offset(offset).all() _posts = [] for p in posts: _u = p.user.json _p = p.json _p.update({'user':_u}) _posts.append(_p) return _posts
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_user_post(user_id, offset=0, limit=20): assert_error(offset >= 0, 'ParamError') posts = Post.query.filter(Post.author_id == user_id).\ order_by(Post.date_create.desc()).\ limit(limit).offset(offset).all() _posts = [] for p in posts: _u = p.user.json _p = p.json _p.update({'user': _u}) _posts.append(_p) return _posts
def get_user_liked_post(user_id, offset=0, limit=20): assert_error(offset >= 0, 'ParamError') posts = Post.query.join(UserLikeAsso,Post.id == UserLikeAsso.post_id).\ filter(UserLikeAsso.user_id == user_id).\ order_by(UserLikeAsso.date_create.desc()).\ limit(limit).offset(offset).all() _posts = [] for p in posts: _u = p.user.json _p = p.json _p.update({'user': _u}) _posts.append(_p) return _posts
def del_like(user_id,post_id): assert_error(all([type(x) == types.IntType for x in [user_id,post_id]]), 'ParamError') ula = UserLikeAsso.query.filter(UserLikeAsso.user_id == user_id).\ filter(UserLikeAsso.post_id == post_id).first() if ula is None: return try: db.session.delete(ula) db.session.commit() except: db.session.rollback() raise BackendError('InternalError',traceback.format_exc()) else: return True
def get_user_liked_post(user_id,offset=0,limit=20): assert_error(offset >= 0,'ParamError') posts = Post.query.join(UserLikeAsso,Post.id == UserLikeAsso.post_id).\ filter(UserLikeAsso.user_id == user_id).\ order_by(UserLikeAsso.date_create.desc()).\ limit(limit).offset(offset).all() _posts = [] for p in posts: _u = p.user.json _p = p.json _p.update({'user':_u}) _posts.append(_p) return _posts
def del_like(user_id, post_id): assert_error(all([type(x) == types.IntType for x in [user_id, post_id]]), 'ParamError') ula = UserLikeAsso.query.filter(UserLikeAsso.user_id == user_id).\ filter(UserLikeAsso.post_id == post_id).first() if ula is None: return try: db.session.delete(ula) db.session.commit() except: db.session.rollback() raise BackendError('InternalError', traceback.format_exc()) else: return True
def new_insall(user_id,device_token,version='',device_type=''): assert_error(type(user_id) == types.IntType,'ParamError') assert_error(type(device_token) == types.StringType,'ParamError') install = Install(user_id=user_id, device_token=device_token, version=version, device_type=device_type) try: db.session.add(install) db.session.commit() except: db.session.rollback() raise return install.json
def new_insall(user_id, device_token, version='', device_type=''): assert_error(type(user_id) == types.IntType, 'ParamError') assert_error(type(device_token) == types.StringType, 'ParamError') install = Install(user_id=user_id, device_token=device_token, version=version, device_type=device_type) try: db.session.add(install) db.session.commit() except: db.session.rollback() raise return install.json
def get_user_by_uid(uid): assert_error(type(uid) in (types.StringType,types.ListType),'ParamError') multi = False if type(uid) == types.ListType: multi = True else: uid = uid, users = User.query.filter(User.uid.in_(uid)).all() if len(users) == 0: 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 add_post(title, author_id, video_url, pic_small='', pic_big='', show=True, recommended=False): assert_error(type(title) == types.StringType, 'ParamError') assert_error(type(author_id) == types.IntType, 'ParamError') assert_error(type(video_url) == types.StringType, 'ParamError') qd = { 'title': title, 'author_id': author_id, 'video_url': video_url, '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_comment(post_id, content, author_id): assert_error(type(post_id) == types.IntType, 'ParamError') assert_error(type(author_id) == types.IntType, 'ParamError') assert_error(type(content) == types.StringType, 'ParamError') qd = {'post_id': post_id, 'content': content, 'author_id': author_id} co = Comment(**qd) try: db.session.add(co) db.session.commit() except: db.session.rollback() raise BackendError('InternalError', traceback.format_exc()) return co.json
def add_comment(post_id,content,author_id): assert_error(type(post_id) == types.IntType,'ParamError') assert_error(type(author_id) == types.IntType,'ParamError') assert_error(type(content) == types.StringType,'ParamError') qd = { 'post_id':post_id, 'content':content, 'author_id':author_id } co = Comment(**qd) try: db.session.add(co) db.session.commit() except: db.session.rollback() raise BackendError('InternalError',traceback.format_exc()) return co.json
def add_post(title,author_id,video_url,pic_small='',pic_big='',show=True,recommended=False): assert_error(type(title) == types.StringType,'ParamError') assert_error(type(author_id) == types.IntType,'ParamError') assert_error(type(video_url) == types.StringType,'ParamError') qd = { 'title':title, 'author_id':author_id, 'video_url':video_url, '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,photo_url,uid,signature='',access_token=''): assert_error(type(username)==types.StringType,'ParamError','用户昵称应该为字符串') assert_error(photo_url == None or type(photo_url) == types.StringType,'ParamError') assert_error(type(uid) == types.StringType,'ParamError') qd = { 'username':username, 'photo_url':photo_url or '', 'uid':uid, 'signature':signature, 'access_token':access_token } try: user = User(**qd) db.session.add(user) db.session.commit() except: db.session.rollback() raise BackendError('InternalError',traceback.format_exc()) return user.json
def get_activity_by_user(user_id,limit=30,offset=0): assert_error(type(user_id) == types.IntType,'ParamError') activitys = Activity.query.filter(Activity.to_id == user_id).\ order_by(db.desc(Activity.date_create)).limit(limit).offset(offset).all() return [a.json for a in activitys]
def get_new_activity_count(user_id, last_update_time): assert_error(type(user_id) == types.IntType, 'ParamError') count = Activity.query.filter(Activity.to_id == user_id).\ filter(Activity.date_create > last_update_time).count() return count
def is_email_exist(email): assert_error(type(email) == types.StringType,'ParamError') return True if _check_email(email) else False
def is_username_exist(username): assert_error(type(username) == types.StringType,'ParamError') return True if _check_username(username) 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 get_new_activity_count(user_id,last_update_time): assert_error(type(user_id) == types.IntType,'ParamError') count = Activity.query.filter(Activity.to_id == user_id).\ filter(Activity.date_create > last_update_time).count() return count
def get_activity_by_user(user_id, limit=30, offset=0): assert_error(type(user_id) == types.IntType, 'ParamError') activitys = Activity.query.filter(Activity.to_id == user_id).\ order_by(db.desc(Activity.date_create)).limit(limit).offset(offset).all() return [a.json for a in activitys]
def get_tag(tag_id): assert_error(type(tag_id) == types.IntType, 'ParamError') tag = Tag.query.get(tag_id) return tag.json
def get_comment(comment_id): assert_error(type(comment_id) == types.IntType,'ParamError') comm = Comment.query.get(comment_id) return comm.json
def get_tag_post_count(tag_id): assert_error(type(tag_id) == types.IntType, 'ParamError') count = Tagging.query.filter(Tagging.tag_id == tag_id).count() return count
def get_comment(comment_id): assert_error(type(comment_id) == types.IntType, 'ParamError') comm = Comment.query.get(comment_id) return comm.json
def get_tag(tag_id): assert_error(type(tag_id) == types.IntType, "ParamError") tag = Tag.query.get(tag_id) return tag.json
def get_tag_post_count(tag_id): assert_error(type(tag_id) == types.IntType, "ParamError") count = Tagging.query.filter(Tagging.tag_id == tag_id).count() return count