-
Notifications
You must be signed in to change notification settings - Fork 97
/
models.py
162 lines (128 loc) · 6.22 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from __future__ import division, unicode_literals
from decimal import Decimal
import swapper
from warnings import warn
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db.models import Avg, Count, Sum
from django.utils.translation import gettext as _
from model_utils.models import TimeStampedModel
from . import app_settings, get_star_ratings_rating_model_name, get_star_ratings_rating_model
def _clean_user(user):
if not app_settings.STAR_RATINGS_ANONYMOUS:
if not user:
raise ValueError(_("User is mandatory. Enable 'STAR_RATINGS_ANONYMOUS' for anonymous ratings."))
return user
return None
class RatingManager(models.Manager):
def for_instance(self, instance):
if isinstance(instance, self.model):
raise TypeError("Rating manager 'for_instance' expects model to be rated, not Rating model.")
ct = ContentType.objects.get_for_model(instance)
ratings, created = self.get_or_create(content_type=ct, object_id=instance.pk)
return ratings
def ratings_for_instance(self, instance):
warn("RatingManager method 'ratings_for_instance' has been renamed to 'for_instance'. Please change uses of 'Rating.objects.ratings_for_instance' to 'Rating.objects.for_instance' in your code.", DeprecationWarning)
return self.for_instance(instance)
def delete_existing(self, existing_rating):
rating = existing_rating.rating
existing_rating.delete()
rating._user_rating_deleted = True
return rating
def rate(self, instance, score, user=None, ip=None, clear=False):
if isinstance(instance, self.model):
raise TypeError("Rating manager 'rate' expects model to be rated, not Rating model.")
ct = ContentType.objects.get_for_model(instance)
user = _clean_user(user)
existing_rating = UserRating.objects.for_instance_by_user(instance, user)
if existing_rating:
if not app_settings.STAR_RATINGS_CLEARABLE and not app_settings.STAR_RATINGS_RERATE:
raise ValidationError(_('Already rated.'))
same_as_previous = existing_rating.score == score
if (app_settings.STAR_RATINGS_CLEARABLE and clear) or \
(app_settings.STAR_RATINGS_RERATE_SAME_DELETE and same_as_previous):
return self.delete_existing(existing_rating=existing_rating)
elif score is not None:
existing_rating.score = score
existing_rating.save()
return existing_rating.rating
elif clear:
# user has cleared without an existing_rating
return
else:
rating, created = self.get_or_create(content_type=ct, object_id=instance.pk)
return UserRating.objects.create(user=user, score=score, rating=rating, ip=ip).rating
class AbstractBaseRating(models.Model):
"""
Attaches Rating models and running counts to the model being rated via a generic relation.
"""
count = models.PositiveIntegerField(default=0)
total = models.PositiveIntegerField(default=0)
average = models.DecimalField(max_digits=6, decimal_places=3, default=Decimal(0.0))
content_type = models.ForeignKey(ContentType, null=True, blank=True, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField(null=True, blank=True)
content_object = GenericForeignKey()
objects = RatingManager()
class Meta:
unique_together = ['content_type', 'object_id']
abstract = True
@property
def percentage(self):
return (self.average / app_settings.STAR_RATINGS_RANGE) * 100
def to_dict(self):
return {
'count': self.count,
'total': self.total,
'average': self.average,
'percentage': self.percentage,
}
def __str__(self):
return '{}'.format(self.content_object)
def calculate(self):
"""
Recalculate the totals, and save.
"""
aggregates = self.user_ratings.aggregate(total=Sum('score'), average=Avg('score'), count=Count('score'))
self.count = aggregates.get('count') or 0
self.total = aggregates.get('total') or 0
self.average = aggregates.get('average') or 0.0
self.save()
class Rating(AbstractBaseRating):
class Meta(AbstractBaseRating.Meta):
swappable = swapper.swappable_setting('star_ratings', 'Rating')
class UserRatingManager(models.Manager):
def for_instance_by_user(self, instance, user=None):
ct = ContentType.objects.get_for_model(instance)
user = _clean_user(user)
if user:
return self.filter(rating__content_type=ct, rating__object_id=instance.pk, user=user).first()
else:
return None
def has_rated(self, instance, user=None):
if isinstance(instance, get_star_ratings_rating_model()):
raise TypeError("UserRating manager 'has_rated' expects model to be rated, not UserRating model.")
rating = self.for_instance_by_user(instance, user=user)
return rating is not None
def bulk_create(self, objs, batch_size=None):
objs = super(UserRatingManager, self).bulk_create(objs, batch_size=batch_size)
for rating in set(o.rating for o in objs):
rating.calculate()
return objs
class UserRating(TimeStampedModel):
"""
An individual rating of a user against a model.
"""
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.CASCADE)
ip = models.GenericIPAddressField(blank=True, null=True)
score = models.PositiveSmallIntegerField()
rating = models.ForeignKey(get_star_ratings_rating_model_name(), related_name='user_ratings', on_delete=models.CASCADE)
objects = UserRatingManager()
class Meta:
unique_together = ['user', 'rating']
def __str__(self):
if not app_settings.STAR_RATINGS_ANONYMOUS:
return '{} rating {} for {}'.format(self.user, self.score, self.rating.content_object)
return '{} rating {} for {}'.format(self.ip, self.score, self.rating.content_object)