6b5850aa588ec213df8feba5ce923cc978b430d2
[zzz-floof.git] / floof / model / ratings.py
1 from elixir import *
2 from art import Art
3 from floof.lib.dbhelpers import find_or_create, update_or_create
4
5 class Rating(Entity):
6 art = ManyToOne('Art', ondelete='cascade')
7 rater = ManyToOne('User', ondelete='cascade')
8 score = Field(Integer)
9
10 options = {-1:"sucks", 0:"undecided", 1:"good", 2:"great"}
11 default = 0
12
13 Rating.reverse_options = dict (zip(Rating.options.values(), Rating.options.keys()))
14
15
16 class RatingMixin(object):
17 def rate(self, score, user):
18 return update_or_create(Rating, {"rater":user, "art":self}, {"score":score})
19
20 def user_score(self, user):
21 rating = Rating.get_by(rater=user, art=self)
22 if rating:
23 return rating.score
24 return Rating.default
25
26 Art.__bases__ += (RatingMixin,)