2
0

wagtailcrx.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. from django.core.management.templates import TemplateCommand
  5. from django.core.management.utils import get_random_secret_key
  6. CURRENT_PYTHON = sys.version_info[:2]
  7. REQUIRED_PYTHON = (3, 4)
  8. if CURRENT_PYTHON < REQUIRED_PYTHON:
  9. sys.stderr.write(
  10. "This version of Wagtail requires Python {}.{} or above - you are running {}.{}\n".format(*(REQUIRED_PYTHON + CURRENT_PYTHON)) # noqa
  11. )
  12. sys.exit(1)
  13. class CreateProject(TemplateCommand):
  14. """
  15. Based on django.core.management.startproject
  16. """
  17. help = "Creates the directory structure for a new Wagtail CRX project."
  18. missing_args_message = "You must provide a project name."
  19. def add_arguments(self, parser):
  20. parser.add_argument(
  21. '--sitename',
  22. help='Human readable name of your website or brand, e.g. "Mega Corp Inc."'
  23. )
  24. parser.add_argument(
  25. '--domain',
  26. help='Domain that will be used for your website in production, e.g. "www.example.com"'
  27. )
  28. super().add_arguments(parser)
  29. def handle(self, **options):
  30. # pop standard args
  31. project_name = options.pop('name')
  32. target = options.pop('directory')
  33. # Make sure given name is not already in use by another python package/module.
  34. try:
  35. __import__(project_name)
  36. except ImportError:
  37. pass
  38. else:
  39. sys.exit("'%s' conflicts with the name of an existing "
  40. "Python module and cannot be used as a project "
  41. "name. Please try another name." % project_name)
  42. # Create a random SECRET_KEY to put it in the main settings.
  43. options['secret_key'] = get_random_secret_key()
  44. # Handle custom template logic
  45. import wagtailcrx
  46. codered_path = os.path.dirname(wagtailcrx.__file__)
  47. if not options['template']:
  48. options['template'] = 'basic'
  49. template_path = os.path.join(
  50. os.path.join(codered_path, 'project_template'),
  51. options['template']
  52. )
  53. # Check if provided template is built-in to wagtailcrx,
  54. # otherwise, do not change it.
  55. if os.path.isdir(template_path):
  56. options['template'] = template_path
  57. # Treat these files as Django templates to render the boilerplate.
  58. options['extensions'] = ['py', 'md', 'txt']
  59. options['files'] = ['Dockerfile']
  60. # Set options
  61. message = "Creating a Wagtail CRX project called %(project_name)s"
  62. if options.get('sitename'):
  63. message += " for %(sitename)s"
  64. else:
  65. options['sitename'] = project_name
  66. if options.get('domain'):
  67. message += " (%(domain)s)"
  68. # Stip protocol out of domain if it is present.
  69. options['domain'] = options['domain'].split('://')[-1]
  70. # Figure out www logic.
  71. if options['domain'].startswith('www.'):
  72. options['domain_nowww'] = options['domain'].split('www.')[-1]
  73. else:
  74. options['domain_nowww'] = options['domain']
  75. else:
  76. options['domain'] = 'localhost'
  77. options['domain_nowww'] = options['domain']
  78. # Add additional custom options to the context.
  79. options['wagtailcrx_release'] = wagtailcrx.release
  80. # Print a friendly message
  81. print(message % {
  82. 'project_name': project_name,
  83. 'sitename': options.get('sitename'),
  84. 'domain': options.get('domain'),
  85. })
  86. # Run command
  87. super().handle('project', project_name, target, **options)
  88. # Be a friend once again.
  89. print("Success! %(project_name)s has been created" % {'project_name': project_name})
  90. nextsteps = """
  91. Next steps:
  92. 1. cd %(directory)s/
  93. 2. python manage.py migrate
  94. 3. python manage.py createsuperuser
  95. 4. python manage.py runserver
  96. 5. Go to http://localhost:8000/admin/ and start editing!
  97. """
  98. print(nextsteps % {'directory': target if target else project_name})
  99. COMMANDS = {
  100. 'start': CreateProject(),
  101. }
  102. def prog_name():
  103. return os.path.basename(sys.argv[0])
  104. def help_index():
  105. print("Type '%s help <subcommand>' for help on a specific subcommand.\n" % prog_name()) # NOQA
  106. print("Available subcommands:\n") # NOQA
  107. for name, cmd in sorted(COMMANDS.items()):
  108. print(" %s%s" % (name.ljust(20), cmd.help)) # NOQA
  109. def unknown_command(command):
  110. print("Unknown command: '%s'" % command) # NOQA
  111. print("Type '%s help' for usage." % prog_name()) # NOQA
  112. sys.exit(1)
  113. def main():
  114. try:
  115. command_name = sys.argv[1]
  116. except IndexError:
  117. help_index()
  118. return
  119. if command_name == 'help':
  120. try:
  121. help_command_name = sys.argv[2]
  122. except IndexError:
  123. help_index()
  124. return
  125. try:
  126. command = COMMANDS[help_command_name]
  127. except KeyError:
  128. unknown_command(help_command_name)
  129. return
  130. command.print_help(prog_name(), help_command_name)
  131. return
  132. try:
  133. command = COMMANDS[command_name]
  134. except KeyError:
  135. unknown_command(command_name)
  136. return
  137. command.run_from_argv(sys.argv)
  138. if __name__ == "__main__":
  139. main()