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
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 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 add_follows(): jdata = request.get_json() state = constants.OPER_INVALID ftype = jdata['type'] obj_id = jdata['obj_id'] try: session = Session() item = Userfocus(user_id=jdata['follower_id'], focus_type=ftype, obj_id=obj_id) session.add(item) if ftype == FOLLOW_TYPE_TOPIC: session.query(Topic).filter(Topic.id == obj_id).update( {Topic.focus_count: Topic.focus_count + 1}) pass elif ftype == FOLLOW_TYPE_COLLECTPACK: session.query(CollectPack).filter(CollectPack.id == obj_id).update( {CollectPack.focus_count: CollectPack.focus_count + 1}) elif ftype == FOLLOW_TYPE_COLLECTOR: session.query(User).filter(User.id == obj_id).update( {User.focus_count: User.focus_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 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 add_profession(name, desc): profession = Profession(profession=name, description=desc) try: session = Session() session.add(profession) session.commit() except Exception, e: print e
def config(ctype, key, name, value1, value2, value3, comment): session = Session() try: c = ServerConfig(cfg_type=ctype,cfg_key=key,cfg_name=name,field_a = value1,\ field_b=value2,field= value3,comment=comment) session.add(c) session.commit() except Exception, e: print e
def do_register(): ''' this method register an item of user info . To complete this operation need to do some things as follow: <1>parse user's info from json data <2>verify the primary info. <3>update database. <4>send email and wait active account. ''' reg_state = OPER_INVALID if (request.method == 'POST'): jdata = request.get_json() #verify the email email = jdata['email'].strip() uname = jdata['name'].strip() url = ACCOUNT_ACTIVE_URL active_code = uuid.uuid1() #base timestamp session = Session() uid = '' try: print uname, email items = session.query(User).filter( or_(User.name == uname, User.email == email)).all() print items if len(items) > 0: if items[0].name == uname: reg_state = REG_SAMENAME print "same name" if items[0].email == email: reg_state = REG_SAMEEMAIL print "same email" else: print "same nothing" user = User(name=uname,sex=jdata['sex'],dev_id=jdata['dev_id'],\ email=email,signature=jdata['signature'],acti_code=active_code) user.hash_password(jdata['pwd']) session.add(user) session.commit() uid = user.id reg_state = REG_SENDEMAIL except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) reg_state = OPER_EXCEPTION finally:
def add_advice(): jdata = request.get_json(); state = constants.OPER_INVALID; try: session = Session(); item = Advice( advice= jdata['advice'], contact = jdata['contact'], adviser_id = jdata['adviser_id']); session.add(item); session.commit(); state = constants.OPER_SUCCESS; except Exception,e: print e ; logger = logging.getLogger('watch_dog.'.join(__name__)); logger.error(e); state = constants.OPER_EXCEPTION;
def add_topic(name,desc,level,path,pic=None,fcount,owner=-1): imgdata = Null; if pic is not None and pic is '': fp =open(pic,'rb'); imgdata =fp.read(); fp.close(); topic =Topic(level_code=level,level_path=path,topic_name=name,topic_desc=desc,\ topic_pic=imgdata,focus_count=fcount,create_by=owner); try: session = Session(); session.add(topic); session.commit(); except Exception,e: print e; session.rollback();
def add_favoritebox(): jdata = request.get_json() state = constants.OPER_INVALID result = {} try: session = Session() item = FavoriteBox(type=jdata['box_type'], box_name=jdata['box_name'], box_desc=jdata['box_desc'], is_private=bool(jdata['is_private']), create_by=jdata['create_by']) session.add(item) session.commit() state = constants.OPER_SUCCESS result = {'box_id': item.id} except Exception, e: logger = logging.getLogger('watch_dog') logger.error(e) state = constants.OPER_EXCEPTION
def add_report(): jdata = request.get_json(); state = constants.OPER_INVALID; try: session = Session(); item = Report( obj_id = jdata['cid'],accuser_id= jdata['accuser_id'],defendant_id = jdata['defendant_id'], reason = jdata['reason']); session.add(item); action = UserAction(uid = jdata['accuser_id'],action_type = ACTION_REPORT_COLLECTION, obj_id = jdata['cid']); session.add(action); session.commit(); state = constants.OPER_SUCCESS; except Exception,e: print e ; logger = logging.getLogger('watch_dog'); logger.error(e); state = constants.OPER_EXCEPTION;
def add_collectpack(): jdata = request.get_json() state = constants.OPER_INVALID result = {} try: session = Session() item = CollectPack(pack_name=jdata['pack_name'], type=jdata['pack_type'], topic_id=jdata['topic_id'], is_private=bool(jdata['private']), csn=jdata['csn'], pack_desc=jdata['pack_desc'], create_by=jdata['create_by']) session.add(item) session.commit() result['pack_id'] = item.id state = constants.OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = constants.OPER_EXCEPTION
def add_message(): jdata = request.get_json() sender_id = jdata['sender_id'] receiver_id = jdata['receiver_id'] message = jdata['message'] session = Session() state = OPER_INVALID msg_id = '' create_time = '' try: item = Message(content=message, sender_id=sender_id, receiver_id=receiver_id) session.add(item) session.commit() msg_id = item.id create_time = item.create_time.strftime('%Y-%m-%d') state = OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = OPER_EXCEPTION
def add_useraction(): jdata = request.get_json() state = constants.OPER_INVALID uid = jdata['uid'] atype = jdata['action_type'] obj_id = jdata['obj_id'] try: session = Session() item = UserAction(uid=uid, action_type=atype, obj_id=obj_id) if atype == ACTION_VOTE_COLLECTION: session.query(Collection).filter(Collection.id == obj_id).update( {Collection.vote_count: Collection.vote_count + 1}) if atype == ACTION_OPPOSE_COLLECTION: session.query(Collection).filter(Collection.id == obj_id).update( {Collection.bro_count: Collection.bro_count + 1}) session.add(item) session.commit() state = constants.OPER_SUCCESS except Exception, e: print e logger = logging.getLogger('watch_dog') logger.error(e) state = constants.OPER_EXCEPTION
def do_upload(): infomap = {}; cid = request.form['cid']; cover_url = request.form['cover_url']; howmany = request.form['size']; try: ###thread task for i in range(0,int(howmany)): url = request.form['url_'+str(i)]; map_url = request.form['map_url_'+str(i)]; slst = map_url.split('/'); datestr = slst[-2]; store_folder= UPLOAD_IAMGE_ROOT_PATH+datestr; if not os.path.exists(store_folder): os.makedirs(store_folder); unique_name = slst[-1]; f = request.files.get('data_'+str(i)); data = f.read(); path = store_folder+'/'+unique_name ; output = open(path ,'wb'); output.write(data); output.close(); infomap[url]= datestr+'/'+unique_name; #### session = Session(); if cover_url.strip() != '': cover_name = request.form['cover_name']; f = request.files.get('cover_data'); data = f.read(); path = UPLOAD_IMAGE_COVER_PATH +cover_url; output = open(path ,'wb'); output.write(data); output.close(); params = [ {'size':(200,150),'dst_path':UPLOAD_IMAGE_THUMBNAIL_PATH}, ] make_thumbnail(path,params); cover = Imagelib(image_name=cover_name,image_url=cover_url, type = 'COVER',collection_id=cid); session.add(cover); ids = [] ; for key in infomap.keys(): url = infomap[key]; img = Imagelib(image_name=key,image_url=url,collection_id=cid); session.add(img); ids.append(img.id); if cover_url == '': session.query(Imagelib).filter(Imagelib.collection_id == cid, Imagelib.image_name == request.form['url_0']) \ .update({Imagelib.type:'THUMBNAIL'}); entry = session.query(Imagelib).filter(Imagelib.collection_id == cid, Imagelib.image_name == request.form['url_0']).one(); session.commit(); #thread task ### src_path = UPLOAD_IAMGE_ROOT_PATH+entry.image_url ; print "gen path:",src_path ; params = [ {'size':(200,150),'dst_path':UPLOAD_IMAGE_THUMBNAIL_PATH}, ] make_thumbnail(src_path,params); ### else : session.commit(); state =UPLOAD_SUCCESS; except Exception,e: logger = logging.getLogger('watch_dog'); logger.error(e); state =UPLOAD_FAILD;