-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core: Flask-Utils is now an extension #26
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
flask>=2.2.0 | ||
Pallets-Sphinx-Themes | ||
sphinx==7.1.2 | ||
sphinx-notfound-page | ||
sphinx-rtd-theme==1.3.0rc1 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from typing import Optional | ||
|
||
from flask import Flask | ||
|
||
from flask_utils.errors import _register_error_handlers | ||
|
||
|
||
class FlaskUtils(object): | ||
""" | ||
FlaskUtils extension class. | ||
|
||
This class currently optionally register the custom error handlers found in :mod:`flask_utils.errors`. | ||
Call :meth:`init_app` to configure the extension on an application. | ||
|
||
:param app: Flask application instance. | ||
:param register_error_handlers: Register the custom error handlers. Default is True. | ||
|
||
:Example: | ||
|
||
.. code-block:: python | ||
|
||
from flask import Flask | ||
from flask_utils import FlaskUtils | ||
|
||
app = Flask(__name__) | ||
fu = FlaskUtils(app) | ||
|
||
# or | ||
|
||
fu = FlaskUtils() | ||
fu.init_app(app) | ||
|
||
.. versionadded:: 0.5.0 | ||
""" | ||
|
||
def __init__(self, app: Optional[Flask] = None, register_error_handlers: bool = True): | ||
if app is not None: | ||
self.init_app(app, register_error_handlers) | ||
|
||
def init_app(self, app: Flask, register_error_handlers: bool = True): | ||
"""Initialize a Flask application for use with this extension instance. This | ||
must be called before any request is handled by the application. | ||
|
||
If the app is created with the factory pattern, this should be called after the app | ||
is created to configure the extension. | ||
|
||
If `register_error_handlers` is True, the custom error handlers will be registered and | ||
can then be used in routes to raise errors. | ||
|
||
:param app: The Flask application to initialize. | ||
:param register_error_handlers: Register the custom error handlers. Default is True. | ||
|
||
:Example: | ||
|
||
.. code-block:: python | ||
|
||
from flask import Flask | ||
from flask_utils import FlaskUtils | ||
|
||
app = Flask(__name__) | ||
fu = FlaskUtils() | ||
fu.init_app(app) | ||
|
||
.. versionadded:: 0.5.0 | ||
""" | ||
if register_error_handlers: | ||
_register_error_handlers(app) | ||
|
||
app.extensions["flask_utils"] = self |
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
File renamed without changes.
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,49 @@ | ||
from flask import Flask | ||
|
||
from flask_utils import BadRequestError | ||
from flask_utils import FlaskUtils | ||
|
||
|
||
class TestExtension: | ||
def test_init_app(self): | ||
app = Flask(__name__) | ||
assert "flask_utils" not in app.extensions | ||
fu = FlaskUtils() | ||
|
||
fu.init_app(app) | ||
assert "flask_utils" in app.extensions | ||
|
||
def test_normal_instantiation(self): | ||
app = Flask(__name__) | ||
|
||
assert "flask_utils" not in app.extensions | ||
|
||
FlaskUtils(app) | ||
|
||
assert "flask_utils" in app.extensions | ||
|
||
def test_error_handlers_not_registered(self): | ||
app = Flask(__name__) | ||
|
||
FlaskUtils(app, register_error_handlers=False) | ||
|
||
@app.route("/") | ||
def index(): | ||
raise BadRequestError("Bad Request") | ||
|
||
with app.test_client() as client: | ||
response = client.get("/") | ||
assert response.status_code == 500 | ||
|
||
def test_error_handlers_registered(self): | ||
app = Flask(__name__) | ||
|
||
FlaskUtils(app, register_error_handlers=True) | ||
|
||
@app.route("/") | ||
def index(): | ||
raise BadRequestError("Bad Request") | ||
|
||
with app.test_client() as client: | ||
response = client.get("/") | ||
assert response.status_code == 400 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove redundant inheritance from
object
.Committable suggestion
Tools
Ruff