def handle(self, *args, **options): JomFactory.autodiscover() factory = JomFactory.default() # By default jom files are placed in MEDIA_ROOT # and not in STATIC_ROOT because only collectstatic # should put files in STATIC_ROOT base_path = settings.MEDIA_ROOT if hasattr(settings, "JOM_ROOT"): # Developers can specify a different path by # setting the JOM_ROOT variable and adding it to # settings.STATICFILES_DIRS base_path = settings.JOM_ROOT folder = os.path.join(base_path, "jom/js/descriptors").replace("\\", "/") for model, jomDescriptor in factory.descriptors.items(): # get the django module name app_name = model.__module__.split(".")[::-1][1] app_folder = os.path.join(folder, app_name).replace("\\", "/") filename = os.path.join(app_folder, jomDescriptor.__class__.__name__ + ".js").replace("\\", "/") ensure_dir(filename) print("[JOM] Generating " + filename) out_file = open(filename, "w") out_file.write(factory.getJomClass(model).renderClass()) out_file.close()
class JomFactoryTestCase(TestCase): def setUp(self): self.factory = JomFactory() def tearDown(self): self.factory = None def testRegisterJomDescriptor(self): descriptor = self.factory.register(SimpleModelJomDescriptor) self.assertTrue(isinstance(descriptor, SimpleModelJomDescriptor)) def testGetForName(self): name = SimpleModel.__name__ descriptor = self.factory.register(SimpleModelJomDescriptor) self.assertEqual(descriptor, self.factory.getForName(name)) def testGetForModel(self): descriptor = self.factory.register(SimpleModelJomDescriptor) self.assertEqual(descriptor, self.factory.getForModel(SimpleModel)) def testGetJomInstance(self): instance = SimpleModel.objects.create(name = "foo") descriptor = self.factory.register(SimpleModelJomDescriptor) self.assertEqual(descriptor, self.factory.getJomInstance(instance).descriptor) instance.delete() def testGetJomClass(self): descriptor = self.factory.register(SimpleModelJomDescriptor) self.assertEqual(descriptor, self.factory.getJomClass(SimpleModel).descriptor)
def jom_async_update_ajax(request): values = request.POST.dict() factory = JomFactory.default() model = values.get('model') descriptor = factory.getForName(model) if descriptor == None: raise ValueError( "Descriptor for model %s was not registered" % model) if not descriptor.canUpdate(request): raise ValueError( "Permission denied for user %s." % request.user) instance = descriptor.model.objects.get( id = values.get("id")) #jomInstance = factory.getJomInstance(instance) #jomInstance.update(values) form = descriptor.update_form( request.POST, request.FILES, instance = instance) if form.is_valid(): updated_instance = form.save() jomInstance = factory.getJomInstance(updated_instance) return True, {'config': jomInstance.instanceToDict()}, "" else: return False, form._errors, form.non_field_errors()
def jom_dict(instance): """ Creates an associative array which can initialize a JomInstance. """ jomInstance = JomFactory.default().getJomInstance(instance) if not jomInstance: raise AssertionError( "Model not registered: %s" % instance.__class__) return {'jomInstance': jomInstance}
def register_instance(instance): """ Creates an associative array which can initialize a JomInstance. """ jomInstance = JomFactory.default().getJomInstance(instance) if not jomInstance: raise AssertionError( "Model not registered: %s" % instance.__class__) return {'jomInstance': jomInstance, 'clazz': jomInstance.descriptor.__class__.__name__, 'instance': instance}
def jom_async_create_ajax(request): values = request.POST.dict() factory = JomFactory.default() model = values.get('model') descriptor = factory.getForName(model) if descriptor == None: raise ValueError( "Descriptor for model %s was not registered" % model) if not descriptor.canCreate(request): raise ValueError( "Permission denied for user %s." % request.user) instance = descriptor.model() jomInstance = factory.getJomInstance(instance) jomInstance.update(values) return {'config': jomInstance.instanceToDict()}
def jom_async_get_ajax(request): values = request.POST.dict() factory = JomFactory.default() model = values.get('model') descriptor = factory.getForName(model) if descriptor == None: raise ValueError( "Descriptor for model %s was not registered" % model) if not descriptor.canGet(request): raise ValueError( "Permission denied for user %s." % request.user) instance = descriptor.model.objects.get( id = values.get("id")) jomInstance = factory.getJomInstance(instance) return True, {'config': jomInstance.instanceToDict()}, ""
def jom_async_delete_ajax(request): values = request.POST.dict() factory = JomFactory.default() model = values.get('model') descriptor = factory.getForName(model) if descriptor == None: raise ValueError( "Descriptor for model %s was not registered" % model) if not descriptor.canDelete(request): raise ValueError( "Permission denied for user %s." % request.user) instance = descriptor.model.objects.get( id = values.get("id")) instance.delete() return {}
class JomDescriptorTestCase(TestCase): def setUp(self): self.factory = JomFactory() self.descriptor = self.factory.register( ModelWithAllFieldsJomDescriptor) def tearDown(self): self.factory = None self.descriptor = None def testJomFieldsCreated(self): fields = self.descriptor.jom_fields self.assertEqual(StringJomField, fields['slug']) self.assertEqual(StringJomField, fields['text']) self.assertEqual(StringJomField, fields['char']) self.assertEqual(StringJomField, fields['email']) self.assertEqual(StringJomField, fields['url']) self.assertEqual(StringJomField, fields['comma_separated_integer']) self.assertEqual(NumeralJomField, fields['integer']) self.assertEqual(NumeralJomField, fields['positive_integer']) self.assertEqual(NumeralJomField, fields['small_integer']) self.assertEqual(NumeralJomField, fields['small_positive_integer']) self.assertEqual(NumeralJomField, fields['big_integer']) self.assertEqual(NumeralJomField, fields['float']) self.assertEqual(BooleanJomField, fields['boolean']) self.assertEqual(DateJomField, fields['date']) self.assertEqual(DateJomField, fields['time']) self.assertEqual(DateJomField, fields['datetime']) self.assertEqual(UrlJomField, fields['file']) self.assertEqual(UrlJomField, fields['image'])
''' Created on Jul 30, 2012 @author: Michele Sama ([email protected]) ''' from jom.factory import JomFactory, JomDescriptor from jomtest.foo.models import SimpleModel, ModelWithAllFields, ModelWithFK,\ ModelWithM2M, ModelWithOneToOne jomFactory = JomFactory.default() class SimpleModelJomDescriptor(JomDescriptor): model = SimpleModel def canUpdate(self, request): return True def canCreate(self, request): return True def canGet(self, request): return True def canDelete(self, request): return True jomFactory.register(SimpleModelJomDescriptor) class ModelWithAllFieldsJomDescriptor(JomDescriptor): model = ModelWithAllFields
def setUp(self): self.factory = JomFactory() self.descriptor = self.factory.register( ModelWithAllFieldsJomDescriptor)
def setUp(self): self.factory = JomFactory()
def setUp(self): JomFactory.autodiscover()