Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17517 -- Added `--name` option to startproject and startapp ma…

…nagement commands to be able to render files without a file extension. Thanks, Florian Apolloner.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17432 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit faeee611d69717614424b6c3867cd5798ee25496 1 parent bb6921c
Jannis Leidel authored February 04, 2012
13  django/core/management/templates.py
@@ -48,6 +48,11 @@ class TemplateCommand(BaseCommand):
48 48
                     help='The file extension(s) to render (default: "py") '
49 49
                          'Separate multiple extensions with commas, or use '
50 50
                          '-e multiple times.'),
  51
+        make_option('--name', '-n', dest='files',
  52
+                    action='append', default=[],
  53
+                    help='The file name(s) to render '
  54
+                         'Separate multiple extensions with commas, or use '
  55
+                         '-n multiple times.')
51 56
         )
52 57
     requires_model_validation = False
53 58
     # Can't import settings during this command, because they haven't
@@ -89,10 +94,16 @@ def handle(self, app_or_project, name, target=None, **options):
89 94
 
90 95
         extensions = tuple(
91 96
             handle_extensions(options.get('extensions'), ignored=()))
  97
+        extra_files = []
  98
+        for file in options.get('files'):
  99
+            extra_files.extend(map(lambda x: x.strip(), file.split(',')))
92 100
         if self.verbosity >= 2:
93 101
             self.stdout.write("Rendering %s template files with "
94 102
                               "extensions: %s\n" %
95 103
                               (app_or_project, ', '.join(extensions)))
  104
+            self.stdout.write("Rendering %s template files with "
  105
+                              "filenames: %s\n" %
  106
+                              (app_or_project, ', '.join(extra_files)))
96 107
 
97 108
         base_name = '%s_name' % app_or_project
98 109
         base_subdir = '%s_template' % app_or_project
@@ -142,7 +153,7 @@ def handle(self, app_or_project, name, target=None, **options):
142 153
                 # accidentally render Django templates files
143 154
                 with open(old_path, 'r') as template_file:
144 155
                     content = template_file.read()
145  
-                if filename.endswith(extensions):
  156
+                if filename.endswith(extensions) or filename in extra_files:
146 157
                     template = Template(content)
147 158
                     content = template.render(context)
148 159
                 with open(new_path, 'w') as new_file:
9  docs/man/django-admin.1
@@ -115,11 +115,11 @@ name(s).
115 115
 Prints the SQL statements for resetting PostgreSQL sequences for the
116 116
 given app name(s).
117 117
 .TP
118  
-.BI "startapp [" "\-\-template=PATH_OR_URL" "] [" "\-\-extension=EXTENSION" "] [" "appname" "] [" "destination" "]"
  118
+.BI "startapp [" "\-\-template=PATH_OR_URL" "] [" "\-\-extension=EXTENSION" "] [" "\-\-name=FILENAME" "] [" "appname" "] [" "destination" "]"
119 119
 Creates a Django app directory structure for the given app name in
120 120
 the current directory or the optional destination.
121 121
 .TP
122  
-.BI "startproject [" "\-\-template=PATH_OR_URL" "] [" "\-\-extension=EXTENSION" "] [" "projectname" "] [" "destination" "]"
  122
+.BI "startproject [" "\-\-template=PATH_OR_URL" "] [" "\-\-extension=EXTENSION" "] [" "\-\-name=FILENAME" "] [" "projectname" "] [" "destination" "]"
123 123
 Creates a Django project directory structure for the given project name
124 124
 in the current directory or the optional destination.
125 125
 .TP
@@ -213,9 +213,12 @@ Don't break long message lines into several lines.
213 213
 .I \-a, \-\-all
214 214
 Process all available locales when using makemessages..SH "ENVIRONMENT"
215 215
 .TP
216  
-.I \-a, \-\-template=PATH_OR_URL
  216
+.I \-\-template=PATH_OR_URL
217 217
 The file or directory path or URL to load the project and app templates from.
218 218
 .TP
  219
+.I \-n, \-\-name=FILENAME
  220
+The name of an additional file to render when using app and project templates.
  221
+.TP
219 222
 .I DJANGO_SETTINGS_MODULE
220 223
 In the absence of the
221 224
 .BI \-\-settings
6  docs/ref/django-admin.txt
@@ -951,7 +951,8 @@ creating the ``myapp`` app::
951 951
 
952 952
 When Django copies the app template files, it also renders the files
953 953
 whose extension matches those passed with the ``--extension`` option (``py``
954  
-by default) using the template engine. The :class:`template context
  954
+by default) and those files which names are passed with the ``--name`` option
  955
+using the template engine. The :class:`template context
955 956
 <django.template.Context>` used is:
956 957
 
957 958
 - Any option passed to the startapp command
@@ -1013,7 +1014,8 @@ when creating the ``myproject`` project::
1013 1014
 
1014 1015
 When Django copies the project template files, it will also render the files
1015 1016
 whose extension matches those passed with the ``--extension`` option (``py``
1016  
-by default) using the template engine. The :class:`template context
  1017
+by default) and those files which names are passed with the ``--name`` option
  1018
+using the template engine. The :class:`template context
1017 1019
 <django.template.Context>` used is:
1018 1020
 
1019 1021
 - Any option passed to the startproject command
1  tests/regressiontests/admin_scripts/custom_templates/project_template/additional_dir/Procfile
... ...
@@ -0,0 +1 @@
  1
+# some file for {{ project_name }} test project
1  tests/regressiontests/admin_scripts/custom_templates/project_template/additional_dir/requirements.txt
... ...
@@ -0,0 +1 @@
  1
+# some file for {{ project_name }} test project
18  tests/regressiontests/admin_scripts/tests.py
@@ -1489,3 +1489,21 @@ def test_project_template_tarball_url(self):
1489 1489
         self.assertNoOutput(err)
1490 1490
         self.assertTrue(os.path.isdir(testproject_dir))
1491 1491
         self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'run.py')))
  1492
+
  1493
+    def test_file_without_extension(self):
  1494
+        "Make sure the startproject management command is able to render custom files"
  1495
+        template_path = os.path.join(test_dir, 'admin_scripts', 'custom_templates', 'project_template')
  1496
+        args = ['startproject', '--template', template_path, 'customtestproject', '-e', 'txt', '-n', 'Procfile']
  1497
+        testproject_dir = os.path.join(test_dir, 'customtestproject')
  1498
+
  1499
+        out, err = self.run_django_admin(args)
  1500
+        self.addCleanup(shutil.rmtree, testproject_dir)
  1501
+        self.assertNoOutput(err)
  1502
+        self.assertTrue(os.path.isdir(testproject_dir))
  1503
+        self.assertTrue(os.path.exists(os.path.join(testproject_dir, 'additional_dir')))
  1504
+        base_path = os.path.join(testproject_dir, 'additional_dir')
  1505
+        for f in ('Procfile', 'additional_file.py', 'requirements.txt'):
  1506
+            self.assertTrue(os.path.exists(os.path.join(base_path, f)))
  1507
+            with open(os.path.join(base_path, f)) as fh:
  1508
+                self.assertEqual(fh.read(),
  1509
+                    '# some file for customtestproject test project')

0 notes on commit faeee61

Please sign in to comment.
Something went wrong with that request. Please try again.