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
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
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))
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
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))
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 {}
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
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
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
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
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
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
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
def getList(parent=None): cond = dict(parent=parent) cond.update(StatusCond) lst = list(Tb().find(cond,{'status':0})) return mongo_conv(lst)
def end(self, rs): self.write(dict(data=mongo_conv(list(rs)))) self.finish()
def get(self): coll = get_context().get_mongoclient(name='migrant')['account'] rs = coll.find() self.write(dict(data=mongo_conv(list(rs)))) self.finish()
def end(self,rs): self.write(dict(data=mongo_conv(list(rs)))) self.finish()
def getList(parent=None): cond = dict(parent=parent) cond.update(StatusCond) lst = list(Tb().find(cond, {'id': 0, 'status': 0})) return mongo_conv(lst)
def on_end(self,lst): lst = mongo_conv(lst) self.render("admin/resourceitem.html",data =lst,city=self.city)
def on_end(self, lst): lst = mongo_conv(lst) self.render("admin/resourceitem.html", data=lst, city=self.city)