From 4219e2b7f833171bd24c6dd9802deb9eb7d0bafe Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Wed, 21 Mar 2012 22:29:32 +0000 Subject: [PATCH] Fixed #17920 -- Actually pass the full path of a newly created project or app in the template context as mentioned in the startproject docs. Many thanks to Preston Holmes for the initial patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17773 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/conf/project_template/manage.py | 3 +- django/core/management/templates.py | 6 ++-- .../project_template/manage.py | 7 ++++- tests/regressiontests/admin_scripts/tests.py | 29 +++++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/django/conf/project_template/manage.py b/django/conf/project_template/manage.py index 2568a324b1..391dd88ba4 100755 --- a/django/conf/project_template/manage.py +++ b/django/conf/project_template/manage.py @@ -1,5 +1,6 @@ #!/usr/bin/env python -import os, sys +import os +import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings") diff --git a/django/core/management/templates.py b/django/core/management/templates.py index 5e7e5741b7..9a4b2c1723 100644 --- a/django/core/management/templates.py +++ b/django/core/management/templates.py @@ -89,8 +89,10 @@ class TemplateCommand(BaseCommand): message = e raise CommandError(message) else: - top_dir = path.expanduser(target) - + 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) extensions = tuple( handle_extensions(options.get('extensions'), ignored=())) diff --git a/tests/regressiontests/admin_scripts/custom_templates/project_template/manage.py b/tests/regressiontests/admin_scripts/custom_templates/project_template/manage.py index ad8aafe233..d9843c433f 100755 --- a/tests/regressiontests/admin_scripts/custom_templates/project_template/manage.py +++ b/tests/regressiontests/admin_scripts/custom_templates/project_template/manage.py @@ -1 +1,6 @@ -# The manage.py of the {{ project_name }} test project \ No newline at end of file +# The manage.py of the {{ project_name }} test project + +# template context: +project_name = '{{ project_name }}' +project_directory = '{{ project_directory }}' +secret_key = '{{ secret_key }}' diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py index 74fb20f892..669c6e8258 100644 --- a/tests/regressiontests/admin_scripts/tests.py +++ b/tests/regressiontests/admin_scripts/tests.py @@ -1534,3 +1534,32 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase): with open(os.path.join(base_path, f)) as fh: self.assertEqual(fh.read(), '# some file for customtestproject test project') + + def test_custom_project_template_context_variables(self): + "Make sure template context variables are rendered with proper values" + template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template') + args = ['startproject', '--template', template_path, 'another_project', 'project_dir'] + testproject_dir = os.path.join(test_dir, 'project_dir') + os.mkdir(testproject_dir) + out, err = self.run_django_admin(args) + self.addCleanup(shutil.rmtree, testproject_dir) + self.assertNoOutput(err) + test_manage_py = os.path.join(testproject_dir, 'manage.py') + with open(test_manage_py, 'r') as fp: + content = fp.read() + self.assertIn("project_name = 'another_project'", content) + self.assertIn("project_directory = '%s'" % testproject_dir, content) + + def test_custom_project_destination_missing(self): + """ + Make sure an exception is raised when the provided + destination directory doesn't exist + """ + template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template') + args = ['startproject', '--template', template_path, 'yet_another_project', 'project_dir2'] + testproject_dir = os.path.join(test_dir, 'project_dir2') + out, err = self.run_django_admin(args) + self.assertNoOutput(out) + self.assertOutput(err, "Destination directory '%s' does not exist, please create it first." % testproject_dir) + self.assertFalse(os.path.exists(testproject_dir)) +