4
4
from django.contrib.contenttypes.models import ContentType
7
8
class VoteQuerySet(QuerySet):
8
10
def delete(self, *args, **kwargs):
9
"""Handles updating the related `votes` and `score` fields attached to the model."""
11
"""Handles updating the related `votes` and `score` fields attached to
10
13
# XXX: circular import
11
14
from fields import RatingField
13
qs = self.distinct().values_list('content_type', 'object_id').order_by('content_type')
16
qs = self.distinct().values_list(
17
'content_type', 'object_id').order_by('content_type')
16
20
for content_type, objects in itertools.groupby(qs, key=lambda x: x[0]):
17
model_class = ContentType.objects.get(pk=content_type).model_class()
21
model_class = ContentType.objects.get(
22
pk=content_type).model_class()
19
to_update.extend(list(model_class.objects.filter(pk__in=list(objects)[0])))
25
list(model_class.objects.filter(pk__in=list(objects)[0])))
21
27
retval = super(VoteQuerySet, self).delete(*args, **kwargs)
23
29
# TODO: this could be improved
24
30
for obj in to_update:
25
31
for field in getattr(obj, '_djangoratings', []):
26
32
getattr(obj, field.name)._update(commit=False)
31
38
class VoteManager(Manager):
32
40
def get_query_set(self):
33
41
return VoteQuerySet(self.model)
37
45
if len(objects) > 0:
38
46
ctype = ContentType.objects.get_for_model(objects[0])
39
47
votes = list(self.filter(content_type__pk=ctype.id,
40
object_id__in=[obj._get_pk_val() \
48
object_id__in=[obj._get_pk_val()
41
49
for obj in objects],
43
51
vote_dict = dict([(vote.object_id, vote) for vote in votes])
48
57
class SimilarUserManager(Manager):
49
59
def get_recommendations(self, user, model_class, min_score=1):
50
60
from djangoratings.models import Vote, IgnoredObject
52
62
content_type = ContentType.objects.get_for_model(model_class)
55
65
v=Vote._meta.db_table,
56
66
sm=self.model._meta.db_table,
57
67
m=model_class._meta.db_table,
58
68
io=IgnoredObject._meta.db_table,
61
71
objects = model_class._default_manager.extra(
62
72
tables=[params['v']],
80
90
# object_id__in=Vote.objects.filter(content_type=content_type, user=user).values_list('object_id', flat=True)
81
91
# ).distinct().values_list('object_id', flat=True))
85
95
def update_recommendations(self):
86
96
# TODO: this is mysql only atm
87
97
# TODO: this doesnt handle scores that have multiple values (e.g. 10 points, 5 stars)