Skip to content

Commit

Permalink
Make updated project template work with local_settings.py.template
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHill committed Jun 5, 2015
1 parent 61f119d commit d967e70
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
.DS_Store
.coverage
.idea/
local_settings.py
Mezzanine.egg-info/
build/
docs/build/
dist/
/mezzanine/project_template/static/
mezzanine/project_template/static/
/mezzanine/.project
/mezzanine/.pydevproject
1 change: 1 addition & 0 deletions .hgignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ syntax: glob
.DS_Store
.coverage
.idea
local_settings.py
build
dist
*.egg-info
Expand Down
52 changes: 47 additions & 5 deletions mezzanine/bin/management/commands/mezzanine_project.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,68 @@
from shutil import move
import django
from django.core.management import CommandError
from os import path

from django.core.management.commands.startproject import Command as BaseCommand
from django.utils.crypto import get_random_string
import os

import mezzanine


class Command(BaseCommand):
help = BaseCommand.help.replace("Django", "Mezzanine")

def handle(self, project_name=None, target=None, *args, **options):
"""Overridden to provide a template value for nevercache_key. The
method is copied verbatim from startproject.Command."""
def handle(self, *args, **options):

# Overridden to provide a template value for nevercache_key. The
# method is copied verbatim from startproject.Command."""
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
options['nevercache_key'] = get_random_string(50, chars)

super(Command, self).handle(project_name, target, **options)
# Indicate that local_settings.py.template should be rendered
options['files'] = ['local_settings.py.template']

super(Command, self).handle(*args, **options)

if django.VERSION < (1, 8):
project_name, target = args + (None,) * max(0, 2 - len(args))
else:
project_name, target = options.pop('name'), options.pop('directory')

project_dir = self.get_project_directory(project_name, target)
project_app_dir = os.path.join(project_dir, project_name)

# Now rename "local_settings.py.template" to "local_settings.py"
move(os.path.join(project_app_dir, "local_settings.py.template"),
os.path.join(project_app_dir, "local_settings.py"))

def get_project_directory(self, name, target):
"""This code is copied verbatim from Django's TemplateCommand.handle(),
but with the directory creation commented out."""

# if some directory is given, make sure it's nicely expanded
if target is None:
top_dir = path.join(os.getcwd(), name)
# try:
# os.makedirs(top_dir)
# except OSError as e:
# if e.errno == errno.EEXIST:
# message = "'%s' already exists" % top_dir
# else:
# message = e
# raise CommandError(message)
else:
top_dir = os.path.abspath(path.expanduser(target))
if not os.path.exists(top_dir):
raise CommandError("Destination directory '%s' does not "
"exist, please create it first." % top_dir)

return top_dir

def handle_template(self, template, subdir):
"""Use Mezzanine's project template by default. The method of picking
the default directory is copied from Django's TemplateCommand."""
if template is None:
return path.join(mezzanine.__path__[0], subdir)
return super(Command, self).handle(template, subdir)
return super(Command, self).handle_template(template, subdir)
1 change: 0 additions & 1 deletion mezzanine/bin/mezzanine_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def create_project():
settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) + ['mezzanine.bin']

argv = sys.argv[:1] + ['mezzanine_project'] + sys.argv[1:]
print(sys.argv)
management.execute_from_command_line(argv)


Expand Down
2 changes: 1 addition & 1 deletion mezzanine/project_template/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
*.db
.DS_Store
.coverage
/local_settings.py
local_settings.py
/static
2 changes: 1 addition & 1 deletion mezzanine/project_template/.hgignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ syntax: glob
*.db
.DS_Store
.coverage
local_settings.py

syntax: regexp
^static/
^local_settings.py

0 comments on commit d967e70

Please sign in to comment.