You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

customisation.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from __future__ import absolute_import
  2. import os
  3. from os.path import join, exists
  4. import shutil
  5. import logging
  6. import textwrap
  7. import oscar
  8. def create_local_app_folder(local_app_path):
  9. if exists(local_app_path):
  10. raise ValueError(
  11. "There is already a '%s' folder! Aborting!" % local_app_path)
  12. os.mkdir(local_app_path)
  13. def inherit_app_config(local_app_path, app_package, app_label):
  14. # This only works for non-dashboard apps; but then the fork_app command
  15. # doesn't support them anyway
  16. config_name = app_label.title() + 'Config'
  17. create_file(
  18. join(local_app_path, '__init__.py'),
  19. "default_app_config = '{app_package}.config.{config_name}'\n".format(
  20. app_package=app_package, config_name=config_name))
  21. create_file(
  22. join(local_app_path, 'config.py'),
  23. "from oscar.apps.{app_label} import config\n\n\n"
  24. "class {config_name}(config.{config_name}):\n"
  25. " name = '{app_package}'\n".format(
  26. app_package=app_package,
  27. app_label=app_label,
  28. config_name=config_name))
  29. def fork_app(app_label, folder_path, logger=None):
  30. """
  31. Create a custom version of one of Oscar's apps
  32. """
  33. if logger is None:
  34. logger = logging.getLogger(__name__)
  35. # Check app_label is valid
  36. app_labels = [x.split('.').pop() for x in oscar.OSCAR_CORE_APPS if
  37. x.startswith('oscar')]
  38. if app_label not in app_labels:
  39. raise ValueError("There is no Oscar app with label '%s'" % app_label)
  40. # Check folder exists
  41. if not exists(folder_path):
  42. raise ValueError(
  43. "The folder '%s' does not exist. Please create it then run this "
  44. "command again" % folder_path)
  45. # Create folder
  46. local_app_path = join(folder_path, app_label)
  47. oscar_app_path = join(oscar.__path__[0], 'apps', app_label)
  48. logger.info("Creating folder %s" % local_app_path)
  49. create_local_app_folder(local_app_path)
  50. # Create minimum app files
  51. app_package = local_app_path.replace('/', '.')
  52. logger.info("Enabling Django admin integration")
  53. create_file(join(local_app_path, 'admin.py'),
  54. "from oscar.apps.%s.admin import * # noqa\n" % app_label)
  55. logger.info("Inheriting app config")
  56. inherit_app_config(local_app_path, app_package, app_label)
  57. # Only create models.py and migrations if it exists in the Oscar app
  58. oscar_models_path = join(oscar_app_path, 'models.py')
  59. if exists(oscar_models_path):
  60. logger.info(
  61. "Creating models.py and copying South and native migrations")
  62. create_file(
  63. join(local_app_path, 'models.py'),
  64. "from oscar.apps.%s.models import * # noqa\n" % app_label)
  65. for migrations_path in ['migrations', 'south_migrations']:
  66. source = join(oscar_app_path, migrations_path)
  67. destination = join(local_app_path, migrations_path)
  68. shutil.copytree(source, destination)
  69. # Final step needs to be done by hand
  70. msg = (
  71. "The final step is to add '%s' to INSTALLED_APPS "
  72. "(replacing the equivalent Oscar app). This can be "
  73. "achieved using Oscar's get_core_apps function - e.g.:"
  74. ) % app_package
  75. snippet = (
  76. " # settings.py\n"
  77. " ...\n"
  78. " INSTALLED_APPS = [\n"
  79. " 'django.contrib.auth',\n"
  80. " ...\n"
  81. " ]\n"
  82. " from oscar import get_core_apps\n"
  83. " INSTALLED_APPS = INSTALLED_APPS + get_core_apps(\n"
  84. " ['%s'])"
  85. ) % app_package
  86. record = "\n%s\n\n%s" % (
  87. "\n".join(textwrap.wrap(msg)), snippet)
  88. logger.info(record)
  89. def create_file(filepath, content=''):
  90. with open(filepath, 'w') as f:
  91. f.write(content)