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

how_to_customise_an_app.rst 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. =======================
  2. How to customise an app
  3. =======================
  4. A core part of how Oscar can be customised is to create a local version of one
  5. of Oscar's apps so that it can be modified and extended. Creating a local
  6. version of an app allows customisation of any of the classes within the
  7. corresponding app in oscar.
  8. The way this is done involves a few steps, which are detailed here.
  9. Method
  10. ======
  11. 1. Create an app within your project with the same "app label" as an app in oscar. Eg,
  12. to create a local version of ``oscar.apps.order``, create something like ``myproject.order``.
  13. 2. Ensure the ``models.py`` in your local app imports all the models from Oscar's version::
  14. # models.py
  15. from oscar.apps.order.models import *
  16. 3. Replace Oscar's version of the app with your new version in ``INSTALLED_APPS``.
  17. Worked example
  18. ==============
  19. Suppose you want to modify the homepage view class, which by default is defined in
  20. ``oscar.apps.promotions.views.HomeView``. This view is bound to a URL within the
  21. ``PromotionsApplication`` class in ``oscar.apps.promotions.app`` - hence we need to
  22. override this application class to be able to use a different view.
  23. By default, your base ``urls.py`` should include Oscar's URLs as so::
  24. # urls.py
  25. from oscar.app import application
  26. urlpatterns = patterns('',
  27. ...
  28. (r'', include(application.urls)),
  29. )
  30. To get control over the mapping between URLs and views, you need to use a local
  31. ``application`` instance, that (optionally) subclasses Oscar's. Hence, create
  32. ``myproject/app.py`` with contents::
  33. # myproject/app.py
  34. from oscar.app import Shop
  35. class BaseApplication(Shop):
  36. pass
  37. application = BaseApplication()
  38. No customisation for now, that will come later, but you now have control over which
  39. URLs and view functions are used.
  40. Now hook this up in your ``urls.py``::
  41. # urls.py
  42. from myproject.app import application
  43. urlpatterns = patterns('',
  44. ...
  45. (r'', include(application.urls)),
  46. )
  47. The next step is to create a local app with the same name as the app you want to override::
  48. mkdir myproject/promotions
  49. touch myproject/promotions/__init__.py
  50. touch myproject/promotions/models.py
  51. The ``models.py`` file should import all models from the oscar app being overridden::
  52. # myproject/promotions/models.py
  53. from oscar.apps.promotions.models import *
  54. Now replace ``oscar.apps.promotions`` with ``myproject.promotions`` in the ``INSTALLED_APPS``
  55. setting in your settings file.
  56. Now create a new homepage view class in ``myproject.promotions.views`` - you can subclass
  57. Oscar's view if you like::
  58. from oscar.apps.promotions.views import HomeView as CoreHomeView
  59. class HomeView(CoreHomeView):
  60. template_name = 'promotions/new-homeview.html'
  61. In this example, we set a new template location but it's possible to customise the view
  62. in any imaginable way.
  63. Next, create a new ``app.py`` for your local promotions app which maps your new ``HomeView``
  64. class to the homepage URL::
  65. # myproject/promotions/app.py
  66. from oscar.apps.promotions import PromotionsApplication as CorePromotionsApplication
  67. from myproject.promotions.views import HomeView
  68. class PromotionsApplication(CorePromotionsApplication):
  69. home_view = HomeView
  70. application = PromotionsApplication()
  71. Finally, hook up the new view to the homepage URL::
  72. # myproject/app.py
  73. from oscar.app import Shop
  74. from myproject.promotions.app import application as promotions_app
  75. class BaseApplication(Shop):
  76. promotions_app = promotions_app
  77. Quite long-winded, but once this step is done, you have lots of freedom to customise
  78. the app in question.
  79. Django admin
  80. ------------
  81. One pain point with replacing one of Oscar's apps with a local one in
  82. ``INSTALLED_APPS`` is that admin integration is lost from the original
  83. app. If you'd like to use the Django admin functionality you just need
  84. to run the register code in the replaced app's ``admin.py``::
  85. # myprojects/promotions/admin.py
  86. import oscar.apps.promotions.admin
  87. This isn't great but we haven't found a better way as of yet.