mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2025-08-18 10:54:51 +03:00
setup .envs according to database
This commit is contained in:
parent
46ab64b4b9
commit
1594cc6924
|
@ -217,25 +217,51 @@ def generate_random_user():
|
|||
return generate_random_string(length=32, using_ascii_letters=True)
|
||||
|
||||
|
||||
def generate_postgres_user(debug=False):
|
||||
def generate_database_user(debug=False):
|
||||
return DEBUG_VALUE if debug else generate_random_user()
|
||||
|
||||
|
||||
def set_postgres_user(file_path, value):
|
||||
postgres_user = set_flag(file_path, "!!!SET POSTGRES_USER!!!", value=value)
|
||||
return postgres_user
|
||||
|
||||
|
||||
def set_postgres_password(file_path, value=None):
|
||||
postgres_password = set_flag(
|
||||
def set_database_user(file_path: str, value: str, database_engine: str):
|
||||
database_user = set_flag(
|
||||
file_path,
|
||||
"!!!SET POSTGRES_PASSWORD!!!",
|
||||
f"!!!SET {database_engine.upper()}_USER!!!",
|
||||
value=value)
|
||||
return database_user
|
||||
|
||||
|
||||
def set_database_password(file_path: str, database_engine: str, value: str | None=None):
|
||||
database_password = set_flag(
|
||||
file_path,
|
||||
f"!!!SET {database_engine.upper()}_USER!!!",
|
||||
value=value,
|
||||
length=64,
|
||||
using_digits=True,
|
||||
using_ascii_letters=True,
|
||||
)
|
||||
return postgres_password
|
||||
return database_password
|
||||
|
||||
|
||||
def get_database_env_path(env: str, database_engine: str):
|
||||
local_postgres_envs_path = os.path.join(".envs", ".local", ".postgres")
|
||||
production_postgres_envs_path = os.path.join(".envs", ".production", ".postgres")
|
||||
local_mysql_envs_path = os.path.join(".envs", ".local", ".mysql")
|
||||
production_mysql_envs_path = os.path.join(".envs", ".production", ".mysql")
|
||||
|
||||
is_mysql = database_engine == "mysql"
|
||||
is_postgres = database_engine == "postgresql"
|
||||
|
||||
if env == "local":
|
||||
if is_mysql:
|
||||
return local_mysql_envs_path
|
||||
if is_postgres:
|
||||
return local_postgres_envs_path
|
||||
if env == "prod":
|
||||
if is_mysql:
|
||||
return production_mysql_envs_path
|
||||
if is_postgres:
|
||||
return production_postgres_envs_path
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def set_celery_flower_user(file_path, value):
|
||||
|
@ -263,22 +289,35 @@ def append_to_gitignore_file(ignored_line):
|
|||
gitignore_file.write("\n")
|
||||
|
||||
|
||||
def set_flags_in_envs(postgres_user, celery_flower_user, debug=False):
|
||||
def set_flags_in_envs(database_user, celery_flower_user, debug=False):
|
||||
local_django_envs_path = os.path.join(".envs", ".local", ".django")
|
||||
production_django_envs_path = os.path.join(".envs", ".production", ".django")
|
||||
local_postgres_envs_path = os.path.join(".envs", ".local", ".postgres")
|
||||
production_postgres_envs_path = os.path.join(".envs", ".production", ".postgres")
|
||||
|
||||
selected_database = "{{ cookiecutter.database_engine }}"
|
||||
|
||||
set_django_secret_key(production_django_envs_path)
|
||||
set_django_admin_url(production_django_envs_path)
|
||||
|
||||
set_postgres_user(local_postgres_envs_path, value=postgres_user)
|
||||
set_postgres_password(
|
||||
local_postgres_envs_path, value=DEBUG_VALUE if debug else None
|
||||
set_database_user(
|
||||
get_database_env_path(env="local", database_engine=selected_database),
|
||||
value=database_user,
|
||||
database_engine=selected_database
|
||||
)
|
||||
set_postgres_user(production_postgres_envs_path, value=postgres_user)
|
||||
set_postgres_password(
|
||||
production_postgres_envs_path, value=DEBUG_VALUE if debug else None
|
||||
set_database_password(
|
||||
get_database_env_path(env="local", database_engine=selected_database),
|
||||
database_engine=selected_database,
|
||||
value=DEBUG_VALUE if debug else None
|
||||
)
|
||||
|
||||
set_database_user(
|
||||
get_database_env_path(env="prod", database_engine=selected_database),
|
||||
value=database_user,
|
||||
database_engine=selected_database
|
||||
)
|
||||
set_database_password(
|
||||
get_database_env_path(env="prod", database_engine=selected_database),
|
||||
database_engine=selected_database,
|
||||
value=DEBUG_VALUE if debug else None
|
||||
)
|
||||
|
||||
set_celery_flower_user(local_django_envs_path, value=celery_flower_user)
|
||||
|
|
Loading…
Reference in New Issue
Block a user