david / django-modelviews
Backup of an old repository with useful ideas. Initial goal: integrating REST to django admin (class-based views).
Clone this repository (size: 85.8 KB): HTTPS / SSH
$ hg clone http://code.welldev.org/django-modelviews
| commit 48: | b31a15f01c89 |
| parent 47: | b04c6c1a9f26 |
| branch: | default |
Fleshed out the Responders portion out of the documentation. Although not the RSS/Atom parts, yet.
2 years ago
Changed (Δ3.8 KB):
raw changeset »
docs/modelviews.txt (94 lines added, 0 lines removed)
generic/responders.py (1 lines added, 0 lines removed)
Up to file-list docs/modelviews.txt:
| … | … | @@ -70,21 +70,115 @@ Nested resources |
70 |
70 |
Responders |
71 |
71 |
========== |
72 |
72 |
|
73 |
A responder is a class that subclasses, ``django_modelview.generic.responders.BaseResponder`` |
|
74 |
and whose class name must end with ``Responder``. A given ``ModelView`` may have any number |
|
75 |
of responders, which are specified using the ``responders`` parameter as follows: |
|
76 |
||
77 |
from django_modelview.generic.rest_views import * |
|
78 |
responders = (HtmlResponder, JsonResponder, YamlResponder) |
|
79 |
articles = ModelView(Article.objects.all(), |
|
80 |
responders=responders, |
|
81 |
methods=('GET',)) |
|
82 |
||
83 |
The first responder will be used as the default if another responder is not explicitly |
|
84 |
chosen. Thus, in the above example ``HtmlResponder`` will be used as the default |
|
85 |
responder. |
|
86 |
||
87 |
In addition, each responder is also associated with one or more mimetypes. |
|
88 |
These mimetypes are used for determining the appropriate responder to use |
|
89 |
to respond to a request. Thus, there are three ways that a ``ModelView`` |
|
90 |
will attempt to discover the appropriate responder for a given request: |
|
91 |
||
92 |
1) If ``format`` is explicitly set in the request's url, it will use that format. |
|
93 |
||
94 |
mv = ModelView(Article.objects.all(), |
|
95 |
responders=(HtmlResponder,JsonResponder,YamlResponder), |
|
96 |
methods=('GET',)) |
|
97 |
('^article/(?P<format>(html|json|yaml))?/?', mv) |
|
98 |
||
99 |
Thus, an incoming request that looks like ``/article/html/`` would always |
|
100 |
use the ``HtmlResponder``. |
|
101 |
||
102 |
2) If the ``format`` is not explicitly made known, the ``ModelView`` will |
|
103 |
attempt to determine the correct responder using the request's mimetype. |
|
104 |
For example, a request with the mimetype ``text/yaml`` would be responded |
|
105 |
to using the ``YamlResponder``. The full list of associated mimetypes are |
|
106 |
discussed below. |
|
107 |
||
108 |
3) Finally, if there is no explicit ``format`` in the url, nor is there a |
|
109 |
matching mimetype, then the default responder--the first in the list or |
|
110 |
tuple passed to the ``responders`` argument of the ``ModelView`` |
|
111 |
initializer--will be used instead. |
|
112 |
||
113 |
||
73 |
114 |
BaseResponder |
74 |
115 |
------------- |
75 |
116 |
|
117 |
``BaseResponder`` is the foundation of all the other responder classes. |
|
118 |
||
119 |
||
120 |
Subclassing BaseResponder |
|
121 |
------------------------- |
|
122 |
||
123 |
If the existing responders are inadequate for your application, it is |
|
124 |
easy to subclass ``BaseResponder``. You will need to override two methods: |
|
125 |
||
126 |
1) ``element(self, request, obj)`` returns an ``HttpResponse`` containing the |
|
127 |
appropriate rendering of a single instance, ``obj``. |
|
128 |
||
129 |
2) ``render_list(self, request, object_list, paginator, page_obj)`` or |
|
130 |
``list(self, request, paginate_by, allow_empty)``. Only one of these |
|
131 |
two methods will need to be overridden. |
|
132 |
||
133 |
If possible, it is preferable to override ``render_list``, which has |
|
134 |
already restricted the paramter ``object_list`` to viewable instances, |
|
135 |
and has handled most pagination logic as well. |
|
136 |
||
137 |
``list`` on the other hand does none of this logic for you, and you must |
|
138 |
implement all such restrictions and partitioning on your own. |
|
139 |
||
140 |
Both ``list`` and ``render_list`` should return an ``HttpResponder`` object. |
|
141 |
||
142 |
||
76 |
143 |
HtmlResponder |
77 |
144 |
------------- |
78 |
145 |
|
146 |
The ``HtmlResponder`` renders objects using the Django template system. |
|
147 |
If attempting to render a list of objects, it was try to use the |
|
148 |
``appname/modelname_list.html`` template. If attempting to render a |
|
149 |
single object, it will try to use the ``appname/modelname_detail.html`` |
|
150 |
template. |
|
151 |
||
152 |
Is associated with the ``text/html`` mimetype. |
|
153 |
||
154 |
||
79 |
155 |
JsonResponder |
80 |
156 |
------------- |
81 |
157 |
|
158 |
The ``JsonResponder`` serializes the response using the Json |
|
159 |
serializer at ``django.core.serializers.json``. |
|
160 |
||
161 |
It is associated with the ``application/json`` mimetype. |
|
162 |
||
163 |
||
82 |
164 |
XmlResponder |
83 |
165 |
------------ |
84 |
166 |
|
167 |
The ``XmlResponder`` serializes the response using the XML |
|
168 |
serializer at ``django.core.serializers.xml_serializer``. |
|
169 |
||
170 |
It is associated with the ``application/xml`` mimetype. |
|
171 |
||
172 |
||
85 |
173 |
YamlResponder |
86 |
174 |
------------- |
87 |
175 |
|
176 |
The ``YamlResponder`` serializes the response using the Yaml |
|
177 |
serializer at ``django.core.serializers.pyyaml``. |
|
178 |
||
179 |
It is associated with the ``text/yaml`` mimetype. |
|
180 |
||
181 |
||
88 |
182 |
AtomResponder |
89 |
183 |
------------- |
90 |
184 |
Up to file-list generic/responders.py:
| … | … | @@ -98,6 +98,7 @@ class HtmlResponder(BaseResponder): |
98 |
98 |
HtmlResponder renders an object or a list of objects with the Django |
99 |
99 |
templating system. |
100 |
100 |
""" |
101 |
mimetype = "text/html" |
|
101 |
102 |
def render_response(self, request, template, context_vars, mimetype=None): |
102 |
103 |
""" |
103 |
104 |
Returns an HttpResponse for the given request, template object, |
