Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
prozorro-sale
Cors
Commits
6985bacd
Commit
6985bacd
authored
Feb 01, 2022
by
Viacheslav Sukhovieiev
Browse files
feat: add handler decorator logic
parent
0225f1f8
Pipeline
#35667
passed with stages
in 1 minute and 46 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/prozorro_sale/cors/__init__.py
View file @
6985bacd
...
...
@@ -15,6 +15,7 @@
"""CORS support for aiohttp.
"""
import
functools
from
typing
import
Mapping
,
Union
,
Any
from
aiohttp
import
web
...
...
@@ -65,3 +66,39 @@ def setup(app: web.Application, *,
cors
=
CorsConfig
(
app
,
defaults
=
defaults
)
app
[
APP_CONFIG_KEY
]
=
cors
return
cors
def
get_base_config
()
->
dict
:
"""
returns base CORS config
{
"*": ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
allow_methods="*",
)
}
"""
return
{
"*"
:
ResourceOptions
(
allow_credentials
=
True
,
expose_headers
=
"*"
,
allow_headers
=
"*"
,
allow_methods
=
"*"
,
)
}
def
custom_handler
(
config
=
None
,
defaults
=
False
):
if
not
config
and
defaults
is
True
:
config
=
get_base_config
()
def
wrapper
(
func
):
@
functools
.
wraps
(
func
)
def
handler
(
*
args
,
**
kwargs
):
return
func
(
*
args
,
**
kwargs
)
handler
.
apply_cors
=
True
handler
.
cors_config
=
config
return
handler
return
wrapper
src/prozorro_sale/cors/cors_config.py
View file @
6985bacd
...
...
@@ -261,3 +261,12 @@ class CorsConfig:
stacklevel
=
2
)
return
self
.
_cors_impl
.
add
(
routing_entity
,
config
)
def
apply
(
self
,
app
:
web
.
Application
):
for
route
in
list
(
app
.
router
.
routes
()):
if
hasattr
(
route
.
handler
,
"apply_cors"
):
if
hasattr
(
route
.
handler
,
"cors_config"
):
config
=
route
.
handler
.
cors_config
else
:
config
=
None
self
.
add
(
route
,
config
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment