One of the goals for 2017 year was to reanimate the blog and write 52 posts, one per week.
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
Flask-Admin provides the examples of authentication with Flask-Login and Flask-Security. Here is an example how to authenticate using Microsoft oAuth.
Create an application in Microsoft Application Registration Portal.
Recently I moved some of the sites to a new hosting. The previous server had Ubuntu 12.04 with Exim4 and I forgot how to configure them.
On a new server I installed Postfix with Ansible:
- name: Set up Postfix to relay mail
debconf: name=postfix
question='{{ item.question }}'
value='{{ item.value }}'
vtype='{{ item.vtype }}'
with_items:
- { question: 'postfix/mailname', value: '{{ ansible_fqdn }}', vtype: 'string' }
- { question: 'postfix/main_mailer_type', value: 'Internet Site', vtype: 'string' }
After that I started to receive the next delivery reports from Gmail:
The problem with translation in a Celery task is that there is no request context and flask_babelex.get_locale returns 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.
There is a way to add an email to Todoist by sending it to a special address, the only thing that annoing me, that it doesn’t keep a link to the message in Mail.app.
I created a simple Alfred workflow, which takes the selected messages in Mail.app, creates a Markdown links as [email subject](message://message_id) and send it to Todoist Inbox using its API.
You can find the details about installation and usage in sneawo/mailapp-to-todoist repository.
Socket.IO requires the sticky sessions, so when it’s runned in Docker Swarm environment, the Swarm load balancer can forward the request to any node where the service is launched and there will be the 400 errors. The solution is to limit the number of nodes for Socket.IO service to one or to use another load balancer like Traefik or HAProxy.
Sometimes during development it’s necessary to share the db dump with colleagues. In the previous article i provided the commands for MySQL, here is the same for PostgreSQL.
DATE = $(shell date +%Y-%m-%d)
path ?= .
help: ## show this help
@echo 'usage: make [target] ...'
@echo ''
@echo 'targets:'
@egrep '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sed 's/:.*##/#/' | column -t -c 2 -s '#'
dump: ## dump db, usage 'make dump path=/path/to/dumps'
docker-compose exec --user postgres db pg_dumpall --clean | gzip > $(path)/project-$(DATE).sql.gz
restore: ## restore db from dump file, usage 'make restore dump=dump.sql.gz'
docker-compose stop web
gunzip -c $(dump) | docker exec -i --user postgres `docker-compose ps -q db` psql
docker-compose start web
There are many instructions how to run Django in Docker. The base configuration of docker-compose usualy has the next services: nginx, web (django), db (mysql, postgresql), cache (memcached, redis). I’ll just add some notes.
© 2026 Andrey Zhukov's Tech Blog.