def test_remove_by(self): model = Model('LightDB', 'light', 'unittest', self.define) print( model.remove_by( {'nestsii.fields.nestarray.date': datetime(2005, 1, 1, 0, 0)})) print(model.remove_by('57e0b76f1d41c81776ced8cc')) print(model.remove_by(ObjectId('57e0b76f1d41c81776ced8cc')))
def is_unique(handler, data, option): model = Model(domain=handler.domain, code=handler.code, table=option['table']) for key, val in option['condition'].items(): if isinstance(val, str) and val.startswith('$'): option['condition'][key] = jmespath.search(val.replace('$', ''), {'data': handler.params.data}) count = model.total(condition=option['condition']) return count <= 0
def test_distinct(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.distinct('valid')) print( model.distinct( 'valid', {'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}))
def is_exists(handler, data, option): model = Model(domain=handler.domain, code=handler.code, table=option['table']) for key, val in option['condition'].items(): if isinstance(val, str) and val.startswith('$'): condition_data = jmespath.search(val.replace('$', ''), {'data': handler.params.data}) if not isinstance(condition_data, list): condition_data = list(condition_data) option['condition'][key] = {'$in': condition_data} count = model.total(condition=option['condition']) return count > 0
def test_write_file_to_grid(self): in_file = 'test_model.py' out_file = in_file + '.temp' model = Model('LightDB') result = model.write_file_to_grid(in_file) result = model.read_file_from_grid(result['fileId'], out_file) self.assertEqual(in_file, result['name']) self.assertTrue(os.path.isfile(out_file)) os.remove(out_file)
def test_update_by(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.update_by({'valid': 5}, {'$inc': {'valid': 3}})) print( model.update_by('57e0b76f1d41c81776ced8ca', {'$inc': { 'valid': 30 }})) print( model.update_by(ObjectId('57e0b76f1d41c81776ced8ca'), {'$inc': { 'valid': 30 }}))
def test_increment(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.increment({'valid': 3}, {'$inc': {'valid': 2}})) print( model.increment('123457791d41c817b4a09b37', {'$inc': { 'valid': 57 }})) print( model.increment(ObjectId('678907791d41c817b4a09b37'), {'$inc': { 'valid': 68 }}))
def test_get_by(self): model = Model('LightDB', 'light', 'unittest', self.define) print( model.get_by( {'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)})) print( model.get_by( {'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}, 'valid')) print( model.get_by( {'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}, { 'valid': 1, 'schema': 1 }))
def test_write_stream_to_grid(self): in_file = 'test_model.py' out_file = in_file + '.temp' model = Model('LightDB') f = open(in_file, 'rb') result = model.write_stream_to_grid(in_file, f, 'text/x-python') f.close() result = model.read_stream_from_grid(result['fileId']) f = open(out_file, 'wb') f.write(result['fileStream']) f.close() self.assertEqual(in_file, result['name']) self.assertTrue(os.path.isfile(out_file)) os.remove(out_file)
def __init__(self, handler, table=None): define = {} if table: define = getattr(Structure.instance(), table)['items'] self.uid = handler.uid self.model = Model(domain=handler.domain, code=handler.code, table=table, define=define) self.condition = handler.params.condition or {} self.data = handler.params.data or {} self.id = handler.params.id self.select = handler.params.select or handler.params.field self.sort = handler.params.sort self.files = handler.params.files self.skip = handler.params.skip self.limit = handler.params.limit or 0 if self.skip is None: self.skip = 0
def test_add(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.add( {'schema': 'a', 'nestsii': [{'fields': [{'nestarray': [{'date': '2003/01/01'}, {'date': '2004/01/01'}]}]}], 'valid': '1'} )) print(model.add( {'schema': 'b', 'nestsii': [{'fields': [{'nestarray': [{'date': '2005/01/01'}, {'date': '2006/01/01'}]}]}], 'valid': '2'} )) print(model.add( {'schema': 'c', 'nestsii': [{'fields': [{'nestarray': [{'date': '2007/01/01'}, {'date': '2008/01/01'}]}]}], 'valid': '3'} )) print(model.add( {'schema': 'd', 'nestsii': [{'fields': [{'nestarray': [{'date': '2009/01/01'}, {'date': '2010/01/01'}]}]}], 'valid': '4'} )) print(model.add([ {'schema': 'e', 'nestsii': [{'fields': [{'nestarray': [{'date': '2003/01/01'}, {'date': '2004/01/01'}]}]}], 'valid': '5'}, {'schema': 'e', 'nestsii': [{'fields': [{'nestarray': [{'date': '2003/01/01'}, {'date': '2004/01/01'}]}]}], 'valid': '5'} ]))
def test_get(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.get({'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)})) print(model.get('57e0b76f1d41c81776ced8ca')) print(model.get(ObjectId('57e0b76f1d41c81776ced8ca'))) print(model.get({'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}, 'valid')) print(model.get({'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}, {'valid': 1, 'schema': 1}))
def test_get(self): model = Model('LightDB', 'light', 'unittest', self.define) print( model.get( {'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)})) print(model.get('57e0b76f1d41c81776ced8ca')) print(model.get(ObjectId('57e0b76f1d41c81776ced8ca'))) print( model.get( {'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}, 'valid')) print( model.get( {'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}, { 'valid': 1, 'schema': 1 }))
def __init__(self, handler, table=None): define = {} if table: define = getattr(Structure.instance(), table) self.model = Model(domain=handler.domain, code=handler.code or CONST.DEFAULT_TENANT, table=table, option={'define': define}) self.uid = handler.uid self.condition = handler.params.condition or {} self.data = handler.params.data or {} self.id = handler.params.id self.select = handler.params.select or handler.params.field self.sort = handler.params.sort self.files = handler.params.files self.skip = handler.params.skip self.limit = handler.params.limit or 0 if self.skip is None: self.skip = 0
def test_get_by(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.get_by({'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)})) print(model.get_by({'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}, 'valid')) print(model.get_by({'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}, {'valid': 1, 'schema': 1}))
def init(self): valid = {'valid': 1} # configuration select = 'type, key, value, valueType' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_CONFIG) self.cache.set( CONST.SYSTEM_DB_CONFIG, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # validator select = 'group,name,rule,key,option,message,sanitize,class,action,condition' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_VALIDATOR) self.cache.set( CONST.SYSTEM_DB_VALIDATOR, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # i18n select = 'type,lang,key' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_I18N) self.cache.set( CONST.SYSTEM_DB_I18N, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # structure select = 'public,lock,type,kind,tenant,version,schema,items,extend,tenant' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_STRUCTURE) self.cache.set( CONST.SYSTEM_DB_STRUCTURE, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # board select = 'schema,api,type,kind,path,class,action,filters,selects,sorts,reserved,script' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_BOARD) self.cache.set( CONST.SYSTEM_DB_BOARD, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # route select = 'template,url,class,action' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_ROUTE) self.cache.set( CONST.SYSTEM_DB_ROUTE, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # tenant select = 'code,name' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_TENANT) self.cache.set( CONST.SYSTEM_DB_TENANT, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) return model.db
class Controller(object): """ 1. 封装model的调用, 接收的参数为handler对象 2. 进行缺省值的设定, 如 updateBy createAt valid 等 3. 格式化输出的JSON结果, 获取List时会付上件数totalItems等信息 4. 统一封装关于数据库操作的错误内容 """ def __init__(self, handler, table=None): define = {} if table: define = getattr(Structure.instance(), table)['items'] self.uid = handler.uid self.model = Model(domain=handler.domain, code=handler.code, table=table, define=define) self.condition = handler.params.condition or {} self.data = handler.params.data or {} self.id = handler.params.id self.select = handler.params.select or handler.params.field self.sort = handler.params.sort self.files = handler.params.files self.skip = handler.params.skip self.limit = handler.params.limit or 0 if self.skip is None: self.skip = 0 def get(self): if 'valid' not in self.condition: self.condition['valid'] = CONST.VALID result = self.model.get(condition=self.id or self.condition, select=self.select) return result, None def list(self): if 'valid' not in self.condition: self.condition['valid'] = CONST.VALID count = self.model.total(condition=self.condition) result = self.model.get_by(condition=self.condition, select=self.select, sort=self.sort, skip=self.skip, limit=self.limit) return {'totalItems': count, 'items': result}, None def add(self): if not isinstance(self.data, list): self.data = [self.data] for data in self.data: regular = { 'createAt': datetime.now(), 'createBy': self.uid, 'updateAt': datetime.now(), 'updateBy': self.uid, 'valid': CONST.VALID } data.update(regular) result = self.model.add(data=self.data) return {'_id': result}, None def total(self): if 'valid' not in self.condition: self.condition['valid'] = CONST.VALID count = self.model.total(condition=self.condition) return count, None def count(self): return self.total() def update(self, upsert=False): if 'valid' not in self.condition: self.condition['valid'] = CONST.VALID regular = {'updateAt': datetime.now(), 'updateBy': self.uid} self.data.update(regular) # If the update operation does not result in an insert, $setOnInsert does nothing. data = { '$set': self.data, '$setOnInsert': { 'createAt': datetime.now(), 'createBy': self.uid, 'valid': CONST.VALID } } result = self.model.update_by(condition=self.id or self.condition, data=data, upsert=upsert) return {'_id': result}, None def increment(self, upsert=True): regular = {'updateAt': datetime.now(), 'updateBy': self.uid} self.data.update(regular) data = {'$inc': self.data} result = self.model.increment(condition=self.id or self.condition, update=data, upsert=upsert) return result, None def remove(self): regular = { 'updateAt': datetime.now(), 'updateBy': self.uid, 'valid': CONST.INVALID } result = self.model.update_by(condition=self.id or self.condition, data={'$set': regular}) return {'_id': result}, None def delete(self): result = self.model.remove_by(condition=self.id or self.condition) return result, None def distinct(self): result = self.model.distinct(key=self.select, filter=self.condition) return result, None def create_user(self): raise NotImplementedError def add_user(self): raise NotImplementedError def drop_user(self): raise NotImplementedError def change_password(self): raise NotImplementedError def drop(self): raise NotImplementedError def aggregate(self): raise NotImplementedError def increment(self): raise NotImplementedError def write_file_to_grid(self): data = [] for file in self.files: data.append(self.model.write_file_to_grid(file)) return {'totalItems': len(self.files), 'items': data}, None def write_buffer_to_grid(self): raise NotImplementedError def write_stream_to_grid(self): data = [] for file in self.files: content_type = file.content_type name = file.filename length = file.stream.getbuffer().nbytes data.append( self.model.write_stream_to_grid(name, file.stream, content_type, length)) return {'totalItems': len(data), 'items': data}, None def read_file_from_grid(self): folder = self.data['folder'] name = self.data['name'] if folder is None: folder = Config.instance().app.tmp if name is None: name = light.helper.random_guid(8) return self.model.read_file_from_grid(self.id, os.path.join(folder, name)) def read_buffer_from_grid(self): raise NotImplementedError def read_stream_from_grid(self): return self.model.read_stream_from_grid(self.id)
def is_unique(handler, data, option): model = Model(domain=handler.domain, code=handler.code, table=option['table']) count = model.total(condition=option['condition']) return count <= 0
def test_update_by(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.update_by({'valid': 5}, {'$inc': {'valid': 3}})) print(model.update_by('57e0b76f1d41c81776ced8ca', {'$inc': {'valid': 30}})) print(model.update_by(ObjectId('57e0b76f1d41c81776ced8ca'), {'$inc': {'valid': 30}}))
def test_increment(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.increment({'valid': 3}, {'$inc': {'valid': 2}})) print(model.increment('123457791d41c817b4a09b37', {'$inc': {'valid': 57}})) print(model.increment(ObjectId('678907791d41c817b4a09b37'), {'$inc': {'valid': 68}}))
def test_total(self): model = Model('LightDB', 'light', 'unittest', self.define) self.assertGreater(model.total({'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}), 3)
def test_remove_by(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.remove_by({'nestsii.fields.nestarray.date': datetime(2005, 1, 1, 0, 0)})) print(model.remove_by('57e0b76f1d41c81776ced8cc')) print(model.remove_by(ObjectId('57e0b76f1d41c81776ced8cc')))
def test_add(self): model = Model('LightDB', 'light', 'unittest', self.define) print( model.add({ 'schema': 'a', 'nestsii': [{ 'fields': [{ 'nestarray': [{ 'date': '2003/01/01' }, { 'date': '2004/01/01' }] }] }], 'valid': '1' })) print( model.add({ 'schema': 'b', 'nestsii': [{ 'fields': [{ 'nestarray': [{ 'date': '2005/01/01' }, { 'date': '2006/01/01' }] }] }], 'valid': '2' })) print( model.add({ 'schema': 'c', 'nestsii': [{ 'fields': [{ 'nestarray': [{ 'date': '2007/01/01' }, { 'date': '2008/01/01' }] }] }], 'valid': '3' })) print( model.add({ 'schema': 'd', 'nestsii': [{ 'fields': [{ 'nestarray': [{ 'date': '2009/01/01' }, { 'date': '2010/01/01' }] }] }], 'valid': '4' })) print( model.add([{ 'schema': 'e', 'nestsii': [{ 'fields': [{ 'nestarray': [{ 'date': '2003/01/01' }, { 'date': '2004/01/01' }] }] }], 'valid': '5' }, { 'schema': 'e', 'nestsii': [{ 'fields': [{ 'nestarray': [{ 'date': '2003/01/01' }, { 'date': '2004/01/01' }] }] }], 'valid': '5' }]))
def test_distinct(self): model = Model('LightDB', 'light', 'unittest', self.define) print(model.distinct('valid')) print(model.distinct('valid', {'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}))
def test_total(self): model = Model('LightDB', 'light', 'unittest', self.define) self.assertGreater( model.total( {'nestsii.fields.nestarray.date': datetime(2003, 1, 1, 0, 0)}), 3)
class Controller(object): """ 1. 封装model的调用, 接收的参数为handler对象 2. 进行缺省值的设定, 如 updateBy createAt valid 等 3. 格式化输出的JSON结果, 获取List时会付上件数totalItems等信息 4. 统一封装关于数据库操作的错误内容 """ def __init__(self, handler, table=None): define = {} if table: define = getattr(Structure.instance(), table) self.model = Model(domain=handler.domain, code=handler.code or CONST.DEFAULT_TENANT, table=table, option={'define': define}) self.uid = handler.uid self.condition = handler.params.condition or {} self.data = handler.params.data or {} self.id = handler.params.id self.select = handler.params.select or handler.params.field self.sort = handler.params.sort self.files = handler.params.files self.skip = handler.params.skip self.limit = handler.params.limit or 0 if self.skip is None: self.skip = 0 def get(self): if 'valid' not in self.condition: self.condition['valid'] = CONST.VALID result = self.model.get(condition=self.id or self.condition, select=self.select) return result, None def list(self): if 'valid' not in self.condition: self.condition['valid'] = CONST.VALID count = self.model.total(condition=self.condition) result = self.model.get_by( condition=self.condition, select=self.select, sort=self.sort, skip=self.skip, limit=self.limit ) return {'totalItems': count, 'items': result}, None def add(self): if not isinstance(self.data, list): self.data = [self.data] for data in self.data: regular = { 'createAt': datetime.now(), 'createBy': self.uid, 'updateAt': datetime.now(), 'updateBy': self.uid, 'valid': CONST.VALID } data.update(regular) result = self.model.add(data=self.data) return {'_id': result}, None def total(self): if 'valid' not in self.condition: self.condition['valid'] = CONST.VALID count = self.model.total(condition=self.condition) return count, None def count(self): return self.total() def update(self, upsert=False): if 'valid' not in self.condition: self.condition['valid'] = CONST.VALID regular = {'updateAt': datetime.now(), 'updateBy': self.uid} self.data.update(regular) # If the update operation does not result in an insert, $setOnInsert does nothing. data = { '$set': self.data, '$setOnInsert': {'createAt': datetime.now(), 'createBy': self.uid, 'valid': CONST.VALID} } result = self.model.update_by(condition=self.id or self.condition, data=data, upsert=upsert) return {'_id': result}, None def increment(self, upsert=True): regular = {'updateAt': datetime.now(), 'updateBy': self.uid} self.data.update(regular) data = {'$inc': self.data} result = self.model.increment(condition=self.id or self.condition, update=data, upsert=upsert) return result, None def remove(self): regular = {'updateAt': datetime.now(), 'updateBy': self.uid, 'valid': CONST.INVALID} result = self.model.update_by(condition=self.id or self.condition, data={'$set': regular}) return {'_id': result}, None def delete(self): result = self.model.remove_by(condition=self.id or self.condition) return result, None def distinct(self): result = self.model.distinct(key=self.select, filter=self.condition) return result, None def create_user(self): raise NotImplementedError def add_user(self): raise NotImplementedError def drop_user(self): raise NotImplementedError def change_password(self): raise NotImplementedError def drop(self): raise NotImplementedError def aggregate(self): raise NotImplementedError def increment(self): raise NotImplementedError def write_file_to_grid(self): data = [] for file in self.files: data.append(self.model.write_file_to_grid(file)) return {'totalItems': len(self.files), 'items': data}, None def write_buffer_to_grid(self): raise NotImplementedError def write_stream_to_grid(self): data = [] for file in self.files: content_type = file.content_type name = file.filename length = file.stream.getbuffer().nbytes data.append(self.model.write_stream_to_grid(name, file.stream, content_type, length)) return {'totalItems': len(data), 'items': data}, None def read_file_from_grid(self): folder = self.data['folder'] name = self.data['name'] if folder is None: folder = Config.instance().app.tmp if name is None: name = light.helper.random_guid(8) return self.model.read_file_from_grid(self.id, os.path.join(folder, name)) def read_buffer_from_grid(self): raise NotImplementedError def read_stream_from_grid(self): return self.model.read_stream_from_grid(self.id)
def init(self): valid = {'valid': 1} # configuration select = 'type, key, value, valueType' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_CONFIG) self.cache.set(CONST.SYSTEM_DB_CONFIG, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # validator select = 'group,name,rule,key,option,message,sanitize,class,action,condition' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_VALIDATOR) self.cache.set(CONST.SYSTEM_DB_VALIDATOR, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # i18n select = 'type,lang,key' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_I18N) self.cache.set(CONST.SYSTEM_DB_I18N, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # structure select = 'public,lock,type,kind,tenant,version,schema,items,extend,tenant' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_STRUCTURE) self.cache.set(CONST.SYSTEM_DB_STRUCTURE, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # board select = 'schema,api,type,kind,path,class,action,filters,selects,sorts,reserved,script' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_BOARD) self.cache.set(CONST.SYSTEM_DB_BOARD, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # route select = 'template,url,class,action' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_ROUTE) self.cache.set(CONST.SYSTEM_DB_ROUTE, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) # tenant select = 'code,name' model = Model(domain=self.domain, code=CONST.SYSTEM_DB_PREFIX, table=CONST.SYSTEM_DB_TENANT) self.cache.set(CONST.SYSTEM_DB_TENANT, model.get_by(condition=valid, select=select, limit=CONST.MAX_INT)) return model.db