Пример #1
0
 def __tablename__(cls):
     """ 
         returns the tablename for this model. Convention: pluralized Modelname
         You can overwrite this by just setting the __tablename__ = <yourtablename> in the
         model class.
     """
     return pluralize(cls.__name__.lower())
Пример #2
0
 def __init__(self, *args, **kwargs):
     """
         constructor
     """
     #super(ModelObject, self).init_on_load(*args, **kwargs)
     self.tablename = pluralize(self.__class__.__name__.lower())
     self.doc_type = self.tablename
     
     #
     # if there is a schema (cerberus) set it in the instance
     #
     if "schema" in self.__class__.__dict__:
         print(" .. found a schema for: " +str(self.__class__.__name__) + " in class dict")
         self.schema = merge_two_dicts(
             self.__class__.__dict__["schema"],
             self.__class__.basic_schema)
         print("  .. Schema is now: " + str(self.schema))
Пример #3
0
def generate_model(model_name=None, model_type=None, appname=None):
    """ generates a small model with the given modelname
        also sets the right db and table settings and further boilerplate configuration.
        Template engine = tornado.templates
    """
    #
    # set some attributes
    #
    print(40 * "-")
    print(" generating model: " + model_name)
    print(40 * "-")
    try:
        loader = template.Loader(templates["stubs_path"])
        model_class_name = camel_case(model_name)
        print("model_class_name: " + model_class_name)
        model_name_plural = pluralize(model_name)
        print("model_name_plural: " + model_name_plural)
        #
        # create the model
        #
        ofilePath = os.path.join(templates["model_path"], model_type)
        ofile = open(os.path.join(ofilePath, model_name + ".py"), "wb")
        res = loader.load(model_type + "_model_template.py").generate(
            model_name=model_name,
            model_name_plural=model_name_plural,
            model_class_name=model_class_name,
            appname=appname,
            model_type=model_type)
        ofile.write(res)
        ofile.close()
    except:
        return False
    print("... generated: " + model_type + " DB Model")
    print(40 * "-")
    print("... in : " + ofile.name)
    print(40 * "-")
    return True
Пример #4
0
    def init_on_load(self, *args, **kwargs):
        """
            basic setup for all mongoDB models.
        """
        #print("executin init_on_load")
        
        super().init_on_load()
        
        self.basic_schema = {
            "id"    :   { "type" : "string", "default" : None },
            "_uuid"    :   { "type" : "string", "default" : None },
            "created_at"    : { "type" : "datetime", "default" : None },
            "last_updated"    : { "type" : "datetime", "default" : None },
        }
        #create an index for our own id field.
        self.setup_instance_schema()
        
        #
        # if there is a schema (cerberus) set it in the instance
        #
        # if "schema" in self.__class__.__dict__:
        #     #print(" .. found a schema for: " +str(self.__class__.__name__) + " in class dict")
        #     self.schema = merge_two_dicts(
        #         self.__class__.__dict__["schema"],
        #         self.__class__.basic_schema)
            #print("  .. Schema is now: " + str(self.schema))

        # setup  the instance attributes from schema
        #for key in self.schema.keys():
        #    if self.schema[key].get("default", None) != None:
        #        setattr(self,key,self.schema[key].get("default"))
        #        self.schema[key].pop("default", None)
        #    else:
        #        #print("no default for: " + str(self.schema[key]))
        #        setattr(self, key, None)
        self.setup_instance_values()           
        #
        # setup values from kwargs or from init_from_<format> if format="someformat"
        # example: m = Model( data = { 'test' : 1 }, format="json")
        # will call m.init_from_json(data)
        #
        if "format" in kwargs:
            # set the format and call the according init_from_<format> method
            # which initializes the instance with the given vaules (from data)
            # e.g. Model(format=json, data={data})
            f = getattr(self, "init_from_" + kwargs["format"], None)
            if f:
                f(kwargs)
        else:
            # initializes the instanmce with the given kwargs values:
            # e.g.: Model(test="sometext", title="sometitle")
            for key in kwargs.keys():
                #if key in self.__class__.__dict__:
                if key in self.schema:
                    setattr(self, key, kwargs[key])
        
        self.table = db[pluralize(self.__class__.__name__.lower())]
        self.collection = self.table
        self.table.create_index([('id', pymongo.ASCENDING)], unique=True)
        
        self.tablename = pluralize(self.__class__.__name__.lower())
        #self.table = self.__class__.table
        self._id = None
        self.id = str(uuid.uuid4())
        self._uuid = self.id
        #print("new id is: " + self.id) 
        self.init_observers()
Пример #5
0
    def init_on_load(self, *args, **kwargs):
        """
            Everything that needs to be initialized for the model.
            By default the configured connection from the 
            conf.config["database"] section is taken
        """

        super().init_on_load()
        self.set_connection()
        #self.id = uuid.uuid4()
        #self.created_at = datetime.datetime.now()
        #self.last_updated = datetime.datetime.now()

        self.session=None
        self.tablename = pluralize(self.__class__.__name__.lower())
        #
        # all further Db operations will work on the table
        #
        self.table = self.db.table(self.tablename)
        self.where = where

        self.basic_schema = {
            "id"    :   { "type" : "string" },
            "_uuid" :  { "type" : "string", "default" : uuid.uuid4()},
            #"eid"   :   { "type" : "string" },
            "created_at"    : { "type" : "datetime" },
            "last_updated"    : { "type" : "datetime" },
        }
        if self.__class__._use_pow_schema_attrs:
            self.setup_instance_schema()
        
        #
        # if there is a schema (cerberus) set it in the instance
        #
        # if "schema" in self.__class__.__dict__:
        #     #print(" .. found a schema for: " +str(self.__class__.__name__) + " in class dict")
        #     self.schema = merge_two_dicts(
        #         self.__class__.__dict__["schema"],
        #         self.__class__.basic_schema)
        #     #print("  .. Schema is now: " + str(self.schema))

        # setup  the instance attributes from schema
        #for key in self.schema.keys():
        #    if self.schema[key].get("default", None) != None:
        #        setattr(self,key,self.schema[key].get("default"))
        #        self.schema[key].pop("default", None)
        #    else:
        #        #print("no default for: " + str(self.schema[key]))
        #        setattr(self, key, None)
        self.setup_instance_values()           
        #
        # setup values from kwargs or from init_from_<format> if format="someformat"
        # example: m = Model( data = { 'test' : 1 }, format="json")
        # will call m.init_from_json(data)
        #
        if "format" in kwargs:
            # set the format and call the according init_from_<format> method
            # which initializes the instance with the given vaules (from data)
            # e.g. Model(format=json, data={data})
            f = getattr(self, "init_from_" + kwargs["format"], None)
            if f:
                f(kwargs)
        else:
            # initializes the instanmce with the given kwargs values:
            # e.g.: Model(test="sometext", title="sometitle")
            for key in kwargs.keys():
                #if key in self.__class__.__dict__:
                if key in self.schema:
                    setattr(self, key, kwargs[key])
        #self._uuid = str(uuid.uuid4())
        self.init_observers()