Permalink
Browse files

added manager

  • Loading branch information...
1 parent 86a69b8 commit c96666ea2f159e8bd29b56dc35f7cbc04df67f10 @seanbrant seanbrant committed May 20, 2010
Showing with 149 additions and 9 deletions.
  1. +58 −0 filch/managers.py
  2. +1 −2 filch/tests/__init__.py
  3. +90 −7 filch/tests/tests.py
View
@@ -0,0 +1,58 @@
+from django.db import models
+from django.db.models.loading import get_model
+
+
+class GenericResolutionQueryset(models.query.QuerySet):
+
+ def __init__(self, *args, **kwargs):
+ super(GenericResolutionQueryset, self).__init__(*args, **kwargs)
+ self.ordering = self.model._meta.ordering
+
+ def get_content_objects(self, querysets={}, select_related=True):
+ models = {}
+ objects = {}
+ results = []
+ order_fields = self.model._meta.ordering
+ values = self.values('pk', 'object_id', 'content_type__app_label',
+ 'content_type__model', *order_fields)
+ for item in values:
+ pk = item.pop('pk')
+ object_id = item.pop('object_id')
+ app_label = item.pop('content_type__app_label')
+ model_name = item.pop('content_type__model')
+ ordering = item
+ model = get_model(app_label, model_name)
+ models.setdefault(model, {}) \
+ .setdefault(object_id, []).append(ordering)
+ for model, objs in models.items():
+ if model in querysets:
+ qs = querysets[model]
+ else:
+ qs = model._default_manager
+ object_list = qs.filter(pk__in=objs.keys())
+ for obj in object_list:
+ objects.setdefault(model, {})[obj.pk] = obj
+ for model, objs in models.items():
+ for pk, ordering in objs.items():
+ for order in ordering:
+ try:
+ fields = objects[model][pk].__dict__.copy()
+ state = fields.pop('_state')
+ obj = model(**fields)
+ obj.__temp_ordering = order
+ obj._state = state
+ results.append(obj)
+ except KeyError:
+ pass
+ for order in order_fields:
+ results.sort(lambda a, b: a.__temp_ordering[order] - \
+ b.__temp_ordering[order])
+ for result in results:
+ del result.__temp_ordering
+ return results
+
+
+class GenericResolutionManager(models.Manager):
+
+ def get_query_set(self):
+ return GenericResolutionQueryset(self.model)
@@ -1,2 +1 @@
-from filch.tests.tests import ManyToManyChangeTestCase, \
- RelatedInstanceUpdateTestCase
+from filch.tests.tests import *
View
@@ -1,10 +1,12 @@
+from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from filch.tests.models import Group, Location, Person
+from filch.tests.models import Article, HomepageItem, Press, Slot
-class BaseTestCase(TestCase):
+class DenormManyToManyFieldTestCase(TestCase):
def setUp(self):
self.person = Person.objects.create(name='Sean')
@@ -18,9 +20,6 @@ def setUp(self):
location=self.location,
)
-
-class ManyToManyChangeTestCase(object):
-
def test_many_to_many_change(self):
self.assertEquals(self.person.group_list, [])
@@ -46,9 +45,6 @@ def test_many_to_many_change(self):
self.person.groups.clear()
self.assertEquals(self.person.group_list, [])
-
-class RelatedInstanceUpdateTestCase(BaseTestCase):
-
def test_related_instance_update(self):
self.person.groups.add(self.group1)
self.assertEquals(self.person.group_list,
@@ -65,3 +61,90 @@ def test_related_instance_update(self):
self.group1.delete()
self.assertEquals(self.person.group_list, [])
+
+
+class GenericResolutionManagerTestCase(TestCase):
+
+ def setUp(self):
+ self.article1 = Article.objects.create(
+ name='Django 1.2 released!',
+ )
+ self.article2 = Article.objects.create(
+ name='Filch is a real query saver',
+ )
+ self.article3 = Article.objects.create(
+ name='Unpublished!',
+ is_published=False,
+ )
+ self.press1 = Press.objects.create(
+ name='Django taking the world by storm',
+ )
+ self.slot1 = Slot.objects.create(
+ name='main',
+ )
+ self.slot2 = Slot.objects.create(
+ name='sidebar',
+ )
+ self.main_items1 = HomepageItem.objects.create(
+ slot=self.slot1,
+ content_type=ContentType.objects.get_for_model(self.article1),
+ object_id=self.article1.id,
+ order=1,
+ )
+ self.main_items2 = HomepageItem.objects.create(
+ slot=self.slot1,
+ content_type=ContentType.objects.get_for_model(self.press1),
+ object_id=self.press1.id,
+ order=2,
+ )
+ self.main_items3 = HomepageItem.objects.create(
+ slot=self.slot1,
+ content_type=ContentType.objects.get_for_model(self.article2),
+ object_id=self.article2.id,
+ order=3,
+ )
+ self.main_items4 = HomepageItem.objects.create(
+ slot=self.slot1,
+ content_type=ContentType.objects.get_for_model(self.article1),
+ object_id=self.article1.id,
+ order=4,
+ )
+ self.main_items5 = HomepageItem.objects.create(
+ slot=self.slot1,
+ content_type=ContentType.objects.get_for_model(self.article3),
+ object_id=self.article3.id,
+ order=5,
+ )
+ self.sidebar_items1 = HomepageItem.objects.create(
+ slot=self.slot2,
+ content_type=ContentType.objects.get_for_model(self.article1),
+ object_id=self.article1.id,
+ order=1,
+ )
+ self.sidebar_items2 = HomepageItem.objects.create(
+ slot=self.slot2,
+ content_type=ContentType.objects.get_for_model(self.press1),
+ object_id=self.press1.id,
+ order=2,
+ )
+ self.sidebar_items3 = HomepageItem.objects.create(
+ slot=self.slot2,
+ content_type=ContentType.objects.get_for_model(self.article2),
+ object_id=self.article2.id,
+ order=3,
+ )
+
+ def test_get_content_objects(self):
+ items = HomepageItem.objects \
+ .filter(slot=self.slot1).get_content_objects()
+ self.assertEquals(len(items), 5)
+ # make sure ordering is working :)
+ self.assertEquals(items[1], self.press1)
+
+ def test_get_content_objects_with_custom_queryset(self):
+ items = HomepageItem.objects \
+ .filter(slot=self.slot1).get_content_objects(querysets={
+ Article: Article.published.all(),
+ })
+ print items
+ self.assertEquals(len(items), 4)

0 comments on commit c96666e

Please sign in to comment.