def init_on_load(self, *args, **kwargs): """ basic setup for all mongoDB models. """ #print("executin init_on_load") #create an index for our own id field. # # 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()) #print("new id is: " + self.id) self.init_observers() self.setup_dirty_model()
def init_on_load(self, *args, **kwargs): # # setup a mashmallow schema to be able to dump (serialize) and load (deserialize) # models to json quick, safe and easy. # see: http://marshmallow-sqlalchemy.readthedocs.io/en/latest/ # and link it to the model. (as jsonify attribute) # this enables the model to load / dump json # #print(kwargs) self.class_name = self.__class__.__name__.capitalize() from marshmallow_sqlalchemy import ModelSchema cls_meta = type("Meta", (object, ), {"model": self.__class__}) jschema_class = type( self.class_name + 'Schema', (ModelSchema, ), { "Meta": cls_meta, "model": self.__class__, #"sqla_session" : session }) setattr(self, "marshmallow_schema", jschema_class()) self.session = session self.table = self.metadata.tables[pluralize( self.__class__.__name__.lower())] # # if there is a schema (cerberus) set it in the instance # #print(str(self.__class__.__dict__.keys())) if "schema" in self.__class__.__dict__: #print(" .. found a schema for: " +str(self.__class__.__name__) + " in class dict") self.schema = self.__class__.__dict__["schema"] # add the sqlcolumns schema definitions to the cerberus schema (if there are any) if myapp["sql_auto_schema"]: self._setup_schema_from_sql() #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.init_observers() self.setup_dirty_model()
def init_on_load(self, *args, **kwargs): """ should be called from instances or BaseModels __init__ will be called by sqlalchemy automatically on model creation """ self.tablename = pluralize(self.__class__.__name__.lower()) self.setup_instance_schema() if "format" in kwargs: self.setup_from_format(args, kwargs)
def init_on_load(self, *args, **kwargs): #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 = tinydb.table(self.tablename) self.where = where # # 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.init_observers() self.setup_dirty_model()
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))
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
def __tablename__(cls): """ returns the tablename for this model """ return pluralize(cls.__name__.lower())