def _filter(instance, condition) -> dict: """ Filter properties of instace based on condition :param instance: :param condition: :rtype: dict """ # Use iterate_properties when available if hasattr(instance, 'iterate_properties'): return {field.key: field for field in instance.iterate_properties if condition(field)} # Try sqlalchemy inspection try: inspection = sqinspect(instance) if hasattr(inspection, "all_orm_descriptors"): return {field.key: field for key, field in inspection.all_orm_descriptors.items() if condition(field)} elif hasattr(inspection, "attrs"): return {field.key: field for key, field in inspection.attrs.items() if condition(field)} else: raise NoInspectionAvailable() # Use Inspect except NoInspectionAvailable: return {field.key: field for key, field in inspect.getmembers(instance) if condition(field)}
def initialize(self, model, manager, methods, preprocessor, postprocessor, allow_patch_many, allow_method_override, validation_exceptions, exclude_queries, exclude_hybrids, include_columns, exclude_columns, results_per_page, max_results_per_page): """ Init of the handler, derives arguments from api create_api_blueprint :param model: The sqlalchemy model :param manager: The tornado_restless Api Manager :param methods: Allowed methods for this model :param preprocessor: A dictionary of preprocessor functions :param postprocessor: A dictionary of postprocessor functions :param allow_patch_many: Allow PATCH with multiple datasets :param allow_method_override: Support X-HTTP-Method-Override Header :param validation_exceptions: :param exclude_queries: Don't execude dynamic queries (like from associations or lazy relations) :param exclude_hybrids: When exclude_queries is True and exclude_hybrids is False, hybrids are still included. :param include_columns: Whitelist of columns to be included :param exclude_columns: Blacklist of columns to be excluded :param results_per_page: The default value of how many results are returned per request :param max_results_per_page: The hard upper limit of resutest per page :reqheader X-HTTP-Method-Override: If allow_method_override is True, this header overwrites the request method """ # Override Method if Header provided if allow_method_override and 'X-HTTP-Method-Override' in self.request.headers: self.request.method = self.request.headers['X-HTTP-Method-Override'] super().initialize() self.model = SessionedModelWrapper(model, manager.session_maker()) self.pk_length = len(sqinspect(model).primary_key) self.methods = [method.lower() for method in methods] self.allow_patch_many = allow_patch_many self.validation_exceptions = validation_exceptions self.preprocessor = preprocessor self.postprocessor = postprocessor self.results_per_page = results_per_page self.max_results_per_page = max_results_per_page self.include = self.parse_columns(include_columns) self.exclude = self.parse_columns(exclude_columns) self.to_dict_options = {'execute_queries': not exclude_queries, 'execute_hybrids': not exclude_hybrids}
def initialize(self, model, manager, methods: set, preprocessor: dict, postprocessor: dict, allow_patch_many: bool, allow_method_override: bool, validation_exceptions, exclude_queries: bool, exclude_hybrids: bool, include_columns: list, exclude_columns: list, results_per_page: int, max_results_per_page: int): """ Init of the handler, derives arguments from api create_api_blueprint :param model: The sqlalchemy model :param manager: The tornado_restless Api Manager :param methods: Allowed methods for this model :param preprocessor: A dictionary of preprocessor functions :param postprocessor: A dictionary of postprocessor functions :param allow_patch_many: Allow PATCH with multiple datasets :param allow_method_override: Support X-HTTP-Method-Override Header :param validation_exceptions: :param exclude_queries: Don't execude dynamic queries (like from associations or lazy relations) :param exclude_hybrids: When exclude_queries is True and exclude_hybrids is False, hybrids are still included. :param include_columns: Whitelist of columns to be included :param exclude_columns: Blacklist of columns to be excluded :param results_per_page: The default value of how many results are returned per request :param max_results_per_page: The hard upper limit of resutest per page :reqheader X-HTTP-Method-Override: If allow_method_override is True, this header overwrites the request method """ # Override Method if Header provided if allow_method_override and 'X-HTTP-Method-Override' in self.request.headers: self.request.method = self.request.headers[ 'X-HTTP-Method-Override'] super(BaseHandler, self).initialize() self.model = SessionedModelWrapper(model, manager.session_maker()) self.pk_length = len(sqinspect(model).primary_key) self.methods = [method.lower() for method in methods] self.allow_patch_many = allow_patch_many self.validation_exceptions = validation_exceptions self.preprocessor = preprocessor self.postprocessor = postprocessor self.results_per_page = results_per_page self.max_results_per_page = max_results_per_page self.include = self.parse_columns(include_columns) self.exclude = self.parse_columns(exclude_columns) self.to_dict_options = { 'execute_queries': not exclude_queries, 'execute_hybrids': not exclude_hybrids }
def get_hybrids(instance) -> list: """ Returns the relations objects of the model """ Proxy = namedtuple('Proxy', ['key', 'field']) if hasattr(instance, 'iterate_properties'): return [Proxy(key, field) for key, field in sqinspect(instance).all_orm_descriptors.items() if isinstance(field, hybrid_property)] else: return [Proxy(key, field) for key, field in inspect.getmembers(instance) if isinstance(field, hybrid_property)]
def get_proxies(instance) -> list: """ Returns the proxies objects of the model Inspired by https://groups.google.com/forum/?fromgroups=#!topic/sqlalchemy/aDi_M4iH7d0 """ Proxy = namedtuple('Proxy', ['key', 'field']) if hasattr(instance, 'iterate_properties'): return [Proxy(key, field) for key, field in sqinspect(instance).all_orm_descriptors.items() if isinstance(field, AssociationProxy)] else: return [Proxy(key, field) for key, field in inspect.getmembers(instance) if isinstance(field, AssociationProxy)]
def get_hybrids(instance) -> list: """ Returns the relations objects of the model """ Proxy = namedtuple('Proxy', ['key', 'field']) # Try sqlalchemy inspection try: return [Proxy(key, field) for key, field in sqinspect(instance).all_orm_descriptors.items() if isinstance(field, hybrid_property)] # Use Inspect except NoInspectionAvailable: return [Proxy(key, field) for key, field in inspect.getmembers(instance) if isinstance(field, hybrid_property)]