An application with Flask-Security makes a request to the database for the authenticated user for each route, but sometimes it’s not necessary. Here is a way how to disable it with a decorator and custom UserDatastore.
The task is to show a model from another application in Flask-Admin.
A way to do it is to overwrite
BaseModelView and get the data through HTTP request.
Here is a simple example for a sortable list with a pagination.
Another tip for Flask-Admin. The task’s requirements are:
- possibility to choose some model’s objects and download them in JSON
- possibility to upload them back
There is a ModelView property can_export, it adds an action to export in CSV or another format supported by tablib, but it does not allow to select records and there is no import. So for my task it’s not a solution.
The export is easy to do with an action decorathor.
Starting a new project is a common task in microservices architecture. To do this it’s better to have a some template. I put my version to the flask-mongoengine-bootstrap repository. The key point are:
- very basic, only flask, flask-mongoengine and structlog in requirements
- configuration through environment variables
- configured logging in JSON format
- marking log records with request_id
- possibility to run development version
make devand tests
make testthrough docker
- a template for Makefile
- examples of model, api route and tests
If you’ll use it, do not forget to change
The second line of “The Zen of Python” (try
import this) says “Explicit is better than implicit.”.
By default Flask response does not return any cache header and you can suppose that browsers will not cache, but some of them can decide for you. So it’s better to provide them, especially for API and it’s easy:
@app.after_request def set_response_headers(response): response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' response.headers['Pragma'] = 'no-cache' response.headers['Expires'] = '0' return response
The problem with translation in a Celery task is that there is no request context and
None. For Flask-Babel there is a force_locale context manager which solves this problem. But Flask-Security uses Flask-BabelEx and it does not work well with Flask-Babel.
Here is an example how to create a context manager which provides dummy request context.
Suppose there are several similar MongoEngine models and there is a need to modify them through REST. Here is an example how to create a set of such similar resources.