-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from kimbugp/develop
Implement query parser (#26)
- Loading branch information
Showing
26 changed files
with
224 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
.pytest_cache/v/cache/lastfailed | ||
.coverage | ||
htmlcov | ||
prod.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
from datetime import datetime | ||
from .schema_utils import validate_date | ||
|
||
from .schema_utils import validate_date | ||
|
||
from webargs import fields as flds | ||
|
||
|
||
def param(func): | ||
return flds.Nested({"operator": flds.Str(required=True), "field": flds.Str(required=True), "value": func}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import re | ||
|
||
from webargs import core | ||
from webargs.flaskparser import FlaskParser | ||
from functools import wraps | ||
|
||
suffix = {'ne': '!=', 'gt': '>', 'gte': '>=', | ||
'lt': '<', 'lte': '<=', 'eq': '='} | ||
|
||
|
||
param_regex = re.compile(r"(^[a-zA-Z]+[_]?[a-zA-Z]+)|(gt|lt|lte|eq|ne)\b") | ||
|
||
|
||
class CustomParamsParser(FlaskParser): | ||
def parse_querystring(self, req, name, field): | ||
return core.get_value(_structure_dict(req.args), name, field) | ||
|
||
|
||
def _structure_dict(dict_): | ||
def structure_dict_pair(r, key, value): | ||
m = param_regex.findall(key) | ||
operator = 'eq' | ||
if m: | ||
((col, *_), *t) = m | ||
if t: | ||
(*_, (*_, operator)) = t | ||
if r.get(col) is None: | ||
r[col] = {'operator': suffix.get( | ||
operator), 'value': value, 'field': col} | ||
r = {} | ||
for key, value in dict_.items(): | ||
structure_dict_pair(r, key, value) | ||
return r | ||
|
||
|
||
parser = CustomParamsParser() | ||
use_kwargs = parser.use_kwargs | ||
|
||
|
||
def use_args(fields): | ||
|
||
def decorated(func): | ||
@wraps(func) | ||
@parser.use_args(fields) | ||
def inner(*args, **kwargs): | ||
*args, params = args | ||
params = [item for item in params.values()] | ||
return func(*args, params, **kwargs) | ||
return inner | ||
return decorated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
|
||
from flask import jsonify, make_response | ||
|
||
from apps.cinema import api | ||
from apps.middlewares.validation import ValidationError | ||
from apps.cinema.schema.parser import parser | ||
|
||
|
||
@api.errorhandler(ValidationError) | ||
def handle_invalid_usage(error): | ||
return error.to_dict(), error.status_code | ||
|
||
|
||
@parser.error_handler | ||
def handle_request_parsing_error(err, req, schema, error_status_code, error_headers): | ||
raise ValidationError('error', payload={**err.messages}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.