Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
def del_profession(pid):
    try:
        session = Session()
        session.query(Profession).filter(Profession.id == pid).delete()
        session.commit()
    except Exception, e:
        print e
Exemplo n.º 6
0
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;         
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
def del_follows():
    jdata = request.get_json()
    follower_id = jdata['follower_id']
    obj_id = jdata['obj_id']
    focus_type = jdata['type']
    state = OPER_INVALID
    session = Session()
    try:
        session.query(Userfocus).filter(Userfocus.user_id == follower_id and \
            Userfocus.focus_type == focus_type and Userfocus.obj_id == obj_id ).delete()

        if focus_type == FOLLOW_TYPE_TOPIC:
            session.query(Topic).filter(Topic.id == obj_id).update(
                {Topic.focus_count: Topic.focus_count - 1})
        elif focus_type == FOLLOW_TYPE_COLLECTPACK:
            session.query(CollectPack).filter(CollectPack.id == obj_id).update(
                {CollectPack.focus_count: CollectPack.focus_count - 1})
        elif focus_type == FOLLOW_TYPE_COLLECTOR:
            pass
        state = OPER_SUCCESS
        session.commit()
    except Exception, e:
        print e
        logger = logging.getLogger('watch_dog')
        logger.error(e)
        state = OPER_EXCEPTION
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
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
Exemplo n.º 12
0
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
Exemplo n.º 13
0
def del_topic(did):
    try:
        session= Session();
        session.query(Topic).filter(Topic.id==did).delete();
        session.commit();
    except Exception,e:
        print e; 
Exemplo n.º 14
0
def add_profession(name, desc):
    profession = Profession(profession=name, description=desc)
    try:
        session = Session()
        session.add(profession)
        session.commit()
    except Exception, e:
        print e
Exemplo n.º 15
0
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
Exemplo n.º 16
0
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:
Exemplo n.º 17
0
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;
Exemplo n.º 18
0
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
Exemplo n.º 19
0
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();
Exemplo n.º 20
0
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
Exemplo n.º 21
0
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;
Exemplo n.º 22
0
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
Exemplo n.º 23
0
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
Exemplo n.º 24
0
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
Exemplo n.º 25
0
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;
Exemplo n.º 26
0
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:
Exemplo n.º 27
0
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
Exemplo n.º 28
0
def del_imgs():
    cid = request.get_json().get('cid')
    ids = request.get_json().get('ids')
    state = OPER_INVALID
    ids = tuple(ids.split(","))
    if ids is not None and ids != '':
        session = Session()
        try:
            #delete image file first
            imgs = session.query(Imagelib).filter(Imagelib.id.in_(ids)).all()
            hasthumbnail = remove_files(imgs)

            print '#################', ids
            #delete imagelib record
            session.query(Imagelib).filter(
                Imagelib.id.in_(ids)).delete(synchronize_session=False)

            if not hasthumbnail:
                relist = session.query(Imagelib).filter(
                    Imagelib.collection_id == cid).all()
                if len(relist) > 0:
                    session.query(Imagelib).filter(Imagelib.id == relist[0].id) \
                        .update({Imagelib.type:'THUMBNAIL'})
                    src_path = UPLOAD_IAMGE_ROOT_PATH + relist[0].image_url
                    params = [
                        {
                            'size': (200, 150),
                            'dst_path': UPLOAD_IMAGE_THUMBNAIL_PATH
                        },
                    ]
                    make_thumbnail(src_path, params)
            session.commit()

            state = OPER_SUCCESS
        except Exception, e:
            print e
            logger = logging.getLogger('watch_dog')
            logger.error(e)
            state = OPER_EXCEPTION
        finally:
Exemplo n.º 29
0
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
Exemplo n.º 30
0
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