Example #1
0
def create_class(klass, client=None):
    from models.orient_sql_utils import is_model
    from models.orient_sql_utils import get_model_extensions
    if not is_model(klass):
        raise ValueError("Only OrientDB models can be used to create Orient classes")
    
    if client is None:
        client = get_connection()
    
    class_name = get_orient_valid_class_name(klass)
    
    create_str = 'CREATE CLASS %s' % class_name
    
    parents = get_model_extensions(klass)
    
    if len(parents) > 0:
        if len(parents) > 1:
            extenders = ",".join(parents)
        else:
            extenders = parents[0]
        create_str  =  '%s EXTENDS %s' % (create_str, extenders)
    
    instance = klass()
        
    LOGGER.debug("creating class with command %s" % create_str)

    client.command(create_str)
    
    for k in instance._fields.keys():
        if not instance._fields[k].inherited:
            field_str = 'CREATE PROPERTY %s.%s %s' % (class_name, 
                                  instance._py_to_orient_field_mapping[k], 
                                  instance._fields[k].orientdb_type)
            LOGGER.debug("applying property with command %s" % (field_str))
            client.command(field_str)
Example #2
0
 def test_is_model(self):
     
     self.assertFalse(is_model(Foo))
     self.assertFalse(is_model(Foo()))
     self.assertTrue(is_model(ImAModel))
     self.assertTrue(is_model(ImAModel()))
     self.assertTrue(is_model(IAMToo))
     self.assertTrue(is_model(IAMToo()))
     self.assertTrue(is_model(AndMeThree))
     self.assertTrue(is_model(AndMeThree()))