| 201 | def _get_initial_sql_for_model(model): |
| 202 | "Returns a list of the initial INSERT SQL statements for the given model." |
| 203 | from django.conf import settings |
| 204 | from django.db.models.loading import get_app |
| 205 | |
| 206 | module_name = model._meta.module_name |
| 207 | app = get_app(model._meta.app_label) |
| 208 | app_sql_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) |
| 209 | |
| 210 | sql_files = [os.path.join(app_sql_dir, module_name + '.' + settings.DATABASE_ENGINE + '.sql'), |
| 211 | os.path.join(app_sql_dir, module_name + '.sql')] |
| 212 | |
| 213 | final_output = [] |
| 214 | for sql_file in sql_files: |
| 215 | if os.path.exists(sql_file): |
| 216 | fp = open(sql_file) |
| 217 | final_output.append(fp.read()) |
| 218 | fp.close() |
| 219 | return final_output |
| 220 | |
318 | | |
319 | | app_models = get_models(app) |
320 | | app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) |
321 | | |
322 | | for klass in app_models: |
323 | | opts = klass._meta |
324 | | |
325 | | # Add custom SQL, if it's available. |
326 | | # TODO: This probably needs changing |
327 | | sql_files = [os.path.join(app_dir, opts.module_name + '.' + settings.DATABASE_ENGINE + '.sql'), |
328 | | os.path.join(app_dir, opts.module_name + '.sql')] |
329 | | for sql_file in sql_files: |
330 | | if os.path.exists(sql_file): |
331 | | fp = open(sql_file) |
332 | | output.append(fp.read()) |
333 | | fp.close() |
334 | | |
335 | | # TODO: This is temporarily commented out until we come up |
336 | | # with a better way of letting people initialize permissions. |
337 | | # # Permissions. |
338 | | # for codename, name in _get_all_permissions(opts): |
339 | | # output.append(_get_permission_insert(name, codename, opts)) |
| 338 | for model in get_models(app): |
| 339 | output.extend(_get_initial_sql_for_model(model)) |