coderedcms.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 wagtail
  41. import coderedcms
  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. # Assume all files are NOT Django templates.
  53. options["extensions"] = ["toml"]
  54. # Treat these files as Django templates to render the boilerplate.
  55. options["files"] = [
  56. "0002_initial_data.py",
  57. "base.py",
  58. "dev.py",
  59. "manage.py",
  60. "prod.py",
  61. "README.md",
  62. "requirements.txt",
  63. "staging.py",
  64. "wsgi.py",
  65. ]
  66. # Set options
  67. message = "Creating a Wagtail CRX project called %(project_name)s"
  68. if options.get("sitename"):
  69. message += " for %(sitename)s"
  70. else:
  71. options["sitename"] = project_name
  72. if options.get("domain"):
  73. message += " (%(domain)s)"
  74. # Strip protocol out of domain if it is present.
  75. options["domain"] = options["domain"].split("://")[-1]
  76. # Figure out www logic.
  77. if options["domain"].startswith("www."):
  78. options["domain_nowww"] = options["domain"].split("www.")[-1]
  79. else:
  80. options["domain_nowww"] = options["domain"]
  81. else:
  82. options["domain"] = "localhost"
  83. options["domain_nowww"] = options["domain"]
  84. # Add additional custom options to the context.
  85. options["coderedcms_release"] = coderedcms.release
  86. options["wagtail_release"] = wagtail.VERSION
  87. # Print a friendly message
  88. print(
  89. message
  90. % {
  91. "project_name": project_name,
  92. "sitename": options.get("sitename"),
  93. "domain": options.get("domain"),
  94. }
  95. )
  96. # Run command
  97. super().handle("project", project_name, target, **options)
  98. # Be a friend once again.
  99. print(
  100. "Success! %(project_name)s has been created"
  101. % {"project_name": project_name}
  102. )
  103. nextsteps = """
  104. Next steps:
  105. 1. cd %(directory)s/
  106. 2. python manage.py migrate
  107. 3. python manage.py createsuperuser
  108. 4. python manage.py runserver
  109. 5. Go to http://localhost:8000/admin/ and start editing!
  110. """
  111. print(nextsteps % {"directory": target if target else project_name})
  112. COMMANDS = {
  113. "start": CreateProject(),
  114. }
  115. def prog_name():
  116. return os.path.basename(sys.argv[0])
  117. def help_index():
  118. print(
  119. "Type '%s help <subcommand>' for help on a specific subcommand.\n"
  120. % prog_name()
  121. )
  122. print("Available subcommands:\n")
  123. for name, cmd in sorted(COMMANDS.items()):
  124. print(" %s%s" % (name.ljust(20), cmd.help))
  125. def unknown_command(command):
  126. print("Unknown command: '%s'" % command)
  127. print("Type '%s help' for usage." % prog_name())
  128. sys.exit(1)
  129. def main():
  130. try:
  131. command_name = sys.argv[1]
  132. except IndexError:
  133. help_index()
  134. return
  135. if command_name == "help":
  136. try:
  137. help_command_name = sys.argv[2]
  138. except IndexError:
  139. help_index()
  140. return
  141. try:
  142. command = COMMANDS[help_command_name]
  143. except KeyError:
  144. unknown_command(help_command_name)
  145. return
  146. command.print_help(prog_name(), help_command_name)
  147. return
  148. try:
  149. command = COMMANDS[command_name]
  150. except KeyError:
  151. unknown_command(command_name)
  152. return
  153. command.run_from_argv(sys.argv)
  154. if __name__ == "__main__":
  155. main()