Skip to content

Commit

Permalink
Merge pull request #98 from cuenca-mx/custom-query-validator
Browse files Browse the repository at this point in the history
Add Custom query validator
  • Loading branch information
pachCode authored Apr 26, 2022
2 parents 8d2d6d7 + 1a115cb commit 02cd95e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
23 changes: 23 additions & 0 deletions examples/blueprints/custom_query_blueprint.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions fast_agave/blueprints/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion fast_agave/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.4.0'
__version__ = '0.5.0'

0 comments on commit 02cd95e

Please sign in to comment.