joke2k / django-environ

Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.
https://django-environ.rtfd.org
MIT License
3.02k stars 315 forks source link

env.list() does not do strip ? #345

Closed kristinaDek closed 3 years ago

kristinaDek commented 3 years ago

Why does env.list() not doing stip also?

sergeyklay commented 3 years ago

Could you please provide a bit more info and, if possible the code to reproduce.

kristinaDek commented 3 years ago

I will use example from https://django-environ.readthedocs.io/en/latest/tips.html#nested-lists: '.env' file: DJANGO_ADMINS=Blake:blake@cyb.org, Alice:alice@cyb.org,Juls:juls@cyb.org if we do: ADMINS = env.list('DJANGO_ADMINS') ADMINS will look like: ['Blake:blake@cyb.org', ' Alice:alice@cyb.org', 'Juls:juls@cyb.org'] so you must take care when entering values (mind on space), env.list() will not strip it automatically

sergeyklay commented 3 years ago

There are many approaches to getting a list of already processed email addresses without making a change to django-environ library. For example:

# Environment file

DJANGO_ADMINS="Blake <blake@cyb.org>, Alice Judge <alice@cyb.org>"
# settings.py file

from email.utils import parseaddr

from environ import environ

env = environ.Env(
    # VAR=(casting, default value)
    DJANGO_ADMINS=(list, []),
)
env.read_env(BASE_DIR / '.env')

def _parse_emails(environment_var):
    """Prepare a tuple of email tuples from the value of 'environment_var'."""
    return tuple(parseaddr(email) for email in env(environment_var))

# A list of all the people who get code error notifications.
ADMINS = _parse_emails('DJANGO_ADMINS')

print(ADMINS)
# (('Blake', 'blake@cyb.org'), ('Alice Judge', 'alice@cyb.org'))

Sorry, but I'm not sure if we should include this into the library codebase. Instead of addresses, there may be something else where removing leading whitespace can be harmful.