diff --git a/examples/blueprints/custom_query_blueprint.py b/examples/blueprints/custom_query_blueprint.py new file mode 100644 index 0000000..ab2c037 --- /dev/null +++ b/examples/blueprints/custom_query_blueprint.py @@ -0,0 +1,23 @@ +from typing import Any + +from fast_agave.blueprints import RestApiBlueprint +from starlette_context import context + + +class CustomQueryBlueprint(RestApiBlueprint): + @property + def custom(self) -> str: + return context['custom'] + + def property_filter_required(self) -> bool: + return context.get('custom_filter_required') + + def custom_filter_required(self, query_params: Any, model: any) -> None: + if self.property_filter_required() and hasattr(model, 'custom'): + query_params.custom = self.custom + + def user_id_filter_required(self) -> bool: + return False + + def platform_id_filter_required(self) -> bool: + return False diff --git a/fast_agave/blueprints/rest_api.py b/fast_agave/blueprints/rest_api.py index 618ff28..ab53e16 100644 --- a/fast_agave/blueprints/rest_api.py +++ b/fast_agave/blueprints/rest_api.py @@ -29,6 +29,14 @@ def user_id_filter_required(self) -> bool: def platform_id_filter_required(self) -> bool: return context['platform_id_filter_required'] + def custom_filter_required(self, query_params: Any, model: Any) -> None: + """ + Overwrite this method in order to add new context + based on custom filter. + set de value of your filter ex query_params.wallet = self.wallet + """ + pass + async def retrieve_object( self, resource_class: Any, resource_id: str ) -> Any: @@ -218,6 +226,8 @@ async def query(request: Request): cls.model, 'user_id' ): query_params.user_id = self.current_user_id + # Call for custom filter implemented in overwritemethod + self.custom_filter_required(query_params, cls.model) filters = cls.get_query_filter(query_params) if query_params.count: diff --git a/fast_agave/version.py b/fast_agave/version.py index abeeedb..2b8877c 100644 --- a/fast_agave/version.py +++ b/fast_agave/version.py @@ -1 +1 @@ -__version__ = '0.4.0' +__version__ = '0.5.0'