Added cookie consent

This commit is contained in:
Vivek Santayana 2022-06-16 13:22:06 +01:00
parent e4e07c43b4
commit 5c8435d39e
5 changed files with 18 additions and 8 deletions

View File

@ -152,7 +152,7 @@ $('#dismiss-cookie-alert').click(function(event){
$.ajax({ $.ajax({
url: '/cookies/', url: '/cookies/',
type: 'GET', type: 'POST',
data: { data: {
time: Date.now() time: Date.now()
}, },

View File

@ -9,6 +9,7 @@ class Config(object):
DEBUG = False DEBUG = False
TESTING = False TESTING = False
SECRET_KEY = os.getenv('SECRET_KEY') SECRET_KEY = os.getenv('SECRET_KEY')
SERVER_NAME = os.getenv('SERVER_NAME')
SESSION_COOKIE_SECURE = True SESSION_COOKIE_SECURE = True
SQLALCHEMY_DATABASE_URI = f'sqlite:///{Path(DATA)}/database.db' SQLALCHEMY_DATABASE_URI = f'sqlite:///{Path(DATA)}/database.db'
SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_TRACK_MODIFICATIONS = False

View File

@ -68,7 +68,7 @@ $('#dismiss-cookie-alert').click(function(event){
$.ajax({ $.ajax({
url: '/cookies/', url: '/cookies/',
type: 'GET', type: 'POST',
data: { data: {
time: Date.now() time: Date.now()
}, },

View File

@ -5,7 +5,7 @@ from flask import Blueprint, redirect, request, render_template
from datetime import datetime, timedelta from datetime import datetime, timedelta
views = Blueprint( views = Blueprint(
name='common', name='views',
import_name=__name__, import_name=__name__,
template_folder='templates', template_folder='templates',
static_folder='static' static_folder='static'
@ -15,7 +15,7 @@ views = Blueprint(
def _privacy(): def _privacy():
return render_template('privacy.html') return render_template('privacy.html')
@views.route('/cookie_consent/') @views.route('/cookies/', methods=['POST'])
def _cookie_consent(): def _cookie_consent():
resp = redirect('/') resp = redirect('/')
resp.set_cookie( resp.set_cookie(
@ -24,7 +24,7 @@ def _cookie_consent():
max_age = timedelta(days=14) if request.cookies.get('remember') == 'True' else None, max_age = timedelta(days=14) if request.cookies.get('remember') == 'True' else None,
path = '/', path = '/',
expires = datetime.utcnow() + timedelta(days=14) if request.cookies.get('remember') else None, expires = datetime.utcnow() + timedelta(days=14) if request.cookies.get('remember') else None,
domain = f'.{Config.SERVER_NAME}', domain = f'{Config.SERVER_NAME}',
secure = True secure = True
) )
return resp return resp

View File

@ -2,9 +2,10 @@ from app.models import User
from app.modules import bootstrap, csrf, db, login_manager, mail from app.modules import bootstrap, csrf, db, login_manager, mail
from config import Config from config import Config
from flask import Flask from flask import flash, Flask, request
from flask_wtf.csrf import CSRFError from flask.helpers import url_for
from flask.json import jsonify from flask.json import jsonify
from flask_wtf.csrf import CSRFError
from werkzeug.middleware.proxy_fix import ProxyFix from werkzeug.middleware.proxy_fix import ProxyFix
from datetime import datetime from datetime import datetime
@ -25,6 +26,14 @@ def create_app():
def _load_user(id): def _load_user(id):
return User.query.filter_by(id=id).first() return User.query.filter_by(id=id).first()
@app.before_request
def _check_cookie_consent():
if request.cookies.get('cookie_consent'):
return
if any([ request.path.startswith(x) for x in [ '/admin/static/', '/static/', '/cookies/' ] ]):
return
flash(f'<strong>Cookie Consent</strong>: This web site only stores minimal, functional cookies. It does not store any tracking information. By using this site, you consent to this use of cookies. For more information, see our <a href="{url_for("views._privacy")}">privacy policy</a>.', 'cookie_alert')
@app.errorhandler(404) @app.errorhandler(404)
def _404_handler(error): def _404_handler(error):
return jsonify({'error':'404 &mdash; Not Found'}), 404 return jsonify({'error':'404 &mdash; Not Found'}), 404
@ -37,8 +46,8 @@ def create_app():
from app.admin.views import admin from app.admin.views import admin
from app.api.views import api from app.api.views import api
from app.views import views
from app.quiz.views import quiz from app.quiz.views import quiz
from app.views import views
app.register_blueprint(admin, url_prefix='/admin') app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(api, url_prefix='/api') app.register_blueprint(api, url_prefix='/api')