coderedcms.py 5.1 KB

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