Handle user (cookie) consent in Flask projects
Project description
Flask-Consent
About
Flask-Consent is a Flask extension that helps you handle user (cookie) consent in Flask projects.
Installation
Simply run:
pip install Flask-Consent
Usage
The most basic usage:
from flask import Flask
from flask_consent import Consent
app = Flask(__name__)
app.config['CONSENT_FULL_TEMPLATE'] = 'consent.html'
app.config['CONSENT_BANNER_TEMPLATE'] = 'consent_banner.html'
consent = Consent(app)
consent.add_standard_categories()
And add this somewhere in your Jinja2 templates: {{ flask_consent_code() }}
The add_standard_categories()
adds three common categories of consent: Required, Preferences and Analytics.
If you want to use your own you can simply replace that call by calls to add_category()
.
Use request.consent
in order to act based on the given consent. For example:
from flask import request
if request.consent['required']:
pass
Multiple domains
This package actually supports sites that are present on multiple top-level domains. Since it's not possible to set a single cookie for them this extension instead does AJAX calls to a "primary" domain in order to synchronize the state between the domains and prevent having to show the user an annoying banner multiple times. To enable this simply add the following code:
@consent.domain_loader
def domain_loader():
return ['primary.tld', 'secondary.tld', 'extra.tld']
The primary domain used is determined using the CONSENT_PRIMARY_SERVERNAME
configuration option,
which by default is set to SERVER_NAME
.
Configuration
Option | Default | Description |
---|---|---|
CONSENT_FULL_TEMPLATE |
None | The template that renders the full consent page |
CONSENT_BANNER_TEMPLATE |
None | The template that renders the consent banner |
CONSENT_CONTACT_MAIL |
None | An e-mail adress that users can send questions regarding consent to |
CONSENT_COOKIE_NAME |
_consent | The name of the cookie that stores the consent given |
CONSENT_VALID_FOR_MONTHS |
12 | The number of months we wait before asking for consent again |
CONSENT_PRIMARY_SERVERNAME |
SERVER_NAME |
The primary domain name, used for multi-domain deployments |
CONSENT_PATH |
/consent |
The path used both for accessing consent information and for AJAX calls |
Templates
The templates gets access to the variables flask_consent_categories
(a list fo the categories) and flask_consent_contact_mail
(populated from the similarly named configuration option).
Somewhere in the template you will usually be adding a set of checkboxes:
<input type="checkbox" id="category_{{ category.name }}"
{% if category.default %}checked="checked"{% endif %}
{% if category.is_required %}disabled="disabled"{% endif %}
name="flask_consent_category" value="{{ category.name }}"/>
<label for="category_{{ category.name }}">{{ category.title }}</label>
Note: The name="flask_consent_category"
should not be changed, as it is used internally.
(only use category.default
in the banner template, in the full template you should replace it by request.consent[category]
)
Development and Testing
- Get the code:
git clone https://github.com/02JanDal/Flask-Consent.git
- Do your changes
- Test the result:
tox -e py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Flask-Consent-0.0.3.tar.gz
.
File metadata
- Download URL: Flask-Consent-0.0.3.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9aa4224917cc2031e396d880194fa0c159e96938e992a0270c137ddd6284fe1e |
|
MD5 | 78ed224b02fa0710f2e4b86c3ebab980 |
|
BLAKE2b-256 | f17012d1310e0a3bcd3930dd30a80419d25a2ac151e37fd74e190d3434af08ec |
File details
Details for the file Flask_Consent-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: Flask_Consent-0.0.3-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f17986cb8be349ab6c4787436f9fa328ab2c539d6527efa3637625b07a8b34d4 |
|
MD5 | beb224f5e723003b752da977017f6108 |
|
BLAKE2b-256 | 3b9e6061b1277a457dd0dae5fb6caf688becdd2cfae96ef165bc727a73c91855 |