fixed #327 - post_gen_hook now deletes taskapp if celery isn't going to be used

This commit is contained in:
Daniel Roy Greenfeld 2015-09-14 18:55:41 -07:00
parent 656a9b9ed5
commit 5bae08596e
2 changed files with 45 additions and 19 deletions

View File

@ -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/). This project adheres to [Semantic Versioning](http://semver.org/).
## [2015-09-14] ## [2015-09-14]
### Added
- Functionality to delete taskapp if celery isn't going to be used (@pydanny)
### Removed ### Removed
- Remove unused generated CSS styles (@audreyr) - Remove unused generated CSS styles (@audreyr)

View File

@ -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: utilities, specifically:
https://github.com/django/django/blob/master/django/utils/crypto.py https://github.com/django/django/blob/master/django/utils/crypto.py
""" """
@ -8,6 +13,9 @@ import os
import random import random
import shutil import shutil
# Get the root project directory
PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)
# Use the system PRNG if possible # Use the system PRNG if possible
try: try:
random = random.SystemRandom() random = random.SystemRandom()
@ -42,26 +50,41 @@ def get_random_string(
).digest()) ).digest())
return ''.join(random.choice(allowed_chars) for i in range(length)) return ''.join(random.choice(allowed_chars) for i in range(length))
# Get the root project directory def make_secret_key(project_directory):
project_directory = os.path.realpath(os.path.curdir) """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 # Open locals.py
local_setting_file_location = os.path.join( with open(local_setting_file_location) as f:
project_directory, local_py = f.read()
'config/settings/local.py'
)
# Open locals.py # Generate a SECRET_KEY that matches the Django standard
with open(local_setting_file_location) as f: SECRET_KEY = get_random_string()
local_py = f.read() SECRET_KEY = 'CHANGEME!!!' + SECRET_KEY
# Generate a SECRET_KEY that matches the Django standard # Replace "CHANGEME!!!" with SECRET_KEY
SECRET_KEY = get_random_string() local_py = local_py.replace('CHANGEME!!!', SECRET_KEY)
SECRET_KEY = 'CHANGEME!!!' + SECRET_KEY
# Replace "CHANGEME!!!" with SECRET_KEY # Write the results to the locals.py module
local_py = local_py.replace('CHANGEME!!!', SECRET_KEY) with open(local_setting_file_location, 'w') as f:
f.write(local_py)
# Write the results to the locals.py module def remove_task_app(project_directory):
with open(local_setting_file_location, 'w') as f: """Removes the taskapp if celery isn't going to be used"""
f.write(local_py) # 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)