1 from datetime
import datetime
3 from couchdbkit
.ext
.django
.schema
import *
6 class Rating(DocumentSchema
):
7 rating
= IntegerProperty()
8 timestamp
= DateTimeProperty(default
=datetime
.utcnow
)
9 user
= StringProperty()
12 ALLOWED_RATINGS
= (1, -1)
14 class RatingMixin(DocumentSchema
):
15 ratings
= SchemaListProperty(Rating
)
17 def rate(self
, rating_val
, user_id
):
20 raise ValueError('User must not be None')
22 if rating_val
not in ALLOWED_RATINGS
:
23 raise ValueError('Rating must be in %s' % (ALLOWED_RATINGS
, ))
25 rating
= Rating(rating
=rating_val
, user
=user_id
)
26 self
.ratings
= filter(lambda r
: r
.user
is not None, self
.ratings
)
27 self
.ratings
= filter(lambda r
: r
.user
!= user_id
, self
.ratings
)
28 self
.ratings
.append(rating
)
32 return sum(r
.rating
for r
in self
.ratings
)