Fix post-merge leftovers & set DJANGO_ADMIN_URL automatically

This commit is contained in:
Nikita P. Shupeyko 2018-03-06 16:04:55 +03:00
parent 8e8105de8f
commit bd52c0b9f0
4 changed files with 84 additions and 46 deletions

View File

@ -144,6 +144,7 @@ def generate_random_string(length,
def set_flag(file_path, def set_flag(file_path,
flag, flag,
value=None, value=None,
formatted=None,
*args, *args,
**kwargs): **kwargs):
if value is None: if value is None:
@ -155,6 +156,8 @@ def set_flag(file_path,
"Please, make sure to manually {} later.".format(flag) "Please, make sure to manually {} later.".format(flag)
) )
random_string = flag random_string = flag
if formatted is not None:
random_string = formatted.format(random_string)
value = random_string value = random_string
with open(file_path, 'r+') as f: with open(file_path, 'r+') as f:
@ -170,21 +173,38 @@ def set_django_secret_key(file_path):
django_secret_key = set_flag( django_secret_key = set_flag(
file_path, file_path,
'!!!SET DJANGO_SECRET_KEY!!!', '!!!SET DJANGO_SECRET_KEY!!!',
length=50, length=64,
using_digits=True, using_digits=True,
using_ascii_letters=True using_ascii_letters=True
) )
return django_secret_key return django_secret_key
def set_django_admin_url(file_path):
django_admin_url = set_flag(
file_path,
'!!!SET DJANGO_ADMIN_URL!!!',
formatted='^{}/',
length=32,
using_digits=True,
using_ascii_letters=True
)
return django_admin_url
def generate_postgres_user():
return generate_random_string(
length=32,
using_ascii_letters=True
)
def set_postgres_user(file_path, def set_postgres_user(file_path,
value=None): value=None):
postgres_user = set_flag( postgres_user = set_flag(
file_path, file_path,
'!!!SET POSTGRES_USER!!!', '!!!SET POSTGRES_USER!!!',
value=value, value=value or generate_postgres_user()
length=8,
using_ascii_letters=True
) )
return postgres_user return postgres_user
@ -193,42 +213,46 @@ def set_postgres_password(file_path):
postgres_password = set_flag( postgres_password = set_flag(
file_path, file_path,
'!!!SET POSTGRES_PASSWORD!!!', '!!!SET POSTGRES_PASSWORD!!!',
length=42, length=64,
using_digits=True, using_digits=True,
using_ascii_letters=True using_ascii_letters=True
) )
return postgres_password return postgres_password
def initialize_dotenv(postgres_user): def append_to_gitignore_file(s) -> None:
# Initializing `env.example` first. with open(os.path.join(PROJECT_DIR_PATH, '.gitignore'), 'a') as gitignore_file:
envexample_file_path = os.path.join(PROJECT_DIR_PATH, 'env.example') gitignore_file.write(s)
set_django_secret_key(envexample_file_path) gitignore_file.write(os.linesep)
set_postgres_user(envexample_file_path, value=postgres_user)
set_postgres_password(envexample_file_path)
# Renaming `env.example` to `.env`.
dotenv_file_path = os.path.join(PROJECT_DIR_PATH, '.env')
shutil.move(envexample_file_path, dotenv_file_path)
def initialize_localyml(postgres_user): def initialize_envs(postgres_user):
set_postgres_user(os.path.join(PROJECT_DIR_PATH, 'local.yml'), value=postgres_user) envs_dir_path = '.envs'
append_to_gitignore_file(envs_dir_path + '/**/*')
local_postgres_envs_path = os.path.join(PROJECT_DIR_PATH, envs_dir_path, '.local', '.postgres')
set_postgres_user(local_postgres_envs_path, value=postgres_user)
set_postgres_password(local_postgres_envs_path)
production_django_envs_path = os.path.join(PROJECT_DIR_PATH, envs_dir_path, '.production', '.django')
set_django_secret_key(production_django_envs_path)
set_django_admin_url(production_django_envs_path)
production_postgres_envs_path = os.path.join(PROJECT_DIR_PATH, envs_dir_path, '.production', '.postgres')
set_postgres_user(production_postgres_envs_path, value=postgres_user)
set_postgres_password(production_postgres_envs_path)
def initialize_local_settings(): def initialize_settings_files():
set_django_secret_key(os.path.join(PROJECT_DIR_PATH, 'config', 'settings', 'local.py')) set_django_secret_key(os.path.join(PROJECT_DIR_PATH, 'config', 'settings', 'local.py'))
def initialize_test_settings():
set_django_secret_key(os.path.join(PROJECT_DIR_PATH, 'config', 'settings', 'test.py')) set_django_secret_key(os.path.join(PROJECT_DIR_PATH, 'config', 'settings', 'test.py'))
def main(): def main():
postgres_user = generate_random_string(length=16, using_ascii_letters=True) postgres_user = generate_postgres_user()
initialize_dotenv(postgres_user) initialize_envs(postgres_user)
initialize_localyml(postgres_user) initialize_settings_files()
initialize_local_settings()
initialize_test_settings()
if '{{ cookiecutter.open_source_license }}' == 'Not open source': if '{{ cookiecutter.open_source_license }}' == 'Not open source':
remove_open_source_project_only_files() remove_open_source_project_only_files()

View File

@ -1,2 +1,2 @@
POSTGRES_USER={{cookiecutter.project_slug}} POSTGRES_USER=!!!SET POSTGRES_USER!!!
POSTGRES_PASSWORD=!!!SET POSTGRES_PASSWORD!!! POSTGRES_PASSWORD=!!!SET POSTGRES_PASSWORD!!!

View File

@ -1,31 +1,43 @@
CONN_MAX_AGE= # General
# ------------------------------------------------------------------------------
# DJANGO_READ_DOT_ENV_FILE=True # DJANGO_READ_DOT_ENV_FILE=True
DJANGO_SETTINGS_MODULE=config.settings.production DJANGO_SETTINGS_MODULE=config.settings.production
DJANGO_SECRET_KEY=!!!SET DJANGO_SECRET_KEY!!! DJANGO_SECRET_KEY=!!!SET DJANGO_SECRET_KEY!!!
DJANGO_ADMIN_URL=!!!SET DJANGO_ADMIN_URL!!!
DJANGO_ALLOWED_HOSTS=.{{ cookiecutter.domain_name }} DJANGO_ALLOWED_HOSTS=.{{ cookiecutter.domain_name }}
DJANGO_ADMIN_URL= # Security
DJANGO_ACCOUNT_ALLOW_REGISTRATION=True # ------------------------------------------------------------------------------
{% if cookiecutter.use_compressor == 'y' -%} # TIP: better off using DNS, however, redirect is OK too
COMPRESS_ENABLED= DJANGO_SECURE_SSL_REDIRECT=False
{% endif %}
{% if cookiecutter.use_sentry_for_error_reporting == 'y' -%} # Email
# ------------------------------------------------------------------------------
MAILGUN_API_KEY=
DJANGO_SERVER_EMAIL=
MAILGUN_DOMAIN=
# AWS
# ------------------------------------------------------------------------------
DJANGO_AWS_ACCESS_KEY_ID=
DJANGO_AWS_SECRET_ACCESS_KEY=
DJANGO_AWS_STORAGE_BUCKET_NAME=
# django-allauth
# ------------------------------------------------------------------------------
DJANGO_ACCOUNT_ALLOW_REGISTRATION=True
{% if cookiecutter.use_compressor == 'y' %}
# django-compressor
# ------------------------------------------------------------------------------
COMPRESS_ENABLED=
{% endif %}{% if cookiecutter.use_sentry_for_error_reporting == 'y' %}
# Sentry
# ------------------------------------------------------------------------------
DJANGO_SENTRY_DSN= DJANGO_SENTRY_DSN=
{% endif %} {% endif %}{% if cookiecutter.use_opbeat == 'y' %}
{% if cookiecutter.use_opbeat == 'y' -%} # opbeat
# ------------------------------------------------------------------------------
DJANGO_OPBEAT_ORGANIZATION_ID= DJANGO_OPBEAT_ORGANIZATION_ID=
DJANGO_OPBEAT_APP_ID= DJANGO_OPBEAT_APP_ID=
DJANGO_OPBEAT_SECRET_TOKEN= DJANGO_OPBEAT_SECRET_TOKEN=
{% endif %} {% endif %}
DJANGO_MAILGUN_API_KEY=
DJANGO_SERVER_EMAIL=
MAILGUN_SENDER_DOMAIN=
# Major security concern! You'd be better off using DNS.
DJANGO_SECURE_SSL_REDIRECT=False
DJANGO_AWS_ACCESS_KEY_ID=
DJANGO_AWS_SECRET_ACCESS_KEY=
DJANGO_AWS_STORAGE_BUCKET_NAME=

View File

@ -348,3 +348,5 @@ mailhog
# See issue https://github.com/pydanny/cookiecutter-django/issues/1321 # See issue https://github.com/pydanny/cookiecutter-django/issues/1321
!/compose/local/ !/compose/local/
{% endif %} {% endif %}
.env