Skip to content

Instantly share code, notes, and snippets.

@goodtiding5
Created June 18, 2019 07:33
Show Gist options
  • Save goodtiding5/8c620b963943b9d5a539239ed2a792a3 to your computer and use it in GitHub Desktop.
Save goodtiding5/8c620b963943b9d5a539239ed2a792a3 to your computer and use it in GitHub Desktop.
Flask: permission for cookies
  1. Mandate a banner on any page base.html
{% if cookies_check() %}
        {# then user has already consented so no requirement for consent banner #}
{% else %}
        {# show a cookie consent banner #}
        <div id="cookie-consent-container">
            <button id="cookie-consent">I Consent</button>
        </div>
        <script>
            var fn = function () {
                document.cookie = "cookie_consent=true";
                document.getElementById('cookie-consent-container').hidden = true;
            };
            document.getElementById('cookie-consent').onclick = fn;
        </script>
{% endif %}
  1. Inject the function into jijna2 to check the cookies
@app.app_context_processor
def inject_template_scope():
    injections = dict()

    def cookies_check():
        value = request.cookies.get('cookie_consent')
        return value == 'true'
    injections.update(cookies_check=cookies_check)

    return injections
@sorenwacker
Copy link

It works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment