def add_comment(): jdata = request.get_json() state = OPER_INVALID cid = jdata['cid'] receiver_id = jdata['receiver_id'] comment_type = jdata['type'] try: session = Session() item = Comment(collection_id=cid, type=comment_type, content=jdata['content'], receiver_id=receiver_id, create_by=jdata['create_by']) session.add(item) session.commit() #use trigger more flexible session.query(Collection).filter(Collection.id == cid).update( {Collection.comment_count: Collection.comment_count + 1}) session.commit() state = constants.OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = constants.OPER_EXCEPTION
def del_profession(pid): try: session = Session() session.query(Profession).filter(Profession.id == pid).delete() session.commit() except Exception, e: print e
def add_favorite(): jdata = request.get_json() state = constants.OPER_INVALID bids = jdata['bids'].strip() ids = bids[1:-1].split(',') try: session = Session() for bid in ids: item = Favorite(collection_id=jdata['cid'], box_id=bid, create_by=jdata['create_by']) session.add(item) action = UserAction(uid=jdata['create_by'], action_type=ACTION_COLLECT_COLLECTION, obj_id=jdata['cid']) session.add(action) session.query(FavoriteBox).filter(FavoriteBox.id.in_( tuple(ids))).update( {FavoriteBox.box_count: FavoriteBox.box_count + 1}, synchronize_session=False) session.commit() state = constants.OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = constants.OPER_EXCEPTION
def del_topic(did): try: session= Session(); session.query(Topic).filter(Topic.id==did).delete(); session.commit(); except Exception,e: print e;
def update_userinfo(): jdata = request.get_json() fields = {} if 'name' in jdata: fields['name'] = jdata['name'] if 'sex' in jdata: fields['sex'] = jdata['sex'] if 'signature' in jdata: fields['signature'] = jdata['signature'] if 'profession' in jdata: fields['profession'] = jdata['profession'] if 'residence' in jdata: fields['residence'] = jdata['residence'] if 'education' in jdata: fields['education'] = jdata['education'] state = OPER_INVALID session = Session() try: session.query(User).filter(User.id == jdata['uid']).update(fields) session.commit() state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def get_userpacks(): req_uid = request.get_json().get("req_uid"); uid = request.get_json().get("uid"); ismyself = False ; if req_uid == uid : #myself ismyself = True ; off = request.get_json().get("off"); state = OPER_INVALID; items = [] ; session=Session(); try: if ismyself: relist = session.query(CollectPack).filter(CollectPack.create_by == uid)\ .offset(int(off)).limit(10).all(); else: relist = session.query(CollectPack).filter(CollectPack.create_by == uid,CollectPack.is_private == False)\ .offset(int(off)).limit(10).all(); for e in relist : d={ 'id':e.id, 'pack_name':e.pack_name, 'pack_desc':e.pack_desc, 'pack_type':e.type, 'focus_count':e.focus_count, 'collect_count':e.collect_count, 'topic_ids':e.topic_id, 'create_by':e.create_by }; items.append(d); state = OPER_SUCCESS; except Exception,e: print e ; logger = logging.getLogger('watch_dog'); logger.error(e); state = OPER_EXCEPTION ;
def config_removebykey(key): session = Session() try: session.query(ServerConfig).filter( ServerConfig.cfg_key == key).delete() except Exception, e: print e
def add_collection(): jdata = request.get_json() uid = jdata['create_by'] pid = jdata['pack_id'] ctype = jdata['type'] #collection type content = jdata['content'] abstract = jdata['abstract'] cover = jdata['cover'] state = constants.OPER_INVALID result = {} if ctype == CTYPE_TEXTIMG: content = replace_url(content) try: session = Session() item = Collection(type=ctype,title=jdata['title'],cover=cover,content=content,abstract = abstract,\ pack_id = pid,cisn = jdata['cisn'],create_by=uid) session.query(CollectPack).filter(CollectPack.id == pid).update( {CollectPack.collect_count: CollectPack.collect_count + 1}) session.add(item) session.commit() result['cid'] = item.id state = constants.OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog.'.join(__name__)) logger.error(e) state = constants.OPER_EXCEPTION
def update_collection(): jdata = request.get_json() cid = jdata['cid'] fields = {} if 'title' in jdata: fields['title'] = jdata['title'] if 'content' in jdata: content = jdata['content'] content = replace_url(content) fields['content'] = content if 'type' in jdata: fields['type'] = jdata['type'] if 'vote_count' in jdata: fields['vote_count'] = jdata['vote_count'] if 'bro_count' in jdata: fields['bro_count'] = jdata['bro_count'] if 'share_count' in jdata: fields['share_count'] = jdata['share_count'] if 'visit_count' in jdata: fields['visit_count'] = jdata['visit_count'] if 'focus_count' in jdata: fields['focus_count'] = jdata['focus_count'] if 'cover' in jdata: fields['cover'] = jdata['cover'] state = OPER_INVALID try: session = Session() session.query(Collection).filter(Collection.id == cid).update(fields) session.commit() state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def config_removebytype(ctype): session = Session() try: session.query(ServerConfig).filter( ServerConfig.cfg_type == ctype).delete() except Exception, e: print e
def del_collectpack(): jdata = request.get_json() state = constants.OPER_INVALID session = Session() print jdata['pid'] try: ''' sql = " select * from imagelib where collection_id in " ; sql += " (select id from collection where pack_id = %s ) " % jdata['pid']; imgs = session.query(Imagelib.id,Imagelib.type,Imagelib.image_name,Imagelib.image_url).\ from_statement(sql).all(); remove_files(imgs); session.execute('delete from imagelib where collection_id in(select id from collection where pack_id = :id )', {'id':jdata['pid']}); session.query(Collection).filter(Collection.pack_id == jdata["pid"]).delete(); session.query(CollectPack).filter(CollectPack.id == jdata["pid"]).delete(); ''' #update state session.query(CollectPack).filter( CollectPack.id == jdata['pid']).update({CollectPack.state: 0}) session.commit() state = constants.OPER_SUCCESS except Exception, e: print e state = constants.OPER_EXCEPTION logger = logging.getLogger('watch_dog') logger.error(e)
def get_topiclist(): jdata = request.get_json() off = request.get_json().get("off") s = jdata['slice'] session = Session() items = [] state = OPER_INVALID try: relist = None if off is None: relist = session.query(Topic).filter( Topic.topic_name.like('%' + s + '%')).all() else: relist = session.query(Topic).filter(Topic.topic_name.like('%'+s+'%')).\ offset(int(off)).limit(10).all() for e in relist: d = { 'tid': e.id, 'topic_name': e.topic_name, 'topic_desc': e.topic_desc, 'topic_pic': NGINX_TOPICIMAGE_URL_BASE + e.pic_url } items.append(d) print items state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def do_userpic(): uid = request.form['uid']; fmt = uid+':'+request.form['email']; hstr = hashlib.sha1(fmt).hexdigest(); path = UPLOAD_USERPHOTO_ROOT_PATH+hstr+'.png'; if os.path.exists(path): os.remove(path); url = hstr+'.png'; state = OPER_INVALID; session = Session(); try: session.query(User).filter(User.id == uid).update({User.photo_url:url}); session.commit(); f = request.files.get('user_pic'); data = f.read(); output = open(path ,'wb'); output.write(data); output.close(); #make user photo thumbnail params = [ {'size':(100,100),'dst_path':UPLOAD_USERPHOTO_BIG_PATH}, {'size':(40,40),'dst_path':UPLOAD_USERPHOTO_MEDIUM_PATH} ] make_thumbnail(path,params); state = UPLOAD_SUCCESS ; except Exception , e: print e ; logger = logging.getLogger('watch_dog'); logger.error(e); state = UPLOAD_FAILD;
def get_packinfo(): jdata = request.get_json(); uid = jdata['uid']; pack_id = jdata['pack_id']; state = OPER_INVALID; session = Session(); result = {}; try: sql = " select a.id,a.pack_name,a.type,a.pack_desc,a.focus_count,a.create_by,a.topic_id,a.is_private,"; sql += " u.name,u.sex,u.photo_url,u.signature," if uid is None: #not login actually . sql += " (select false from dual ) is_focus " ; else: sql += " (select b.id from userfocus b where a.id = b.obj_id and b.focus_type ='003' and b.user_id = %s limit 1 ) is_focus" % (uid); sql += " from collect_pack a ,user u where a.create_by = u.id and a.id = %s " % (pack_id); entry = session.query(CollectPack.id,CollectPack.pack_name,CollectPack.type,CollectPack.pack_desc,CollectPack.focus_count, CollectPack.create_by,CollectPack.topic_id,CollectPack.is_private, User.name,User.photo_url,User.sex,User.signature,'is_focus',).\ from_statement(sql).first(); isFocus = True; if entry.is_focus is None or bool(entry.is_focus) is False: isFocus = False ; result = { 'id' : entry.id, 'pack_name':entry.pack_name, 'pack_type':entry.type, 'pack_desc':entry.pack_desc, 'focus_count':entry.focus_count, 'create_by':entry.create_by, 'is_focus': isFocus, 'topic_id':entry.topic_id, 'is_private':bool(entry.is_private), 'name':entry.name, 'sex':entry.sex, 'signature':entry.signature }; if entry.topic_id is not None and entry.topic_id != '': tids = entry.topic_id.split(','); names = ''; item = session.query(Topic.topic_name).filter(Topic.id.in_(tuple(tids))).all(); for e in item : names+=e.topic_name ; names+=','; result['topic_names'] = names; else : result['topic_names'] = ''; if entry.photo_url is not None : result['photo'] = NGINX_USERPHOTO_BIG_URL_BASE+entry.photo_url else: result['photo'] = ""; print result ; state = OPER_SUCCESS ; except Exception,e: print e ; logger = logging.getLogger('watch_dog'); logger.error(e); state= OPER_EXCEPTION ;
def user_logout(): jdata = request.get_json() uid = jdata['uid'] state = OPER_INVALID try: session = Session() session.query(User).filter(User.id == uid).update( {User.login_state: False}) session.commit() state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def get_userlist(): jdata = request.get_json() s = jdata['slice'] off = jdata['off'] session = Session() items = [] state = OPER_INVALID try: relist = session.query(User).filter(User.name.like('%'+s+'%')).\ offset(int(off)).limit(10).all() for e in relist: d = { 'id': e.id, 'name': e.name, 'sex': e.sex, 'signature': e.signature } if e.photo_url is not None: d['photo'] = NGINX_USERPHOTO_MEDIUM_URL_BASE + e.photo_url else: d['photo'] = "" items.append(d) state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def getpacklist(): jdata = request.get_json(); off= jdata['off']; items = [] ; try: session = Session(); relist = session.query(Collection,User).filter(Collection.create_by == User.id, Collection.pack_id == jdata['pack_id'], Collection.state == '1').\ order_by(Collection.last_modify).offset(off).limit(PAGE_SIZE).all(); for c in relist : d = { 'cid':c[0].id, 'cisn':c[0].cisn, 'title':c[0].title, 'abstract':c[0].abstract, 'create_by':c[0].create_by, 'vote_count':c[0].vote_count, 'name':c[1].name, 'sex':c[1].sex, 'signature':c[1].signature, 'comment_count':c[0].comment_count }; if c[1].photo_url is not None : d['photo'] = NGINX_USERPHOTO_BIG_URL_BASE+c[1].photo_url else: d['photo'] = ""; if c[0].cover is not None : d['cover'] = NGINX_IMAGE_SERVER_URI+c[0].cover items.append(d); except Exception,e: print e ; logger = logging.getLogger('watch_dog'); logger.error(e);
def del_comment(): jdata = request.get_json(); state = constants.OPER_INVALID; session = Session(); try: session.query(Comment).filter(Comment.id == jdata['comment_id']).delete(); session.commit(); session.query(Collection).filter(Collection.id == jdata['cid']).update({ Collection.comment_count: Collection.comment_count - 1}); session.commit(); state = constants.OPER_SUCCESS; except Exception,e: print e ; logger = logging.getLogger('watch_dog'); logger.error(e); state = constants.OPER_EXCEPTION;
def update_message(): jdata = request.get_json() sender_id = jdata['sender_id'] receiver_id = jdata['receiver_id'] state = OPER_INVALID try: session = Session() session.query(Message).filter(Message.sender_id == sender_id,\ Message.receiver_id == receiver_id).update({Message.is_read:True}) session.commit() state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def get_boxdetail(): jdata = request.get_json() uid = jdata['uid'] off = jdata['off'] session = Session() items = [] state = OPER_INVALID try: sql = " select a.id,a.type,a.box_name,a.box_desc ,a.focus_count, a.box_count," sql += " (select group_concat(b.title) from collection b ,favorite c where c.collection_id = b.id and c.box_id = a.id limit 3) titles" sql += " from favorite_box a where a.create_by = %s " % uid sql += " order by a.create_time desc limit %s,%s" % (int(off), PAGE_SIZE) relist = session.query(FavoriteBox.id, FavoriteBox.type, FavoriteBox.box_name, FavoriteBox.box_desc, FavoriteBox.box_count, FavoriteBox.focus_count, 'titles').from_statement(sql).all() for e in relist: d = { 'box_id': e.id, 'box_type': e.type, 'box_name': e.box_name, 'box_desc': e.box_desc, 'box_count': e.box_count, 'focus_count': e.focus_count, 'titles': e.titles } items.append(d) print items state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def del_favoritebox(): jdata = request.get_json() uid = jdata['uid'] box_id = jdata['box_id'] state = OPER_INVALID session = Session() try: session.query(Favorite).filter(Favorite.box_id==box_id,Favorite.create_by==uid)\ .delete(synchronize_session=False) session.query(FavoriteBox).filter(FavoriteBox.id==box_id,FavoriteBox.create_by==uid)\ .delete(synchronize_session=False) session.commit() state = OPER_SUCCESS except Exception, e: print e state = OPER_EXCEPTION
def get_actionlist(): jdata = request.get_json() uid = jdata['uid'] off = jdata['off'] state = OPER_INVALID items = [] try: sql = " select a.action_type,b.id,b.title,b.content,b.type,b.abstract,c.name " sql += " from user_action a, collection b ,user c " sql += " where a.obj_id = b.id and a.uid = c.id and " sql += " a.action_type in ('1000','1100','1200') and b.create_by = %s " % uid sql += " order by a.create_time desc limit %s,%s" % (int(off), PAGE_SIZE) session = Session() relist = session.query(UserAction.action_type, Collection.id, Collection.type, Collection.content, Collection.abstract, User.name).from_statement(sql).all() for e in relist: d = { 'cid': e.id, 'type': e.type, 'name': e.name, 'action_type': e.action_type, 'abstract': e.abstract } items.append(d) state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def get_boxes(): jdata = request.get_json() uid = jdata['uid'] session = Session() state = OPER_INVALID items = [] try: relist = session.query(FavoriteBox).filter( FavoriteBox.create_by == uid).all() state = OPER_SUCCESS for e in relist: d = { 'box_id': e.id, 'box_type': e.type, 'box_type': e.type, 'box_name': e.box_name, 'box_desc': e.box_desc, 'focus_count': e.focus_count, 'is_private': e.is_private, 'create_by': e.create_by } items.append(d) except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def get_topic(): jdata = request.get_json() uid = jdata['uid'] obj_id = jdata['obj_id'] state = OPER_INVALID info = {} session = Session() try: sql = " select a.id,a.topic_name,a.topic_desc,a.focus_count ," if uid is None: sql += " ( select false from dual ) is_focus " else: sql += " (select b.id from userfocus b where a.id = b.obj_id and b.focus_type ='002' and b.user_id = %s limit 1 ) is_focus " % ( uid) sql += " from topic a where a.id = %s " % (obj_id) entry = session.query(Topic.id, Topic.topic_name, Topic.topic_desc, Topic.focus_count, 'is_focus').from_statement(sql).one() flag = True if entry.is_focus is None or bool(entry.is_focus) is False: flag = False info = { 'topic_id': entry.id, 'topic_name': entry.topic_name, 'topic_desc': entry.topic_desc, 'focus_count': entry.focus_count, 'is_focus': flag } state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def del_favorite(): jdata = request.get_json() cid = jdata['cid'] uid = jdata['uid'] box_ids = jdata.get('box_ids') ids = tuple(box_ids.split(",")) state = OPER_INVALID session = Session() hasCollected = True try: session.query(Favorite).filter(Favorite.box_id.in_(ids),Favorite.collection_id==cid,\ Favorite.create_by==uid).delete(synchronize_session=False) session.query(FavoriteBox).filter(FavoriteBox.id.in_(ids),FavoriteBox.create_by == uid).\ update({FavoriteBox.box_count: FavoriteBox.box_count -1},synchronize_session=False) relist = session.query(Favorite).filter(Favorite.create_by==uid,\ Favorite.collection_id == cid).first() if relist is None: session.query(UserAction).filter(UserAction.uid == uid,UserAction.obj_id == cid ,\ UserAction.action_type == ACTION_COLLECT_COLLECTION).delete() hasCollected = False session.commit() state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def clear_favoritebox(): jdata = request.get_json() uid = jdata['uid'] box_id = jdata['box_id'] state = OPER_INVALID session = Session() try: session.query(Favorite).filter(Favorite.box_id==box_id,Favorite.create_by==uid)\ .delete(synchronize_session=False) session.query(FavoriteBox).filter(FavoriteBox.id == box_id,Favorite.create_by==uid)\ .update({FavoriteBox.box_count:0}) session.commit() state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def do_active(): uid = request.args.get('id') acti_code = request.args.get('acti_code') session = Session() try: entry = session.query(User).get(uid) except: result = "server exception." if entry.acti_code == acti_code: try: session.query(User).filter(User.id == uid).update( {User.reg_state: 1}) session.commit() result = "账号已经激活,欢迎使用跬步。" except Exception, e: logger = logging.getLogger('watch_dog') logger.error(e) result = "server exception." finally:
def update_collectpack(): jdata = request.get_json() print jdata['pack_id'] fields = { 'pack_name': jdata['pack_name'], 'pack_desc': jdata['pack_desc'], 'is_private': bool(jdata['is_private']), 'topic_id': jdata['topic_id'] } state = OPER_INVALID session = Session() try: session.query(CollectPack).filter( CollectPack.id == jdata['pack_id']).update(fields) session.commit() state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def verify_auth_token(token): s = Serializer(app.secret_key) try: data = s.loads(token) except SignatureExpired: return None except BadSignature: return None db_session = Session() user = db_session.query(User).filter(User.id == data['id']) db_session.close() return user
def user_follow(): jdata = request.get_json() uid = jdata['user_id'] obj_id = jdata['obj_id'] target = jdata['target'] focus_type = '' state = OPER_INVALID try: session = Session() if target == 'COLLECTOR': focus_type = '001' session.query(User).filter(User.id == obj_id).update({\ User.focus_count: User.focus_count + 1}) if target == 'COLLECTION': focus_type = '002' session.query(Collection).filter(Collection.id == obj_id).update({\ Collection.focus_count: Collection.focus_count + 1}) if target == 'TOPIC': focus_type = '003' session.query(Topic).filter(Topic.id == obj_id).update({\ Topic.focus_count: Topic.focus_count + 1}) item = Userfocus(user_id=uid, focus_type=focus_type, obj_id=obj_id) session.add(item) session.commit() state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION