Skip to content

Commit

Permalink
Merge pull request #74 from Eastwu5788/wudong
Browse files Browse the repository at this point in the history
feature(): update readme docs
  • Loading branch information
Eastwu5788 committed Apr 28, 2020
2 parents da22d0d + aa1119f commit e17fd74
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
14 changes: 13 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
.. py:currentmodule:: pre_request
Version 2.0.9 (20200509)
-------------------------

- TODO: 强化处理数组类型参数能力
- TODO: 过滤器剥离为普通过滤器和复杂跨字段过滤器

Version 2.0.8 (20200430)
-------------------------

- 支持复杂数据结构字段校验
- 支持跨数据结构的字段互相验证
- 支持自定义验证结果存储键名称

Version 2.0.7
--------------

Expand All @@ -15,4 +28,3 @@ Version 2.0.6

- 新增自定义响应,自定义过滤器,自定义格式化函数能力
- 新增大量跨字段验证方式

2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pre-request提供了非常方便的使用方法,也提供了灵活的扩展机
4. 格式化后的参数置于 `~flask.g` 中,同时尝试将格式化后的参数置于原函数的 `params` 参数中。


能力展示
复杂示例
--------------

我们使用一个复杂的示例来演示pre-request的魅力
Expand Down
2 changes: 1 addition & 1 deletion pre_request/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__title__ = 'pre_request'
__description__ = 'Deal with request params for Flask'
__url__ = 'https://github.com/Eastwu5788/pre-request'
__version__ = '2.0.7'
__version__ = '2.0.8'
__build__ = 0x000001
__author_email__ = 'wudong@eastwu.cn'
__license__ = 'MIT License'
Expand Down
15 changes: 8 additions & 7 deletions pre_request/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class PreRequest:
""" An object to dispatch filters to handler request params
"""

def __init__(self, content_type=None):
def __init__(self, store_key=None, content_type=None):
self.filters = simple_filters
self.complex_filters = complex_filters
self.content_type = content_type or "application/json"
self.store_key = store_key or "params"
self.response = None
self.formatter = None

Expand Down Expand Up @@ -223,8 +224,7 @@ def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
from flask import g, request # pylint: disable=import-outside-toplevel

g.params = rst = dict()
fmt_rst = dict()

# ignore with empty rule
if not rule and not options:
Expand All @@ -246,20 +246,21 @@ def wrapper(*args, **kwargs):
try:
value = self._handler_simple_filter(k, r)
# simple filter handler
rst[r.key_map if isinstance(r, Rule) and r.key_map else k] = value
fmt_rst[r.key_map if isinstance(r, Rule) and r.key_map else k] = value
except ParamsValueError as e:
return self._f_resp(e)

# use complex filter to handler params
for k, r in rules.items():
try:
self._handler_complex_filter(k, r, rst)
self._handler_complex_filter(k, r, fmt_rst)
except ParamsValueError as e:
return self._f_resp(e)

# assignment params to func args
if "params" in getfullargspec(func).args:
kwargs["params"] = rst
setattr(g, self.store_key, fmt_rst)
if self.store_key in getfullargspec(func).args:
kwargs[self.store_key] = fmt_rst

return func(*args, **kwargs)
return wrapper
Expand Down
6 changes: 6 additions & 0 deletions tests/test_filter/test_split.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# !/usr/local/python/bin/python
# -*- coding: utf-8 -*-
# (C) Wu Dong, 2020
# All rights reserved
# @Author: 'Wu Dong <wudong@eastwu.cn>'
# @Time: '2020-04-27 11:25'

0 comments on commit e17fd74

Please sign in to comment.