Skip to content

Commit

Permalink
Added inline APIDOX
Browse files Browse the repository at this point in the history
  • Loading branch information
vdboor committed May 23, 2011
1 parent 5c9440d commit 1495078
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions queryset_decorator/__init__.py
Expand Up @@ -2,7 +2,17 @@
from django.db.models.query import QuerySet


# Based on django-queryset-transform.
# This object however, operates on a per-object instance,
# so it doesn't break the result generators


class DecoratingQuerySet(QuerySet):
"""
An enhancement of the QuerySet which allows objects to be decorated
with extra properties before they are returned.
"""

def __init__(self, *args, **kwargs):
super(DecoratingQuerySet, self).__init__(*args, **kwargs)
self._decorate_funcs = []
Expand All @@ -12,12 +22,20 @@ def _clone(self, klass=None, setup=False, **kw):
c._decorate_funcs = self._decorate_funcs[:]
return c


def decorate(self, fn):
"""
Register a function which will decorate a retrieved object before it's returned.
"""
if fn not in self._decorate_funcs:
self._decorate_funcs.append(fn)
return self


def iterator(self):
"""
Overwritten iterator which will apply the decorate functions before returning it.
"""
base_iterator = super(DecoratingQuerySet, self).iterator()
for obj in base_iterator:
# Apply the decorators
Expand All @@ -28,5 +46,8 @@ def iterator(self):


class DecoratorManager(models.Manager):
"""
The manager class which ensures the enhanced DecoratorQuerySet object is used.
"""
def get_query_set(self):
return DecoratingQuerySet(self.model)

0 comments on commit 1495078

Please sign in to comment.