Exemple #1
0
def m_list(table, fields=None, sorts = None, **kwargs):
    """
        列表查询
        fields 指定需要输出的字段 like {'name':1}
    """
    dbname = None
    if 'dbname' in kwargs:
        dbname=kwargs.pop('dbname')

    #if not sorts:
    #    sorts = [('_id', 1)]

    page_index = int(kwargs.pop('page_index', 1))
    page_size = int(kwargs.pop('page_size', 10))
    findall = kwargs.pop('findall', None)

    tb = Tb(table, dbname = dbname)
    count = tb.find(kwargs).count()
    if count and findall in [1, '1', True]:
        page_index = 1
        page_size = count

    page_num = (count + page_size - 1)/ page_size
    page = dict(page_index = page_index, page_size = page_size, page_num = page_num,allcount=count)

    if sorts:
        ret = mongo_conv(list(tb.find(kwargs, fields).sort(sorts).skip((page_index - 1) * page_size).limit(page_size)))
    else:
        ret = mongo_conv(list(tb.find(kwargs, fields).skip((page_index - 1) * page_size).limit(page_size)))


    return ret, page
Exemple #2
0
def m_list(table, fields=None, sorts = None, **kwargs):
    """
        列表查询
        fields 指定需要输出的字段 like {'name':1}
    """
    dbname = None
    if 'dbname' in kwargs:
        dbname=kwargs.pop('dbname')

    #if not sorts:
    #    sorts = [('_id', 1)]

    page_index = int(kwargs.pop('page_index', 1))
    page_size = int(kwargs.pop('page_size', 10))
    findall = kwargs.pop('findall', None)

    tb = Tb(table, dbname = dbname)
    count = tb.find(kwargs).count()
    if count and findall in [1, '1', True]:
        page_index = 1
        page_size = count

    page_num = (count + page_size - 1)/ page_size
    page = dict(page_index = page_index, page_size = page_size, page_num = page_num,allcount=count)

    if sorts:
        ret = mongo_conv(list(tb.find(kwargs, fields).sort(sorts).skip((page_index - 1) * page_size).limit(page_size)))
    else:
        ret = mongo_conv(list(tb.find(kwargs, fields).skip((page_index - 1) * page_size).limit(page_size)))


    return ret, page
Exemple #3
0
def m_page(table,since=None,size=10,**kwargs):
    """
        通用数据集查询方案,一次性获取size条大于since 及大于addon的数据记录
    """
    try:
        cond = {}
        cond.update(StatusCond)
        cond_id = None
        if since:
            cond_id = ObjectId(since)
            cond.update(_id = {'$lt':cond_id})
        cond.update(kwargs)
        
        if cond.get('addon',None):
            #查询addon 时间以后的记录 
            t = float(cond.pop('addon'))
            t = hex(int(t))[2:]
            _id = '{0}{1}'.format(t,'0'*16)
            if cond_id and cond_id >ObjectId(_id):
                cond.update({'_id':{'$lt':ObjectId(_id)}})
        elif 'addon' in cond:
            cond.pop('addon')
        
        print cond
        lst = list(Tb(table).find(cond,{'status':0}).limit(size).sort('_id',-1))
        for item in lst:
            if 'addon' not in item:item['addon'] = item['_id'].generation_time.strftime('%Y:%m:%d %H:%M:%S')
        
        return True, mongo_conv(lst)
    except Exception as e:
        return False,e.message
Exemple #4
0
def m_page(table,since=None,size=10,**kwargs):
    """
        通用数据集查询方案,一次性获取size条大于since 及大于addon的数据记录
    """
    try:
        cond = {}
        cond.update(StatusCond)
        cond_id = None
        if since:
            cond_id = ObjectId(since)
            cond.update(_id = {'$lt':cond_id})
        cond.update(kwargs)
        
        if cond.get('addon',None):
            #查询addon 时间以后的记录 
            t = float(cond.pop('addon'))
            t = hex(int(t))[2:]
            _id = '{0}{1}'.format(t,'0'*16)
            if cond_id and cond_id >ObjectId(_id):
                cond.update({'_id':{'$lt':ObjectId(_id)}})
        elif 'addon' in cond:
            cond.pop('addon')
        
        print cond
        lst = list(Tb(table).find(cond,{'status':0}).limit(size).sort('_id',-1))
        for item in lst:
            if 'addon' not in item:item['addon'] = item['_id'].generation_time.strftime('%Y:%m:%d %H:%M:%S')
        
        return True, mongo_conv(lst)
    except Exception as e:
        return False,e.message
Exemple #5
0
def suggest(category, top=10, key=None):
    """
        按热度排序,返回category 分类前top 条记录
    """
    cond = dict(category=category, status={'$ne': -1})
    if key:
        cond.update(name={'$regex': key})

    lst = Tb().find(cond).limit(top).sort('usage', -1)
    return mongo_conv(list(lst))
Exemple #6
0
def m_info(table,_id):
    not_empty(_id)
    try:
        cond = dict(_id=ObjectId(_id))
        cond.update(StatusCond)
        return True,mongo_conv(Tb(table).find_one(cond,{'status':0}))
    except InvalidId as e:
        return False,"查询参数格式错误"
    except Exception as e:
        return False,e.message
Exemple #7
0
def suggest(category, top=10, key=None):
    """
        按热度排序,返回category 分类前top 条记录
    """
    cond = dict(category=category,status={'$ne':-1})
    if key:
        cond.update(name = {'$regex':key})

    lst = Tb().find(cond).limit(top).sort('usage',-1)
    return mongo_conv(list(lst))
Exemple #8
0
def m_find_one(table, fields=None, **kwargs):
    """
        查询单条记录
        fields 指定需要输出的字段 like {'name':1}
    """
    dbname = None
    if 'dbname' in kwargs:
        dbname=kwargs.pop('dbname')

    return mongo_conv(Tb(table, dbname=dbname).find_one(kwargs, fields)) or {}
Exemple #9
0
def m_info(table,_id):
    not_empty(_id)
    try:
        cond = dict(_id=ObjectId(_id))
        cond.update(StatusCond)
        return True,mongo_conv(Tb(table).find_one(cond,{'status':0}))
    except InvalidId as e:
        return False,"查询参数格式错误"
    except Exception as e:
        return False,e.message
Exemple #10
0
def m_find_one(table, fields=None, **kwargs):
    """
        查询单条记录
        fields 指定需要输出的字段 like {'name':1}
    """
    dbname = None
    if 'dbname' in kwargs:
        dbname=kwargs.pop('dbname')

    return mongo_conv(Tb(table, dbname=dbname).find_one(kwargs, fields)) or {}
Exemple #11
0
def auth_login(site,otherid,name,**kwargs):
    try:
        not_empty(site,otherid,name)
        r = m_exists(TName,site=site,otherid=otherid,name=name)
        if r:
            r = mongo_conv(r)
            return True,r
        else:
            val = dict(site=site,otherid=otherid,name=name)
            _id = Tb().insert(val,saft=True)
            val['_id'] = str(_id)
            return True,val
    except Exception as e:
        return False,e.message
Exemple #12
0
 def auth_login(self, site,otherid,name,**kwargs):
     try:
         not_empty(site,otherid,name)
         r = self.exists(site=site,otherid=otherid,name=name)
         if r:
             r = mongo_conv(r)
             return True,r
         else:
             val = dict(site=site,otherid=otherid,name=name)
             _id = self.insert(val)
             val['_id'] = str(_id)
             return True,val
     except Exception as e:
         return False,e.message
Exemple #13
0
def login(username,password,isadmin = None):
    try:
        not_empty(username,password)
        cond = dict(username=username,password=password)
        if isadmin:
            cond.update(isadmin = isadmin)

        r = m_exists(TName,**cond)
        if r:
            r = mongo_conv(r)
            return True, r
        else:
            return False,None
    except Exception as e:
        return False,e.message
Exemple #14
0
    def login(self, username, password, isadmin=None):
        try:
            not_empty(username, password)
            cond = dict(username=username, password=hashPassword(password))
            #cond = dict(username=username, password=password)
            if isadmin:
                cond.update(isadmin=isadmin)

            r = self.exists(**cond)
            if r:
                r = mongo_conv(r)
                return True, r
            else:
                return False, 'NO_EXISTED'
        except ValueError:
            return False, 'NO_EMPTY'
        except Exception as e:
            return False, e.message
Exemple #15
0
    def login(self, username, password, isadmin=None):
        try:
            not_empty(username, password)
            cond = dict(username=username, password=hashPassword(password))
            #cond = dict(username=username, password=password)
            if isadmin:
                cond.update(isadmin=isadmin)

            r = self.exists(**cond)
            if r:
                r = mongo_conv(r)
                return True, r
            else:
                return False, 'NO_EXISTED'
        except ValueError:
            return False, 'NO_EMPTY'
        except Exception as e:
            return False, e.message
Exemple #16
0
def login(username, password, isadmin=None):
    try:
        not_empty(username, password)
        cond = dict(username=username, password=hashPassword(password))
        if isadmin:
            cond.update(isadmin=isadmin)

        r = m_exists(TName, **cond)
        if r:
            r = mongo_conv(r)
            if r['status'] == INIT:
                return False, 'UNACTIVATED'
            return True, r
        else:
            return False, 'NO_EXISTED'
    except ValueError:
        return False, 'NO_EMPTY'
    except Exception as e:
        return False, e.message
Exemple #17
0
def m_page(table,since=None,size=10,sort=[('_id',-1),],**kwargs):
    """
        通用数据集查询方案,一次性获取size条大于since 及大于addon的数据记录
    """
    try:
        cond = {}
        cond.update(StatusCond)
        cond_id = None
        if since:
            cond_id = ObjectId(since)
            cond.update(_id = {'$lt':cond_id})
        cond.update(kwargs)
        
        print cond
        lst = list(Tb(table).find(cond,{'status':0}).limit(size).sort(sort))
        for item in lst:
            if 'addon' not in item:item['addon'] = item['_id'].generation_time
        
        return True, mongo_conv(lst)
    except Exception as e:
        return False,e.message
Exemple #18
0
def m_page(table, since=None, size=10, sort=[
    ('_id', -1),
], **kwargs):
    """
        通用数据集查询方案,一次性获取size条大于since 及大于addon的数据记录
    """
    try:
        cond = {}
        cond.update(StatusCond)
        cond_id = None
        if since:
            cond_id = ObjectId(since)
            cond.update(_id={'$lt': cond_id})
        cond.update(kwargs)

        print cond
        lst = list(Tb(table).find(cond, {'status': 0}).limit(size).sort(sort))
        for item in lst:
            if 'addon' not in item: item['addon'] = item['_id'].generation_time

        return True, mongo_conv(lst)
    except Exception as e:
        return False, e.message
Exemple #19
0
def getList(parent=None):
    cond = dict(parent=parent)
    cond.update(StatusCond)
    lst = list(Tb().find(cond,{'status':0}))
    return mongo_conv(lst)
Exemple #20
0
 def end(self, rs):
     self.write(dict(data=mongo_conv(list(rs))))
     self.finish()
Exemple #21
0
    def get(self):
        coll = get_context().get_mongoclient(name='migrant')['account']
        rs = coll.find()

        self.write(dict(data=mongo_conv(list(rs))))
        self.finish()
Exemple #22
0
 def get(self):
     coll = get_context().get_mongoclient(name='migrant')['account']
     rs = coll.find()
     
     self.write(dict(data=mongo_conv(list(rs))))
     self.finish()
Exemple #23
0
 def end(self,rs):
     self.write(dict(data=mongo_conv(list(rs))))
     self.finish()
Exemple #24
0
def getList(parent=None):
    cond = dict(parent=parent)
    cond.update(StatusCond)
    lst = list(Tb().find(cond, {'id': 0, 'status': 0}))
    return mongo_conv(lst)
Exemple #25
0
 def on_end(self,lst):
     lst = mongo_conv(lst)
     self.render("admin/resourceitem.html",data =lst,city=self.city)
Exemple #26
0
 def on_end(self, lst):
     lst = mongo_conv(lst)
     self.render("admin/resourceitem.html", data=lst, city=self.city)