Code

Ticket #17517: 17517.diff

File 17517.diff, 5.8 KB (added by apollo13, 2 years ago)
Line 
1diff --git a/django/core/management/templates.py b/django/core/management/templates.py
2index 0a6a3a8..45d3885 100644
3--- a/django/core/management/templates.py
4+++ b/django/core/management/templates.py
5@@ -48,6 +48,11 @@ class TemplateCommand(BaseCommand):
6                     help='The file extension(s) to render (default: "py") '
7                          'Separate multiple extensions with commas, or use '
8                          '-e multiple times.'),
9+        make_option('--file', '-f', dest='files',
10+                    action='append', default=[],
11+                    help='The file name(s) to render '
12+                         'Separate multiple extensions with commas, or use '
13+                         '-f multiple times.')
14         )
15     requires_model_validation = False
16     # Can't import settings during this command, because they haven't
17@@ -89,10 +94,16 @@ class TemplateCommand(BaseCommand):
18 
19         extensions = tuple(
20             handle_extensions(options.get('extensions'), ignored=()))
21+        extra_files = []
22+        for file in options.get('files'):
23+            extra_files.extend(map(lambda x: x.strip(), file.split(',')))
24         if self.verbosity >= 2:
25             self.stdout.write("Rendering %s template files with "
26                               "extensions: %s\n" %
27                               (app_or_project, ', '.join(extensions)))
28+            self.stdout.write("Rendering %s template files with "
29+                              "filenames: %s\n" %
30+                              (app_or_project, ', '.join(extra_files)))
31 
32         base_name = '%s_name' % app_or_project
33         base_subdir = '%s_template' % app_or_project
34@@ -142,7 +153,7 @@ class TemplateCommand(BaseCommand):
35                 # accidentally render Django templates files
36                 with open(old_path, 'r') as template_file:
37                     content = template_file.read()
38-                if filename.endswith(extensions):
39+                if filename.endswith(extensions) or filename in extra_files:
40                     template = Template(content)
41                     content = template.render(context)
42                 with open(new_path, 'w') as new_file:
43diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
44index 4bde86c..9b0ac39 100644
45--- a/docs/ref/django-admin.txt
46+++ b/docs/ref/django-admin.txt
47@@ -951,7 +951,8 @@ creating the ``myapp`` app::
48 
49 When Django copies the app template files, it also renders the files
50 whose extension matches those passed with the ``--extension`` option (``py``
51-by default) using the template engine. The :class:`template context
52+by default) and those files which names are passed with the ``--file`` option
53+using the template engine. The :class:`template context
54 <django.template.Context>` used is:
55 
56 - Any option passed to the startapp command
57@@ -1013,7 +1014,8 @@ when creating the ``myproject`` project::
58 
59 When Django copies the project template files, it will also render the files
60 whose extension matches those passed with the ``--extension`` option (``py``
61-by default) using the template engine. The :class:`template context
62+by default) and those files which names are passed with the ``--file`` option
63+using the template engine. The :class:`template context
64 <django.template.Context>` used is:
65 
66 - Any option passed to the startproject command
67diff --git a/tests/regressiontests/admin_scripts/custom_templates/project_template/additional_dir/Procfile b/tests/regressiontests/admin_scripts/custom_templates/project_template/additional_dir/Procfile
68new file mode 100644
69index 0000000..032f0bc
70--- /dev/null
71+++ b/tests/regressiontests/admin_scripts/custom_templates/project_template/additional_dir/Procfile
72@@ -0,0 +1 @@
73+# some file for {{ project_name }} test project
74\ No newline at end of file
75diff --git a/tests/regressiontests/admin_scripts/custom_templates/project_template/additional_dir/requirements.txt b/tests/regressiontests/admin_scripts/custom_templates/project_template/additional_dir/requirements.txt
76new file mode 100644
77index 0000000..032f0bc
78--- /dev/null
79+++ b/tests/regressiontests/admin_scripts/custom_templates/project_template/additional_dir/requirements.txt
80@@ -0,0 +1 @@
81+# some file for {{ project_name }} test project
82\ No newline at end of file
83diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
84index 17a2ccb..fd1efb5 100644
85--- a/tests/regressiontests/admin_scripts/tests.py
86+++ b/tests/regressiontests/admin_scripts/tests.py
87@@ -1489,3 +1489,21 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
88         self.assertNoOutput(err)
89         self.assertTrue(os.path.isdir(testproject_dir))
90         self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
91+
92+    def test_custom_project_template(self):
93+        "Make sure the startproject management command is able to render custom files"
94+        template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
95+        args = ['startproject', '--template', template_path, 'customtestproject', '-e', 'txt', '-f', 'Procfile']
96+        testproject_dir = os.path.join(test_dir, 'customtestproject')
97+
98+        out, err = self.run_django_admin(args)
99+        self.addCleanup(shutil.rmtree, testproject_dir)
100+        self.assertNoOutput(err)
101+        self.assertTrue(os.path.isdir(testproject_dir))
102+        self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
103+        base_path = os.path.join(testproject_dir, 'additional_dir')
104+        for f in ('Procfile', 'additional_file.py', 'requirements.txt'):
105+            self.assertTrue(os.path.exists(os.path.join(base_path, f)))
106+            with open(os.path.join(base_path, f)) as fh:
107+                self.assertEqual(fh.read(),
108+                    '# some file for customtestproject test project')