Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty cookie_consent value after calling /cookies/accept/ #128

Open
1 task
frollow opened this issue Jun 29, 2024 · 0 comments
Open
1 task

Empty cookie_consent value after calling /cookies/accept/ #128

frollow opened this issue Jun 29, 2024 · 0 comments

Comments

@frollow
Copy link

frollow commented Jun 29, 2024

dic = {'unclassified': '', 'marketing': '', 'statistic': ''}

def dict_to_cookie_str(dic):
    return "|".join(["%s=%s" % (k, v) for k, v in dic.items() if v]

The result is that the function returns the empty string because the if v condition in the list generator excludes any key-value pairs where the value is the empty string.

The issue arises when /cookies/accept/ is called, and the cookie_consent remains empty. As a result, we can never set parameters for /cookies/accept/.

The problem with your dict_to_cookie_str(dic) function is that the values in your dictionary dic are empty (empty strings). Consequently, the function returns an empty string because the condition if v in the list comprehension excludes any key-value pairs where the value is an empty string.

How the function works:

  • %s=%s % (k, v): This part formats each key-value pair into a string, for example, key=value.
  • for k, v in dic.items(): This iterates over each key-value pair in the dictionary dic.
  • if v: This condition checks whether the value v is truthy. In Python, an empty string ('') evaluates to False, so if the value is an empty string, it will not be included in the final list.

Example:

For your dictionary dic = {'unclassified': '', 'marketing': '', 'statistic': ''}, each value is an empty string, so the condition if v is not satisfied for any element, and the list comprehension returns no elements. As a result, the function returns an empty string after executing the join() method.

Solutions:

  1. Change the condition: If you want to include all keys in the resulting string, regardless of whether their values are empty, remove the if v:
def dict_to_cookie_str(dic):
    return "|".join(["%s=%s" % (k, v) for k, v in dic.items()])
  1. Data correction: If empty values should be excluded, ensure that your dictionary contains significant (non-empty) values before calling the function. For example:
dic = {'unclassified': '1', 'marketing': '1', 'statistic': '1'}

Tasks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant