david / django-roa (http://welldev.org/)
Turn your models into remote resources that you can access through Django's ORM. ROA stands for Resource Oriented Architecture.
| commit 11: | 85aa0c36b11a |
| parent 10: | 0df0b02c7df3 |
| branch: | default |
Handle count the rigth way, useful for admin
19 months ago
Changed (Δ289 bytes):
raw changeset »
django_roa/db/query.py (19 lines added, 21 lines removed)
test_projects/django_roa_server/urls.py (0 lines added, 6 lines removed)
Up to file-list django_roa/db/query.py:
| … | … | @@ -54,7 +54,10 @@ class Query(object): |
54 |
54 |
|
55 |
55 |
# Ordering |
56 |
56 |
if self.order_by: |
57 |
|
|
57 |
order_by = ','.join(self.order_by) |
|
58 |
if 'remotemodel_ptr' in order_by: |
|
59 |
order_by = order_by.replace('remotemodel_ptr', 'id') |
|
60 |
parameters['order_by'] = order_by |
|
58 |
61 |
|
59 |
62 |
# Slicing |
60 |
63 |
if self.limit_start: |
| … | … | @@ -126,28 +129,23 @@ class RemoteQuerySet(query.QuerySet): |
126 |
129 |
def count(self): |
127 |
130 |
""" |
128 |
131 |
Returns the number of records as an integer. |
129 |
||
130 |
If the QuerySet is already fully cached this simply returns the length |
|
131 |
|
|
132 |
||
133 |
The result is not cached nor comes from cache, cache must be handled |
|
134 |
by the server. |
|
132 |
135 |
""" |
133 |
if self._result_cache is not None and not self._iter: |
|
134 |
return len(self._result_cache) |
|
135 |
||
136 |
self.query.get_count() |
|
137 |
||
138 |
resource = Resource(self.model._meta.resource_url_list) |
|
139 |
||
136 |
140 |
try: |
137 |
# We must force iteration otherwise admin paginator does not work |
|
138 |
return len(list(self.iterator())) |
|
139 |
except TypeError: |
|
140 |
# object of type 'generator' has no len() |
|
141 |
self.query.get_count() |
|
142 |
||
143 |
resource = Resource(self.model._meta.resource_url_list) |
|
144 |
||
145 |
try: |
|
146 |
response = resource.get(**self.query.parameters) |
|
147 |
except ResourceNotFound: |
|
148 |
return 0 |
|
149 |
||
150 |
return int(response) |
|
141 |
response = resource.get(**self.query.parameters) |
|
142 |
except ResourceNotFound: |
|
143 |
response = 0 |
|
144 |
||
145 |
# We must manually set count to False, self.query is shared |
|
146 |
self.query.count = False |
|
147 |
||
148 |
return int(response) |
|
151 |
149 |
|
152 |
150 |
def latest(self, field_name=None): |
153 |
151 |
""" |
Up to file-list test_projects/django_roa_server/urls.py:
| … | … | @@ -3,12 +3,6 @@ from django.conf.urls.defaults import * |
3 |
3 |
|
4 |
4 |
from django_roa_server.views import MethodDispatcher |
5 |
5 |
|
6 |
||
7 |
6 |
urlpatterns = patterns('', |
8 |
7 |
(r'^(?P<app_label>[_\w]+)/(?P<model_name>[_\w]+)/?(?P<object_id>\d+)?/?$', MethodDispatcher()), |
9 |
8 |
) |
10 |
||
11 |
#urlpatterns = patterns('django_roa_server.views', |
|
12 |
# (r'^([^/]+)/([^/]+)/?$', 'resource'), |
|
13 |
# (r'^([^/]+)/([^/]+)/(.+)/?$', 'resource_id'), |
|
14 |
#) |
