def __init__(self): self.handlers = routes # importing !"activates" the add_route decorator self.import_all_handlers() h=getattr(self.__class__, "handlers", None) #self.handlers+=h # use the absolute positioning routing table #print(list(reversed(sorted(htmp, key=get_key)))) # just for route ordering. (sotrted) def get_key(item): return item[1] ## working version: #self.show_positioned_routes( list(reversed(sorted(h, key=get_key))) ) #hordered=[x[0] for x in reversed(sorted(h, key=get_key))] ## end! hordered = self.order_routes(h) #print(str(hordered)) self.handlers+=hordered # merge two dictionaries: z = { **a, **b } # http://stackoverflow.com/questions/38987/how-to-merge-two-python-dictionaries-in-a-single-expression settings = merge_two_dicts( dict( template_path=os.path.join(os.path.dirname(__file__), cfg.server_settings["template_path"]), static_path=os.path.join(os.path.dirname(__file__), cfg.server_settings["static_path"]) ) , cfg.server_settings) super(Application, self).__init__(self.handlers, **settings) self.Session = Session self.engine = engine self.Base = Base
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 setup_instance_schema(self): """ 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"], basic_schema) print(" .. Schema is now: " + str(self.schema))
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))