From 5bae08596e9ff1c55c4b7f4048983139776a8116 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Mon, 14 Sep 2015 18:55:41 -0700 Subject: [PATCH] fixed #327 - post_gen_hook now deletes taskapp if celery isn't going to be used --- CHANGELOG.md | 3 ++ hooks/post_gen_project.py | 61 +++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efe2455b..397fa320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All enhancements and patches to cookiecutter-django will be documented in this f This project adheres to [Semantic Versioning](http://semver.org/). ## [2015-09-14] +### Added +- Functionality to delete taskapp if celery isn't going to be used (@pydanny) + ### Removed - Remove unused generated CSS styles (@audreyr) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 62363b91..e7ef8342 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -1,5 +1,10 @@ """ -This code has been adopted from Django's standard crypto functions and +Does the following: + +1. Generates and saves random secret key +2. Removes the taskapp if celery isn't going to be used + +A portion of this code was adopted from Django's standard crypto functions and utilities, specifically: https://github.com/django/django/blob/master/django/utils/crypto.py """ @@ -8,6 +13,9 @@ import os import random import shutil +# Get the root project directory +PROJECT_DIRECTORY = os.path.realpath(os.path.curdir) + # Use the system PRNG if possible try: random = random.SystemRandom() @@ -42,26 +50,41 @@ def get_random_string( ).digest()) return ''.join(random.choice(allowed_chars) for i in range(length)) -# Get the root project directory -project_directory = os.path.realpath(os.path.curdir) +def make_secret_key(project_directory): + """Generates and saves random secret key""" + # Determine the local_setting_file_location + local_setting_file_location = os.path.join( + project_directory, + 'config/settings/local.py' + ) -# Determine the local_setting_file_location -local_setting_file_location = os.path.join( - project_directory, - 'config/settings/local.py' -) + # Open locals.py + with open(local_setting_file_location) as f: + local_py = f.read() -# Open locals.py -with open(local_setting_file_location) as f: - local_py = f.read() + # Generate a SECRET_KEY that matches the Django standard + SECRET_KEY = get_random_string() + SECRET_KEY = 'CHANGEME!!!' + SECRET_KEY -# Generate a SECRET_KEY that matches the Django standard -SECRET_KEY = get_random_string() -SECRET_KEY = 'CHANGEME!!!' + SECRET_KEY + # Replace "CHANGEME!!!" with SECRET_KEY + local_py = local_py.replace('CHANGEME!!!', SECRET_KEY) -# Replace "CHANGEME!!!" with SECRET_KEY -local_py = local_py.replace('CHANGEME!!!', SECRET_KEY) + # Write the results to the locals.py module + with open(local_setting_file_location, 'w') as f: + f.write(local_py) -# Write the results to the locals.py module -with open(local_setting_file_location, 'w') as f: - f.write(local_py) +def remove_task_app(project_directory): + """Removes the taskapp if celery isn't going to be used""" + # Determine the local_setting_file_location + task_app_location = os.path.join( + PROJECT_DIRECTORY, + '{{ cookiecutter.repo_name }}/taskapp' + ) + shutil.rmtree(task_app_location) + +# 1. Generates and saves random secret key +make_secret_key(PROJECT_DIRECTORY) + +# 2. Removes the taskapp if celery isn't going to be used +if '{{ cookiecutter.use_celery }}'.lower() == 'n': + remove_task_app(PROJECT_DIRECTORY)