Permalink
Browse files

Refactored rating system to allow for ranking statistic

  • Loading branch information...
1 parent d4512a1 commit b79b65232a78d1edc7a36c10b103a46b3b8b40c2 @graingert committed Apr 12, 2012
View
@@ -8,7 +8,7 @@ class Meta:
model = Review
exclude = ("author",)
widgets = {
- 'rating': RangeInput(min_value=-3, max_value=3, step=2), #uses 4 states preventing any "on the fence choices"
+ 'rating': RangeInput(min_value=0, max_value=3, step=1), #uses 4 states preventing any "on the fence choices"
'reviewed_uri' : HiddenInput(),
'mentioned' : HiddenInput(),
}
View
@@ -15,6 +15,30 @@
import bleach
# Create your models here.
+
+DEFAULT_REVIEWS_SCALE = 4;
+DEFAULT_REVIEWS_SCORE_MAP = {
+ 0 : {
+ "label" :"Terrible",
+ "color": "CC0000"
+ },
+ 1 : {
+ "label" :"Bad",
+ "color": "CC6600"
+ },
+ 2 : {
+ "label" :"Good",
+ "color": "99CC00"
+ },
+ 3 : {
+ "label" :"Great",
+ "color": "00CC00"
+ }
+ }
+
+SCALE = getattr(settings,"REVIEWS_SCALE", DEFAULT_REVIEWS_SCALE)
+SCORE_MAP = getattr(settings,"REVIEWS_SCORE_MAP", DEFAULT_REVIEWS_SCORE_MAP)
+
class StepValidator():
def __init__(self, min_value, step):
self.min_value = min_value
@@ -58,7 +82,6 @@ def graph(self):
def get_label(self):
return unicode(self.graph.label(URIRef(self.uri), urlparse.urlparse(self.uri).path))
-
def save(self):
self.label = self.get_label()[:30]
super(Thing, self).save()
@@ -83,29 +106,9 @@ def chart(self):
histogram = self.histogram()
- style_map = {
- -3:
- {
- "label" :"Terrible",
- "color": "CC0000"
- },
- -1 : {
- "label" :"Bad",
- "color": "CC6600"
- },
- 1 : {
- "label" :"Good",
- "color": "99CC00"
- },
- 3 : {
- "label" :"Great",
- "color": "00CC00"
- },
- }
-
for point in histogram:
- style = style_map[point["rating"]]
+ style = SCORE_MAP[point["rating"]]
labels.append(style["label"])
colors.append(style["color"])
@@ -121,7 +124,7 @@ class Review(m.Model):
date_created = CreationDateTimeField()
date_modified = ModificationDateTimeField()
#title = m.CharField(max_length=128)
- rating = RangeField(min_value=-3, max_value = 3, step = 2)
+ rating = RangeField(min_value=0, max_value = SCALE-1, step = 1)
text = m.TextField(verbose_name="Detailed Review")
author = m.ForeignKey(User, editable=False)
reviewed_uri = m.ForeignKey(Thing)
@@ -131,6 +134,9 @@ def get_absolute_url(self):
thing_url = self.reviewed_uri.get_absolute_url()
return thing_url + '#review-' + str(self.id)
+ def rating_label(self):
+ return SCORE_MAP[self.rating]["label"]
+
def __unicode__(self):
return self.text[:140]
@@ -13,23 +13,3 @@
#mentionable-list button.unmention{
display: none;
}
-
-#id_rating[value="-3"]~label[for="id_rating"]:after {
- color: red;
- content: "terrible";
-}
-
-#id_rating[value="-1"]~label[for="id_rating"]:after {
- color: red;
- content: "bad";
-}
-
-#id_rating[value="1"]~label[for="id_rating"]:after {
- color: red;
- content: "good";
-}
-
-#id_rating[value="3"]~label[for="id_rating"]:after {
- color: red;
- content: "great";
-}
@@ -16,26 +16,6 @@
border-radius:3px;
}
-.rating[data-value="-3"]:after {
- content: "Terrible";
- color: red;
-}
-
-.rating[data-value="-1"]:after {
- content: "Bad";
- color: red;
-}
-
-.rating[data-value="1"]:after {
- content: "Good";
- color: green;
-}
-
-.rating[data-value="3"]:after {
- content: "Great";
- color: green;
-}
-
.aggregate dd {
font-size: 3em;
line-height: 1em;
@@ -6,7 +6,7 @@
<article class="review">
<header>
<a href="{{object.get_absolute_url}}">
- <span class="rating">{{ object.rating}}</span>
+ <span class="rating" data-value="{{object.rating}}">{{object.rating_label}}</span>
<address class="author">
<img class="avatar" src="{{ object.author.get_profile.gravatar_url }}" />
<span class="nickname">{{ object.author.get_profile.nickname }}</span>
@@ -15,7 +15,7 @@
{% else %}
this
{% endif %}
- a rating of <span class="rating" data-value="{{review.rating}}">{{review.stars}}</span>
+ a rating of <span class="rating" data-value="{{review.rating}}">{{review.rating_label}}</span>
<address class="author">
<img class="avatar" src="{{ review.author.get_profile.gravatar_url }}" />
</address>

0 comments on commit b79b652

Please sign in to comment.