Ticket #113: management.py

File management.py, 20.3 KB (added by Jason Huggins, 19 years ago)

Here's the new copy of management.py... (so you can more easily do a file compare if you want)

Line 
1# Django management-related functions, including "CREATE TABLE" generation and
2# development-server initialization.
3
4import django
5import os, re, sys
6
7MODULE_TEMPLATE = ''' {%% if perms.%(app)s.%(addperm)s or perms.%(app)s.%(changeperm)s %%}
8 <tr>
9 <th>{%% if perms.%(app)s.%(changeperm)s %%}<a href="/%(app)s/%(mod)s/">{%% endif %%}%(name)s{%% if perms.%(app)s.%(changeperm)s %%}</a>{%% endif %%}</th>
10 <td class="x50">{%% if perms.%(app)s.%(addperm)s %%}<a href="/%(app)s/%(mod)s/add/" class="addlink">{%% endif %%}Add{%% if perms.%(app)s.%(addperm)s %%}</a>{%% endif %%}</td>
11 <td class="x75">{%% if perms.%(app)s.%(changeperm)s %%}<a href="/%(app)s/%(mod)s/" class="changelink">{%% endif %%}Change{%% if perms.%(app)s.%(changeperm)s %%}</a>{%% endif %%}</td>
12 </tr>
13 {%% endif %%}'''
14
15APP_ARGS = '[app app ...]'
16
17# Use django.__path__[0] because we don't know which directory django into
18# which has been installed.
19PROJECT_TEMPLATE_DIR = django.__path__[0] + '/conf/%s_template'
20ADMIN_TEMPLATE_DIR = django.__path__[0] + '/conf/admin_templates'
21
22def _get_packages_insert(app_label):
23 return "INSERT INTO packages (label, name) VALUES ('%s', '%s');" % (app_label, app_label)
24
25def _get_permission_codename(action, opts):
26 return '%s_%s' % (action, opts.object_name.lower())
27
28def _get_all_permissions(opts):
29 "Returns (codename, name) for all permissions in the given opts."
30 perms = []
31 if opts.admin:
32 for action in ('add', 'change', 'delete'):
33 perms.append((_get_permission_codename(action, opts), 'Can %s %s' % (action, opts.verbose_name)))
34 return perms + list(opts.permissions)
35
36def _get_permission_insert(name, codename, opts):
37 return "INSERT INTO auth_permissions (name, package, codename) VALUES ('%s', '%s', '%s');" % \
38 (name.replace("'", "''"), opts.app_label, codename)
39
40def _get_contenttype_insert(opts):
41 return "INSERT INTO content_types (name, package, python_module_name) VALUES ('%s', '%s', '%s');" % \
42 (opts.verbose_name, opts.app_label, opts.module_name)
43
44def _is_valid_dir_name(s):
45 return bool(re.search(r'^\w+$', s))
46
47def get_sql_create(mod):
48 "Returns a list of the CREATE TABLE SQL statements for the given module."
49 from django.core import db, meta
50 final_output = []
51 for klass in mod._MODELS:
52 opts = klass._meta
53 table_output = []
54 for f in opts.fields:
55 if isinstance(f, meta.ForeignKey):
56 rel_field = f.rel.get_related_field()
57 # If the foreign key points to an AutoField, the foreign key
58 # should be an IntegerField, not an AutoField. Otherwise, the
59 # foreign key should be the same type of field as the field
60 # to which it points.
61 if rel_field.__class__.__name__ == 'AutoField':
62 data_type = 'IntegerField'
63 else:
64 data_type = rel_field.__class__.__name__
65 else:
66 rel_field = f
67 data_type = f.__class__.__name__
68 col_type = db.DATA_TYPES[data_type]
69 if col_type is not None:
70 field_output = [f.name, col_type % rel_field.__dict__]
71 field_output.append('%sNULL' % (not f.null and 'NOT ' or ''))
72 if f.unique:
73 field_output.append('UNIQUE')
74 if f.primary_key:
75 field_output.append('PRIMARY KEY')
76 if f.rel:
77 field_output.append('REFERENCES %s (%s)' % \
78 (f.rel.to.db_table, f.rel.to.get_field(f.rel.field_name).name))
79 table_output.append(' '.join(field_output))
80 if opts.order_with_respect_to:
81 table_output.append('_order %s NULL' % db.DATA_TYPES['IntegerField'])
82 for field_constraints in opts.unique_together:
83 table_output.append('UNIQUE (%s)' % ", ".join(field_constraints))
84
85 full_statement = ['CREATE TABLE %s (' % opts.db_table]
86 for i, line in enumerate(table_output): # Combine and add commas.
87 full_statement.append(' %s%s' % (line, i < len(table_output)-1 and ',' or ''))
88 full_statement.append(');')
89 final_output.append('\n'.join(full_statement))
90
91 for klass in mod._MODELS:
92 opts = klass._meta
93 for f in opts.many_to_many:
94 table_output = ['CREATE TABLE %s (' % f.get_m2m_db_table(opts)]
95 table_output.append(' id %s NOT NULL PRIMARY KEY,' % db.DATA_TYPES['AutoField'])
96 table_output.append(' %s_id %s NOT NULL REFERENCES %s (%s),' % \
97 (opts.object_name.lower(), db.DATA_TYPES['IntegerField'], opts.db_table, opts.pk.name))
98 table_output.append(' %s_id %s NOT NULL REFERENCES %s (%s),' % \
99 (f.rel.to.object_name.lower(), db.DATA_TYPES['IntegerField'], f.rel.to.db_table, f.rel.to.pk.name))
100 table_output.append(' UNIQUE (%s_id, %s_id)' % (opts.object_name.lower(), f.rel.to.object_name.lower()))
101 table_output.append(');')
102 final_output.append('\n'.join(table_output))
103 return final_output
104get_sql_create.help_doc = "Prints the CREATE TABLE SQL statements for the given app(s)."
105get_sql_create.args = APP_ARGS
106
107def get_sql_delete(mod):
108 "Returns a list of the DROP TABLE SQL statements for the given module."
109 from django.core import db
110 try:
111 cursor = db.db.cursor()
112 except:
113 cursor = None
114 output = []
115 for klass in mod._MODELS:
116 try:
117 if cursor is not None:
118 # Check whether the table exists.
119 cursor.execute("SELECT 1 FROM %s LIMIT 1" % klass._meta.db_table)
120 except:
121 # The table doesn't exist, so it doesn't need to be dropped.
122 pass
123 else:
124 output.append("DROP TABLE %s;" % klass._meta.db_table)
125 for klass in mod._MODELS:
126 opts = klass._meta
127 for f in opts.many_to_many:
128 try:
129 if cursor is not None:
130 cursor.execute("SELECT 1 FROM %s LIMIT 1" % f.get_m2m_db_table(opts))
131 except:
132 pass
133 else:
134 output.append("DROP TABLE %s;" % f.get_m2m_db_table(opts))
135 output.append("DELETE FROM packages WHERE label = '%s';" % mod._MODELS[0]._meta.app_label)
136 output.append("DELETE FROM auth_permissions WHERE package = '%s';" % mod._MODELS[0]._meta.app_label)
137 output.append("DELETE FROM content_types WHERE package = '%s';" % mod._MODELS[0]._meta.app_label)
138 return output[::-1] # Reverse it, to deal with table dependencies.
139get_sql_delete.help_doc = "Prints the DROP TABLE SQL statements for the given app(s)."
140get_sql_delete.args = APP_ARGS
141
142def get_sql_reset(mod):
143 "Returns a list of the DROP TABLE SQL, then the CREATE TABLE SQL, for the given module."
144 return get_sql_delete(mod) + get_sql_all(mod)
145get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app(s)."
146get_sql_reset.args = APP_ARGS
147
148def get_sql_initial_data(mod):
149 "Returns a list of the initial INSERT SQL statements for the given module."
150 output = []
151 app_label = mod._MODELS[0]._meta.app_label
152 output.append(_get_packages_insert(app_label))
153 app_dir = os.path.normpath(os.path.join(os.path.dirname(mod.__file__), '../sql'))
154 for klass in mod._MODELS:
155 opts = klass._meta
156 # Add custom SQL, if it's available.
157 sql_file_name = os.path.join(app_dir, opts.module_name + '.sql')
158 if os.path.exists(sql_file_name):
159 fp = open(sql_file_name, 'r')
160 output.append(fp.read())
161 fp.close()
162 # Content types.
163 output.append(_get_contenttype_insert(opts))
164 # Permissions.
165 for codename, name in _get_all_permissions(opts):
166 output.append(_get_permission_insert(name, codename, opts))
167 return output
168get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app(s)."
169get_sql_initial_data.args = APP_ARGS
170
171def get_sql_sequence_reset(mod):
172 "Returns a list of the SQL statements to reset PostgreSQL sequences for the given module."
173 from django.core import meta
174 output = []
175 for klass in mod._MODELS:
176 for f in klass._meta.fields:
177 if isinstance(f, meta.AutoField):
178 output.append("SELECT setval('%s_%s_seq', (SELECT max(%s) FROM %s));" % (klass._meta.db_table, f.name, f.name, klass._meta.db_table))
179 return output
180get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app(s)."
181get_sql_sequence_reset.args = APP_ARGS
182
183def get_sql_indexes(mod):
184 "Returns a list of the CREATE INDEX SQL statements for the given module."
185 output = []
186 for klass in mod._MODELS:
187 for f in klass._meta.fields:
188 if f.db_index:
189 unique = f.unique and "UNIQUE " or ""
190 output.append("CREATE %sINDEX %s_%s ON %s (%s);" % \
191 (unique, klass._meta.db_table, f.name, klass._meta.db_table, f.name))
192 return output
193get_sql_indexes.help_doc = "Prints the CREATE INDEX SQL statements for the given app(s)."
194get_sql_indexes.args = APP_ARGS
195
196def get_sql_all(mod):
197 "Returns a list of CREATE TABLE SQL and initial-data insert for the given module."
198 return get_sql_create(mod) + get_sql_initial_data(mod)
199get_sql_all.help_doc = "Prints the CREATE TABLE and initial-data SQL statements for the given app(s)."
200get_sql_all.args = APP_ARGS
201
202def database_check(mod):
203 "Checks that everything is properly installed in the database for the given module."
204 from django.core import db
205 cursor = db.db.cursor()
206 app_label = mod._MODELS[0]._meta.app_label
207
208 # Check that the package exists in the database.
209 cursor.execute("SELECT 1 FROM packages WHERE label = %s", [app_label])
210 if cursor.rowcount < 1:
211# sys.stderr.write("The '%s' package isn't installed.\n" % app_label)
212 print _get_packages_insert(app_label)
213
214 # Check that the permissions and content types are in the database.
215 perms_seen = {}
216 contenttypes_seen = {}
217 for klass in mod._MODELS:
218 opts = klass._meta
219 perms = _get_all_permissions(opts)
220 perms_seen.update(dict(perms))
221 contenttypes_seen[opts.module_name] = 1
222 for codename, name in perms:
223 cursor.execute("SELECT 1 FROM auth_permissions WHERE package = %s AND codename = %s", (app_label, codename))
224 if cursor.rowcount < 1:
225# sys.stderr.write("The '%s.%s' permission doesn't exist.\n" % (app_label, codename))
226 print _get_permission_insert(name, codename, opts)
227 cursor.execute("SELECT 1 FROM content_types WHERE package = %s AND python_module_name = %s", (app_label, opts.module_name))
228 if cursor.rowcount < 1:
229# sys.stderr.write("The '%s.%s' content type doesn't exist.\n" % (app_label, opts.module_name))
230 print _get_contenttype_insert(opts)
231
232 # Check that there aren't any *extra* permissions in the DB that the model
233 # doesn't know about.
234 cursor.execute("SELECT codename FROM auth_permissions WHERE package = %s", (app_label,))
235 for row in cursor.fetchall():
236 try:
237 perms_seen[row[0]]
238 except KeyError:
239# sys.stderr.write("A permission called '%s.%s' was found in the database but not in the model.\n" % (app_label, row[0]))
240 print "DELETE FROM auth_permissions WHERE package='%s' AND codename = '%s';" % (app_label, row[0])
241
242 # Check that there aren't any *extra* content types in the DB that the
243 # model doesn't know about.
244 cursor.execute("SELECT python_module_name FROM content_types WHERE package = %s", (app_label,))
245 for row in cursor.fetchall():
246 try:
247 contenttypes_seen[row[0]]
248 except KeyError:
249# sys.stderr.write("A content type called '%s.%s' was found in the database but not in the model.\n" % (app_label, row[0]))
250 print "DELETE FROM content_types WHERE package='%s' AND python_module_name = '%s';" % (app_label, row[0])
251database_check.help_doc = "Checks that everything is installed in the database for the given app(s) and prints SQL statements if needed."
252database_check.args = APP_ARGS
253
254def get_admin_index(mod):
255 "Returns admin-index template snippet (in list form) for the given module."
256 from django.core import meta
257 output = []
258 app_label = mod._MODELS[0]._meta.app_label
259 output.append('{%% if perms.%s %%}' % app_label)
260 output.append('<div class="module"><h2>%s</h2><table>' % app_label.title())
261 for klass in mod._MODELS:
262 if klass._meta.admin:
263 output.append(MODULE_TEMPLATE % {
264 'app': app_label,
265 'mod': klass._meta.module_name,
266 'name': meta.capfirst(klass._meta.verbose_name_plural),
267 'addperm': klass._meta.get_add_permission(),
268 'changeperm': klass._meta.get_change_permission(),
269 })
270 output.append('</table></div>')
271 output.append('{% endif %}')
272 return output
273get_admin_index.help_doc = "Prints the admin-index template snippet for the given app(s)."
274get_admin_index.args = APP_ARGS
275
276def init():
277 "Initializes the database with auth and core."
278 from django.core import db, meta
279 auth = meta.get_app('auth')
280 core = meta.get_app('core')
281 try:
282 cursor = db.db.cursor()
283 for sql in get_sql_create(core) + get_sql_create(auth) + get_sql_initial_data(core) + get_sql_initial_data(auth):
284 cursor.execute(sql)
285 cursor.execute("INSERT INTO %s (domain, name) VALUES ('mysite.com', 'My Django site')" % core.Site._meta.db_table)
286 except Exception, e:
287 sys.stderr.write("Error: The database couldn't be initialized. Here's the full exception:\n%s\n" % e)
288 db.db.rollback()
289 sys.exit(1)
290 db.db.commit()
291init.args = ''
292
293def install(mod):
294 "Executes the equivalent of 'get_sql_all' in the current database."
295 from django.core import db
296 sql_list = get_sql_all(mod)
297 try:
298 cursor = db.db.cursor()
299 for sql in sql_list:
300 cursor.execute(sql)
301 except Exception, e:
302 mod_name = mod.__name__[mod.__name__.rindex('.')+1:]
303 sys.stderr.write("""Error: %s couldn't be installed. Possible reasons:
304 * The database isn't running or isn't configured correctly.
305 * At least one of the database tables already exists.
306 * The SQL was invalid.
307Hint: Look at the output of 'django-admin.py sqlall %s'. That's the SQL this command wasn't able to run.
308The full error: %s\n""" % \
309 (mod_name, mod_name, e))
310 db.db.rollback()
311 sys.exit(1)
312 db.db.commit()
313install.args = APP_ARGS
314
315def _start_helper(app_or_project, name, directory, other_name=''):
316 other = {'project': 'app', 'app': 'project'}[app_or_project]
317 if not _is_valid_dir_name(name):
318 sys.stderr.write("Error: %r is not a valid %s name. Please use only numbers, letters and underscores.\n" % (name, app_or_project))
319 sys.exit(1)
320 top_dir = os.path.join(directory, name)
321 try:
322 os.mkdir(top_dir)
323 except OSError, e:
324 sys.stderr.write("Error: %s\n" % e)
325 sys.exit(1)
326 template_dir = PROJECT_TEMPLATE_DIR % app_or_project
327 for d, subdirs, files in os.walk(template_dir):
328 relative_dir = d[len(template_dir)+1:].replace('%s_name' % app_or_project, name)
329 if relative_dir:
330 os.mkdir(os.path.join(top_dir, relative_dir))
331 for i, subdir in enumerate(subdirs):
332 if subdir.startswith('.'):
333 del subdirs[i]
334 for f in files:
335 if f.endswith('.pyc'):
336 continue
337 fp_old = open(os.path.join(d, f), 'r')
338 fp_new = open(os.path.join(top_dir, relative_dir, f.replace('%s_name' % app_or_project, name)), 'w')
339 fp_new.write(fp_old.read().replace('{{ %s_name }}' % app_or_project, name).replace('{{ %s_name }}' % other, other_name))
340 fp_old.close()
341 fp_new.close()
342
343def startproject(project_name, directory):
344 "Creates a Django project for the given project_name in the given directory."
345 from random import choice
346 _start_helper('project', project_name, directory)
347 # Populate TEMPLATE_DIRS for the admin templates, based on where Django is
348 # installed.
349 admin_settings_file = os.path.join(directory, project_name, 'settings/admin.py')
350 settings_contents = open(admin_settings_file, 'r').read()
351 fp = open(admin_settings_file, 'w')
352 settings_contents = re.sub(r'(?s)\b(TEMPLATE_DIRS\s*=\s*\()(.*?)\)', "\\1\n '%s',\\2)" % ADMIN_TEMPLATE_DIR, settings_contents)
353 fp.write(settings_contents)
354 fp.close()
355 # Create a random SECRET_KEY hash, and put it in the main settings.
356 main_settings_file = os.path.join(directory, project_name, 'settings/main.py')
357 settings_contents = open(main_settings_file, 'r').read()
358 fp = open(main_settings_file, 'w')
359 secret_key = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)])
360 settings_contents = re.sub(r"(?<=SECRET_KEY = ')'", secret_key + "'", settings_contents)
361 fp.write(settings_contents)
362 fp.close()
363startproject.help_doc = "Creates a Django project directory structure for the given project name in the current directory."
364startproject.args = "[projectname]"
365
366def startapp(app_name, directory):
367 "Creates a Django app for the given project_name in the given directory."
368 # Determine the project_name a bit naively -- by looking at the name of
369 # the parent directory.
370 project_dir = os.path.normpath(os.path.join(directory, '../'))
371 project_name = os.path.basename(project_dir)
372 _start_helper('app', app_name, directory, project_name)
373startapp.help_doc = "Creates a Django app directory structure for the given app name in the current directory."
374startapp.args = "[appname]"
375
376def createsuperuser():
377 "Creates a superuser account."
378 from django.core import validators
379 from django.models.auth import users
380 import getpass
381 try:
382 while 1:
383 username = raw_input('Username (only letters, digits and underscores): ')
384 if not username.isalnum():
385 sys.stderr.write("Error: That username is invalid.\n")
386 continue
387 try:
388 users.get_object(username__exact=username)
389 except users.UserDoesNotExist:
390 break
391 else:
392 sys.stderr.write("Error: That username is already taken.\n")
393 while 1:
394 email = raw_input('E-mail address: ')
395 try:
396 validators.isValidEmail(email, None)
397 except validators.ValidationError:
398 sys.stderr.write("Error: That e-mail address is invalid.\n")
399 else:
400 break
401 while 1:
402 password = getpass.getpass()
403 password2 = getpass.getpass('Password (again): ')
404 if password == password2:
405 break
406 sys.stderr.write("Error: Your passwords didn't match.\n")
407 except KeyboardInterrupt:
408 sys.stderr.write("\nOperation cancelled.\n")
409 sys.exit(1)
410 u = users.create_user(username, email, password)
411 u.is_staff = True
412 u.is_active = True
413 u.is_superuser = True
414 u.save()
415 print "User created successfully."
416createsuperuser.args = ''
417
418def runserver(port):
419 "Starts a lightweight Web server for development."
420 def run():
421 from django.core.servers.basehttp import run, WSGIServerException
422 from django.core.handlers.wsgi import AdminMediaHandler, WSGIHandler
423 from django.conf.settings import SETTINGS_MODULE
424 if not port.isdigit():
425 sys.stderr.write("Error: %r is not a valid port number.\n" % port)
426 sys.exit(1)
427 print "Starting server on port %s with settings module %r." % (port, SETTINGS_MODULE)
428 print "Go to http://127.0.0.1:%s/ for Django." % port
429 try:
430 run(int(port), AdminMediaHandler(WSGIHandler()))
431 except WSGIServerException, e:
432 # Use helpful error messages instead of ugly tracebacks.
433 ERRORS = {
434 13: "You don't have permission to access that port.",
435 98: "That port is already in use.",
436 }
437 try:
438 error_text = ERRORS[e.args[0].args[0]]
439 except (AttributeError, KeyError):
440 error_text = str(e)
441 sys.stderr.write("Error: %s\n" % error_text)
442 sys.exit(1)
443 from django.utils import autoreload
444 autoreload.main(run)
445
446runserver.args = '[optional port number]'
Back to Top