Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

customisation.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from __future__ import absolute_import
  2. import os
  3. import shutil
  4. import logging
  5. import textwrap
  6. import oscar
  7. def fork_app(app_label, folder_path, logger=None):
  8. """
  9. Create a custom version of one of Oscar's apps
  10. """
  11. if logger is None:
  12. logger = logging.getLogger(__name__)
  13. # Check app_label is valid
  14. app_labels = [x.split('.').pop() for x in oscar.OSCAR_CORE_APPS if
  15. x.startswith('oscar')]
  16. if app_label not in app_labels:
  17. raise ValueError("There is no Oscar app with label '%s'" % app_label)
  18. # Check folder exists
  19. if not os.path.exists(folder_path):
  20. raise ValueError(
  21. "The folder '%s' does not exist. Please create it then run this "
  22. "command again")
  23. # Create folder
  24. local_app_folder_path = os.path.join(folder_path, app_label)
  25. oscar_app_folder_path = os.path.join(oscar.__path__[0], 'apps', app_label)
  26. if os.path.exists(local_app_folder_path):
  27. raise ValueError(
  28. "There is already a '%s' folder! Aborting!" %
  29. local_app_folder_path)
  30. logger.info("Creating folder %s" % local_app_folder_path)
  31. os.mkdir(local_app_folder_path)
  32. # Create minimum app files
  33. logger.info("Creating __init__.py and admin.py")
  34. create_file(os.path.join(local_app_folder_path, '__init__.py'))
  35. create_file(os.path.join(local_app_folder_path, 'admin.py'),
  36. "from oscar.apps.%s.admin import * # noqa" % app_label)
  37. # Only create models.py and migrations if it exists in the Oscar app
  38. oscar_models_path = os.path.join(oscar_app_folder_path, 'models.py')
  39. if os.path.exists(oscar_models_path):
  40. # Migrations
  41. source = os.path.join(oscar_app_folder_path, 'migrations')
  42. destination = os.path.join(local_app_folder_path, 'migrations')
  43. logger.info("Creating models.py and copying migrations from %s to %s",
  44. source, destination)
  45. shutil.copytree(source, destination)
  46. create_file(
  47. os.path.join(local_app_folder_path, 'models.py'),
  48. "from oscar.apps.%s.models import * # noqa" % app_label)
  49. # Final step needs to be done by hand
  50. app_package = local_app_folder_path.replace('/', '.')
  51. msg = (
  52. "The final step is to add '%s' to INSTALLED_APPS "
  53. "(replacing the equivalent Oscar app). This can be "
  54. "acheived using Oscar's get_core_apps function - eg:"
  55. ) % app_package
  56. snippet = (
  57. " # settings.py\n"
  58. " ...\n"
  59. " INSTALLED_APPS = [\n"
  60. " 'django.contrib.auth',\n"
  61. " ...\n"
  62. " ]\n"
  63. " from oscar import get_core_apps\n"
  64. " INSTALLED_APPS = INSTALLED_APPS + get_core_apps(\n"
  65. " ['%s'])"
  66. ) % app_package
  67. record = "\n%s\n\n%s" % (
  68. "\n".join(textwrap.wrap(msg)), snippet)
  69. logger.info(record)
  70. def create_file(filepath, content=''):
  71. with open(filepath, 'w') as f:
  72. f.write(content)