in tasks.py [0:0]
def create_env_file(env_file):
"""Create or update the .env file"""
with open(env_file, "r") as f:
env_vars = f.read()
# update the DATABASE_URL env
new_db_url = "DATABASE_URL=postgres://postgres@localhost:5432/pulse"
old_db_url = re.search("DATABASE_URL=.*", env_vars)
if old_db_url:
env_vars = env_vars.replace(old_db_url.group(0), new_db_url)
else:
env_vars = env_vars + "DATABASE_URL=postgres://postgres@localhost:5432/pulse\n"
# update the ALLOWED_HOSTS env
new_hosts = "ALLOWED_HOSTS=*"
old_hosts = re.search("ALLOWED_HOSTS=.*", env_vars)
if old_hosts:
env_vars = env_vars.replace(old_hosts.group(0), new_hosts)
else:
env_vars = env_vars + "ALLOWED_HOSTS=*\n"
# create the new env file
with open(".env", "w") as f:
f.write(env_vars)