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()) self._uuid = self.id #print("new id is: " + self.id) self.init_observers()
def __init__(self, *args, **kwargs): """ """ self.tablename = pluralize(self.__class__.__name__.lower()) self.index = self.tablename super().init_on_load(*args, **kwargs)
def __init__(self, *args, **kwargs): """ """ self.tablename = pluralize(self.__class__.__name__.lower()) self.index = self.tablename super().init_on_load(*args, **kwargs)
def init_on_load(self, *args, **kwargs): self.session=None self.tablename = pluralize(self.__class__.__name__.lower()) # # 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 = self.__class__.__dict__["schema"] # # 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__: setattr(self, key, kwargs[key])
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 # 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 return True
def display_mins(m): message = str(m) if m == 1: message += " min" else: message += pluralize(" min") display.scroll(message, wait=False, loop=True)
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._uuid = str(uuid.uuid4()) self.init_observers()
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])
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} ) setattr(self, "_jsonify", 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() # # 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])
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 __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, reflect=False): """ 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") filename = model_type + "_model_template.py" if str.lower(model_type) == "sql" and reflect: filename = "sql_model_reflection_template.py" res = loader.load(filename).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 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())
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()
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()