Procházet zdrojové kódy

Docs: Updates for dynamically loaded applications

The customisation section required some changes as it's not often
necessary to override the root application instance to change URLs or
views of subapps.
master
Maik Hoepfel před 12 roky
rodič
revize
e579cf4e61

+ 18
- 3
docs/source/howto/how_to_change_a_url.rst Zobrazit soubor

@@ -11,13 +11,15 @@ It builds upon the steps described in :doc:`/topics/customisation`. Please
11 11
 read it first and ensure that you've:
12 12
 
13 13
 * Created a Python module with the the same label
14
-* Added it as Django app
14
+* Added it as Django app to ``INSTALLED_APPS``
15
+* Created a custom ``app.py``
15 16
 
16 17
 Example
17 18
 -------
18 19
 
19
-In order to customise Oscar's URLs, you need to use a custom app instance in
20
-your root ``urls.py`` instead of Oscar's default instance.  Hence, to use
20
+In order to customise Oscar's URLs, you need to use a custom app instance
21
+instead of Oscar's default instance.  ``/catalogue`` is wired up in the root
22
+application, so we need to replace that. Hence, to use
21 23
 ``catalog`` instead of ``catalogue``, create a subclass of Oscar's main
22 24
 ``Application`` class and override the ``get_urls`` method::
23 25
 
@@ -48,3 +50,16 @@ Now modify your root ``urls.py`` to use your new application instance::
48 50
     )
49 51
 
50 52
 All URLs containing ``catalogue`` previously are now displayed as ``catalog``.
53
+
54
+If you wanted to change URLs of a sub-app (e.g. ``/catalogue/category/``),
55
+you only need to replace the ``catalogue`` app. There's no need to change
56
+your ``urls.py`` or touch the root ``application`` instance. ``application``
57
+instances dynamically load their sub-apps, so it just pick up your replacement::
58
+
59
+    # oscar/app.py
60
+    class Shop(Application):
61
+        name = None
62
+
63
+        catalogue_app = get_class('catalogue.app', 'application')
64
+        customer_app = get_class('customer.app', 'application')
65
+        ...

+ 2
- 1
docs/source/howto/how_to_customise_a_view.rst Zobrazit soubor

@@ -7,7 +7,8 @@ your project.  It builds upon the steps described in
7 7
 :doc:`/topics/customisation`. Please read it first and ensure that you've:
8 8
 
9 9
 * Created a Python module with the the same label
10
-* Use custom root and local ``app.py``
10
+* Added it as Django app to ``INSTALLED_APPS``
11
+* Use custom ``app.py``
11 12
 
12 13
 Example
13 14
 -------

+ 1
- 1
docs/source/howto/how_to_override_a_core_class.rst Zobrazit soubor

@@ -11,7 +11,7 @@ It builds upon the steps described in :doc:`/topics/customisation`. Please
11 11
 read it first and ensure that you've:
12 12
 
13 13
 * Created a Python module with the the same label
14
-* Added it as Django app
14
+* Added it as Django app to ``INSTALLED_APPS``
15 15
 
16 16
 Example
17 17
 -------

+ 35
- 38
docs/source/topics/customisation.rst Zobrazit soubor

@@ -14,21 +14,19 @@ doesn't stop there. Almost every aspect of it can be altered.
14 14
 :doc:`Various techniques </internals/design-decisions>` are employed to achieve
15 15
 that level of adaptability.
16 16
 
17
-To extend the behavior of a Oscar core app, you will at least need to create an
17
+To extend the behavior of an Oscar core app, you will at least need to create an
18 18
 app with the same label. Depending on what should be adapted, different steps
19 19
 are necessary beyond that. The steps are detailed below; this overview might
20 20
 help you to figure out what needs to be done.
21 21
 
22
-==================================  ====================  ====================================  ========================
23
-Goals vs. necessary steps           Override model class  Override view class (or change URLs)  Override any other class
24
-==================================  ====================  ====================================  ========================
25
-Python module with same label       Necessary             Necessary                             Necessary
26
-Custom root and local ``app.py``    Not necessary         Necessary                             Not necessary
27
-Add as Django app                   Necessary             Not necessary                         Necessary
28
-==================================  ====================  ====================================  ========================
22
+================================  =============================  =================  =================
23
+Goals vs. necessary steps         Python module with same label  Add as Django app  Custom ``app.py``
24
+================================  =============================  =================  =================
25
+Override a model class            Necessary                      Necessary          Not necessary
26
+Override any other class or view  Necessary                      Necessary          Not necessary
27
+Change app URLs or add views      Necessary                      Necessary          Necessary
28
+================================  =============================  =================  =================
29 29
 
30
-If more complex changes are desired, it is usually easiest to do all of the
31
-steps.
32 30
 Please also refer to the following how-tos for further instructions and examples.
33 31
 
34 32
 * :doc:`/howto/how_to_customise_models`
@@ -47,16 +45,13 @@ E.g., to create a local version of ``oscar.apps.order``, do the following::
47 45
     $ touch yourproject/order/__init__.py
48 46
 
49 47
 
50
-Custom root and local ``app.py``
51
-================================
52
-
53
-Root ``app.py``
54
----------------
48
+Custom ``app.py``
49
+=================
55 50
 
56 51
 Oscar's views and URLs use a tree of 'app' instances, each of which subclass
57 52
 :class:`oscar.core.application.Application` and provide ``urls`` property.
58
-Oscar has a root app instance in ``oscar/app.py`` which can be imported into
59
-your ``urls.py``::
53
+Oscar has a root app instance in ``oscar/app.py`` which should already be
54
+wired up in your ``urls.py``::
60 55
 
61 56
     # urls.py
62 57
     from oscar.app import application
@@ -66,9 +61,12 @@ your ``urls.py``::
66 61
        (r'', include(application.urls)),
67 62
     )
68 63
 
69
-To get control over the mapping between URLs and views, you need to use a local
70
-``application`` instance, that (usually) subclasses Oscar's.  Hence, create
71
-``yourproject/app.py`` with contents::
64
+Modifying root app
65
+------------------
66
+
67
+If you want to change URLs or views of the root application above, you need to
68
+replace it with your own ``application`` instance, that (usually) subclasses
69
+Oscar's.  Hence, create ``yourproject/app.py`` with contents::
72 70
 
73 71
     # yourproject/app.py
74 72
     from oscar.app import Shop
@@ -89,34 +87,33 @@ Now hook this up in your ``urls.py`` instead::
89 87
         (r'', include(application.urls)),
90 88
     )
91 89
 
92
-This step only needs to be done once. All customisation will only entail
93
-overriding parts of the newly added ``BaseApplication``.
90
+Modifying sub-apps
91
+------------------
94 92
 
95
-Local ``app.py``
96
-----------------
93
+Sub-apps such as the ``catalogue`` app are loaded dynamically, just as most
94
+other classes in Oscar::
97 95
 
98
-If you want to modify a view or change a URL, you need to create an ``app.py``
99
-for your local app. It will usually inherit from Oscar's version::
96
+    # oscar/app.py
97
+    class Shop(Application):
98
+        name = None
100 99
 
101
-    # yourproject/order/app.py
100
+        catalogue_app = get_class('catalogue.app', 'application')
101
+        customer_app = get_class('customer.app', 'application')
102
+        ...
103
+
104
+That means you can leave the root app unchanged, and just need to create another
105
+``application`` instance. It will usually inherit from Oscar's version::
106
+
107
+    # yourproject/promotions/app.py
102 108
 
103 109
     from oscar.apps.promotions.app import PromotionsApplication as CorePromotionsApplication
110
+    from .views import MyExtraView
104 111
 
105 112
     class PromotionsApplication(CorePromotionsApplication):
106
-        pass
113
+        extra_view = MyExtraView
107 114
 
108 115
     application = PromotionsApplication()
109 116
 
110
-and hook it up in your root ``app.py``::
111
-
112
-    # yourproject/app.py
113
-    from oscar.app import Shop
114
-
115
-    from yourproject.promotions.app import application as promotions_app
116
-
117
-    class BaseApplication(Shop):
118
-        promotions_app = promotions_app
119
-
120 117
 
121 118
 Add as Django app
122 119
 =================

Načítá se…
Zrušit
Uložit