def restfulcls_to_extjsstore(restfulcls, integrateModel=False, modelkwargs={}, storeidsuffix=""): """ Create an extjs store from the given restful class. :param restfulcls: A class defined using the :ref:`RESTful API <restful>`. :param integrateModel: Make the model a part of the store. Uses Uses :func:`~devilry.apps.extjshelpers.modelintegration.restfulcls_to_extjsmodel` with ``modelkwargs`` as arguments. :param modelkwargs: See ``integrateModel``. :param storeidsuffix: Forwarded to func:`get_extjs_storeid` to generate the ``id`` of the store and to func:`devilry.extjshelpers.modelintegration.get_extjs_modelname` (as modelnamesuffix) to generate the model name. """ if integrateModel: modelkwargs["modelnamesuffix"] = storeidsuffix model = restfulcls_to_extjsmodel(restfulcls, **modelkwargs) else: model = "'{modelname}'".format(modelname=get_extjs_modelname(restfulcls, storeidsuffix)) return """Ext.create('Ext.data.Store', {{ model: {model}, id: '{id}', remoteFilter: true, remoteSort: true, autoSync: true }})""".format( model=model, id=get_extjs_storeid(restfulcls, storeidsuffix) )
def test_to_extjsmodel(self): actual = restfulcls_to_extjsmodel(RestUser) expected = """Ext.define('devilry.apps.extjshelpers.tests.SimplifiedUser', { extend: 'Ext.data.Model', requires: ['devilry.extjshelpers.RestProxy'], fields: [{"type": "int", "name": "id"}, {"type": "auto", "name": "first"}, {"type": "auto", "name": "last"}, {"type": "auto", "name": "email"}, {"type": "int", "name": "score"}], idProperty: 'id', proxy: Ext.create('devilry.extjshelpers.RestProxy', { url: '/restuser', extraParams: { getdata_in_qrystring: true, result_fieldgroups: '[]' }, reader: { type: 'json', root: 'items', totalProperty: 'total' }, writer: { type: 'json' } }) })""" actual = actual.split('\n') expected = expected.split('\n') for index, actualline in enumerate(actual): expectedline = expected[index] self.assertEqual(actualline.strip(), expectedline.strip())
def test_to_extjsmodel(self): actual = restfulcls_to_extjsmodel(RestUser) expected = r""" /******************************************************************************* * NOTE: You will need to add the following before your application code: * * Ext.Loader.setConfig({ * enabled: true, * paths: { * 'devilry': DevilrySettings.DEVILRY_STATIC_URL + '/extjs_classes' * } * }); * Ext.syncRequire('devilry.extjshelpers.RestProxy'); ******************************************************************************/ Ext.define('devilry.apps.extjshelpers.tests.SimplifiedUser', { extend: 'Ext.data.Model', requires: ['devilry.extjshelpers.RestProxy'], fields: [{"type": "int", "name": "id"}, {"type": "auto", "name": "first"}, {"type": "auto", "name": "last"}, {"type": "auto", "name": "email"}, {"type": "int", "name": "score"}], idProperty: 'id', proxy: { type: 'devilryrestproxy', url: '/restuser', headers: { 'X_DEVILRY_USE_EXTJS': true }, extraParams: { getdata_in_qrystring: true, result_fieldgroups: '[]' }, reader: { type: 'json', root: 'items', totalProperty: 'total' }, writer: { type: 'json' } } })""" actual = actual.split('\n') expected = expected.split('\n') for index, actualline in enumerate(actual): expectedline = expected[index] self.assertEqual(actualline.strip(), expectedline.strip())
def test_to_extjsmodel(self): js = restfulcls_to_extjsmodel(RestUser) expected = """Ext.define('devilry.apps.extjshelpers.tests.SimplifiedUser', { extend: 'Ext.data.Model', requires: ['devilry.extjshelpers.RestProxy'], fields: [{"type": "int", "name": "id"}, {"type": "auto", "name": "first"}, {"type": "auto", "name": "last"}, {"type": "auto", "name": "email"}, {"type": "int", "name": "score"}], idProperty: 'id', proxy: Ext.create('devilry.extjshelpers.RestProxy', { url: '/restuser', extraParams: { getdata_in_qrystring: true, result_fieldgroups: '[]' }, reader: { type: 'json', root: 'items', totalProperty: 'total' }, writer: { type: 'json' } }) })""" self.assertEquals(js, expected)
def test_to_extjsmodel_fieldgroups(self): js = restfulcls_to_extjsmodel(RestUser) self.assertFalse('textfields' in js) js = restfulcls_to_extjsmodel(RestUser, ['textfields']) self.assertTrue('textfields' in js)