Permalink
Browse files

Return a real queryset from get_recommendations

  • Loading branch information...
dcramer committed Jun 22, 2010
1 parent d2f4dbc commit 80e9eec37afd39070bae29e35fe0ac73dd124b2a
Showing with 38 additions and 24 deletions.
  1. +7 −0 djangoratings/management/commands/update_recommendations.py
  2. +24 −17 djangoratings/managers.py
  3. +7 −7 djangoratings/tests.py
@@ -0,0 +1,7 @@
+from django.core.management.base import NoArgsCommand, CommandError
+
+from djangoratings.models import SimilarUser
+
+class Command(NoArgsCommand):
+ def handle_noargs(self, **options):
+ SimilarUser.objects.update_recommendations()
View
@@ -46,27 +46,34 @@ def get_for_user_in_bulk(self, objects, user):
return vote_dict
class SimilarUserManager(Manager):
- def get_recommendations(self, user, model_class, min_score=1, offset=0, limit=10):
+ def get_recommendations(self, user, model_class, min_score=1):
from djangoratings.models import Vote, IgnoredObject
content_type = ContentType.objects.get_for_model(model_class)
+
+ params = dict(
+ v=Vote._meta.db_table,
+ sm=self.model._meta.db_table,
+ m=model_class._meta.db_table,
+ )
+
+ objects = model_class._default_manager.extra(
+ tables=[params['v']],
+ where=[
+ '%(v)s.object_id = %(m)s.id and %(v)s.content_type_id = %%s' % params,
+ '%(v)s.user_id IN (select to_user_id from %(sm)s where from_user_id = %%s and exclude = 0)' % params,
+ '%(v)s.score >= %%s' % params,
+ ],
+ params=[content_type.id, user.id, min_score]
+ ).distinct()
- votes = content_type.votes.extra(
- where = ['user_id IN (select to_user_id from %s where from_user_id = %%d and exclude = 0)' % (self.model._meta.db_table,)],
- params = [user.id],
- ).filter(score__gte=min_score).exclude(
- object_id__in=IgnoredObject.objects.filter(content_type=content_type, user=user).values_list('object_id', flat=True),
- ).exclude(
- object_id__in=Vote.objects.filter(content_type=content_type, user=user).values_list('object_id', flat=True)
- ).distinct().values_list('object_id', flat=True)[offset:limit]
-
- # Thank you Django, for not working.. ever
- from django.db import connection
- cursor = connection.cursor()
- cursor.execute(str(votes.query))
- object_ids = list(r[0] for r in cursor.fetchall())
-
- objects = list(model_class._default_manager.filter(pk__in=object_ids))
+ # objects = model_class._default_manager.filter(pk__in=content_type.votes.extra(
+ # where=['user_id IN (select to_user_id from %s where from_user_id = %d and exclude = 0)' % (self.model._meta.db_table, user.pk)],
+ # ).filter(score__gte=min_score).exclude(
+ # object_id__in=IgnoredObject.objects.filter(content_type=content_type, user=user).values_list('object_id', flat=True),
+ # ).exclude(
+ # object_id__in=Vote.objects.filter(content_type=content_type, user=user).values_list('object_id', flat=True)
+ # ).distinct().values_list('object_id', flat=True))
return objects
View
@@ -89,25 +89,25 @@ def testExclusions(self):
self.assertEquals(SimilarUser.objects.count(), 2)
- recs = SimilarUser.objects.get_recommendations(self.user2, RatingTestModel)
+ recs = list(SimilarUser.objects.get_recommendations(self.user2, RatingTestModel))
self.assertEquals(len(recs), 4)
ct = ContentType.objects.get_for_model(RatingTestModel)
IgnoredObject.objects.create(user=self.user2, content_type=ct, object_id=self.instance2.pk)
- recs = SimilarUser.objects.get_recommendations(self.user2, RatingTestModel)
+ recs = list(SimilarUser.objects.get_recommendations(self.user2, RatingTestModel))
self.assertEquals(len(recs), 3)
IgnoredObject.objects.create(user=self.user2, content_type=ct, object_id=self.instance3.pk)
IgnoredObject.objects.create(user=self.user2, content_type=ct, object_id=self.instance4.pk)
- recs = SimilarUser.objects.get_recommendations(self.user2, RatingTestModel)
+ recs = list(SimilarUser.objects.get_recommendations(self.user2, RatingTestModel))
self.assertEquals(len(recs), 1)
self.assertEquals(recs, [self.instance5])
self.instance5.rating.add(score=1, user=self.user2, ip_address='127.0.0.2')
- recs = SimilarUser.objects.get_recommendations(self.user2, RatingTestModel)
+ recs = list(SimilarUser.objects.get_recommendations(self.user2, RatingTestModel))
self.assertEquals(len(recs), 0)
def testSimilarUsers(self):
@@ -126,7 +126,7 @@ def testSimilarUsers(self):
self.assertEquals(SimilarUser.objects.count(), 2)
- recs = SimilarUser.objects.get_recommendations(self.user2, RatingTestModel)
+ recs = list(SimilarUser.objects.get_recommendations(self.user2, RatingTestModel))
self.assertEquals(len(recs), 2)
self.instance4.rating.add(score=1, user=self.user2, ip_address='127.0.0.2')
@@ -135,7 +135,7 @@ def testSimilarUsers(self):
self.assertEquals(SimilarUser.objects.count(), 2)
- recs = SimilarUser.objects.get_recommendations(self.user2, RatingTestModel)
+ recs = list(SimilarUser.objects.get_recommendations(self.user2, RatingTestModel))
self.assertEquals(len(recs), 1)
self.assertEquals(recs, [self.instance5])
@@ -145,5 +145,5 @@ def testSimilarUsers(self):
self.assertEquals(SimilarUser.objects.count(), 2)
- recs = SimilarUser.objects.get_recommendations(self.user2, RatingTestModel)
+ recs = list(SimilarUser.objects.get_recommendations(self.user2, RatingTestModel))
self.assertEquals(len(recs), 0)

0 comments on commit 80e9eec

Please sign in to comment.