如何在Flask中获得用户对cookie的许可

时间:2021-12-19 01:49:54

I have a website built with Flask and Flask-Login, so I know it generates cookies at some point. But my question is more basic than this.

我有一个使用Flask和Flask-Login构建的网站,所以我知道它会在某些时候生成cookie。但我的问题比这更基本。

Does a Flask application always, and immediately, generate cookies?

If no, are there any tools to allow me to identify which part of my application does generate cookies at which point, for example is it possible to monitor Chrome Dev tools when operating the website?

如果不是,是否有任何工具可以让我确定我的应用程序的哪个部分确实生成了cookie,例如在操作网站时是否可以监控Chrome Dev工具?

The context for this question is GDPR compliance and specifically trying to request permission at the start (without initially creating a cookie!) and then when the user clicks accept have a process that means that user (anonymous or otherwise) won't see that permission request banner again (until local browser cache clearing)

此问题的上下文是GDPR合规性,特别是尝试在开始时请求权限(最初没有创建cookie!)然后当用户单击“接受”时有一个进程,表示用户(匿名或其他方式)将不会看到该权限再次请求横幅(直到本地浏览器缓存清除)

1 个解决方案

#1


0  

For what its worth I did this:

我为此付出了多少代价:

1) Mandate a banner on any page base.html:

1)在任何页面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 %}

2) Inject the function into jijna2 to check the cookies:

2)将功能注入jijna2以检查cookie:

@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

I also used the dev console to detect existing cookies by exploring document.cookies. It seemed the only cookies initially generated were Google Analytics.

我还使用开发控制台通过探索document.cookies来检测现有的cookie。最初生成的唯一Cookie似乎是Google Analytics。

#1


0  

For what its worth I did this:

我为此付出了多少代价:

1) Mandate a banner on any page base.html:

1)在任何页面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 %}

2) Inject the function into jijna2 to check the cookies:

2)将功能注入jijna2以检查cookie:

@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

I also used the dev console to detect existing cookies by exploring document.cookies. It seemed the only cookies initially generated were Google Analytics.

我还使用开发控制台通过探索document.cookies来检测现有的cookie。最初生成的唯一Cookie似乎是Google Analytics。