~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to djangoratings/managers.py

  • Committer: franku
  • Date: 2016-12-13 18:28:51 UTC
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: somal@arcor.de-20161213182851-bo5ebf8pdvw5beua
run the script

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from django.contrib.contenttypes.models import ContentType
5
5
import itertools
6
6
 
 
7
 
7
8
class VoteQuerySet(QuerySet):
 
9
 
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
 
12
        the model."""
10
13
        # XXX: circular import
11
14
        from fields import RatingField
12
15
 
13
 
        qs = self.distinct().values_list('content_type', 'object_id').order_by('content_type')
14
 
    
 
16
        qs = self.distinct().values_list(
 
17
            'content_type', 'object_id').order_by('content_type')
 
18
 
15
19
        to_update = []
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()
18
23
            if model_class:
19
 
                to_update.extend(list(model_class.objects.filter(pk__in=list(objects)[0])))
20
 
        
 
24
                to_update.extend(
 
25
                    list(model_class.objects.filter(pk__in=list(objects)[0])))
 
26
 
21
27
        retval = super(VoteQuerySet, self).delete(*args, **kwargs)
22
 
        
 
28
 
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)
27
33
            obj.save()
28
 
        
 
34
 
29
35
        return retval
30
 
        
 
36
 
 
37
 
31
38
class VoteManager(Manager):
 
39
 
32
40
    def get_query_set(self):
33
41
        return VoteQuerySet(self.model)
34
42
 
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],
42
50
                                     user__pk=user.id))
43
51
            vote_dict = dict([(vote.object_id, vote) for vote in votes])
45
53
            vote_dict = {}
46
54
        return vote_dict
47
55
 
 
56
 
48
57
class SimilarUserManager(Manager):
 
58
 
49
59
    def get_recommendations(self, user, model_class, min_score=1):
50
60
        from djangoratings.models import Vote, IgnoredObject
51
 
        
 
61
 
52
62
        content_type = ContentType.objects.get_for_model(model_class)
53
 
        
 
63
 
54
64
        params = dict(
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,
59
69
        )
60
 
        
 
70
 
61
71
        objects = model_class._default_manager.extra(
62
72
            tables=[params['v']],
63
73
            where=[
79
89
        # ).exclude(
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))
82
 
        
 
92
 
83
93
        return objects
84
 
    
 
94
 
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)
111
121
            t2=Vote._meta.db_table,
112
122
        ))
113
123
        cursor.execute('commit')
114
 
        cursor.close()
 
 
b'\\ No newline at end of file'
 
124
        cursor.close()