0091_create_managedfiles.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import os
  2. import pkgutil
  3. from django.conf import settings
  4. from django.db import migrations, models
  5. import extras.models.mixins
  6. def create_files(cls, root_name, root_path):
  7. modules = list(pkgutil.iter_modules([root_path]))
  8. filenames = []
  9. for importer, module_name, ispkg in modules:
  10. try:
  11. module = importer.find_module(module_name).load_module(module_name)
  12. rel_path = os.path.relpath(module.__file__, root_path)
  13. filenames.append(rel_path)
  14. except ImportError:
  15. pass
  16. managed_files = [
  17. cls(file_root=root_name, file_path=filename)
  18. for filename in filenames
  19. ]
  20. cls.objects.bulk_create(managed_files)
  21. def replicate_scripts(apps, schema_editor):
  22. ScriptModule = apps.get_model('extras', 'ScriptModule')
  23. create_files(ScriptModule, 'scripts', settings.SCRIPTS_ROOT)
  24. def replicate_reports(apps, schema_editor):
  25. ReportModule = apps.get_model('extras', 'ReportModule')
  26. create_files(ReportModule, 'reports', settings.REPORTS_ROOT)
  27. class Migration(migrations.Migration):
  28. dependencies = [
  29. ('core', '0002_managedfile'),
  30. ('extras', '0090_objectchange_index_request_id'),
  31. ]
  32. operations = [
  33. # Create proxy models
  34. migrations.CreateModel(
  35. name='ReportModule',
  36. fields=[
  37. ],
  38. options={
  39. 'proxy': True,
  40. 'indexes': [],
  41. 'constraints': [],
  42. },
  43. bases=(extras.models.mixins.PythonModuleMixin, 'core.managedfile', models.Model),
  44. ),
  45. migrations.CreateModel(
  46. name='ScriptModule',
  47. fields=[
  48. ],
  49. options={
  50. 'proxy': True,
  51. 'indexes': [],
  52. 'constraints': [],
  53. },
  54. bases=(extras.models.mixins.PythonModuleMixin, 'core.managedfile', models.Model),
  55. ),
  56. # Instantiate ManagedFiles to represent scripts & reports
  57. migrations.RunPython(
  58. code=replicate_scripts,
  59. reverse_code=migrations.RunPython.noop
  60. ),
  61. migrations.RunPython(
  62. code=replicate_reports,
  63. reverse_code=migrations.RunPython.noop
  64. ),
  65. ]