Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-web
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jayesh
swh-web
Commits
ad9eafcd
Commit
ad9eafcd
authored
9 years ago
by
Antoine R. Dumont
Browse files
Options
Downloads
Patches
Plain Diff
Add jsonp decorator
CLOSE T110 source:
http://flask.pocoo.org/snippets/79/
parent
c54fdc8c
No related branches found
Branches containing commit
Tags
v0.0.5
v0.0.6
v0.0.7
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swh/web/ui/controller.py
+2
-0
2 additions, 0 deletions
swh/web/ui/controller.py
swh/web/ui/decorators.py
+32
-0
32 additions, 0 deletions
swh/web/ui/decorators.py
with
34 additions
and
0 deletions
swh/web/ui/controller.py
+
2
−
0
View file @
ad9eafcd
...
...
@@ -13,6 +13,7 @@ from flask import make_response
from
swh.core.hashutil
import
ALGORITHMS
from
swh.web.ui.main
import
app
from
swh.web.ui
import
service
,
query
from
swh.web.ui.decorators
import
jsonp
hash_filter_keys
=
ALGORITHMS
...
...
@@ -287,6 +288,7 @@ def revision_at_origin(timestamp, origin_type, origin_url):
@app.route
(
'
/api/1/stat/counters
'
)
@jsonp
def
api_stats
():
"""
Return statistics as a JSON object
"""
return
jsonify
(
service
.
stat_counters
())
...
...
This diff is collapsed.
Click to expand it.
swh/web/ui/decorators.py
0 → 100644
+
32
−
0
View file @
ad9eafcd
# Copyright (C) 2015 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information
from
functools
import
wraps
from
flask
import
request
,
current_app
def
jsonp
(
func
):
"""
Wraps JSONified output for JSONP requests.
"""
@wraps
(
func
)
def
decorated_function
(
*
args
,
**
kwargs
):
callback
=
request
.
args
.
get
(
'
callback
'
,
False
)
if
callback
:
res
=
func
(
*
args
,
**
kwargs
)
if
hasattr
(
res
,
'
data
'
):
# for a response object, data
data
=
res
.
data
if
isinstance
(
data
,
bytes
):
# we're dealing in utf-8 bytes
data
=
data
.
decode
(
'
utf-8
'
)
else
:
data
=
str
(
data
)
else
:
# fallback case...
data
=
str
(
res
)
content
=
''
.
join
([
str
(
callback
),
'
(
'
,
data
,
'
)
'
])
return
current_app
.
response_class
(
content
,
mimetype
=
'
application/javascript
'
)
return
func
(
*
args
,
**
kwargs
)
return
decorated_function
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment