Commit e5551869 authored by cynddl's avatar cynddl

Add scores for up and down vote

parent 9f652de1
......@@ -39,6 +39,8 @@ class UnDVotedMixin(models.Model):
und_votes = GenericRelation(UnDVotes)
#: Score of the model
und_score = models.IntegerField(default=0)
und_score_up = models.IntegerField(default=0)
und_score_down = models.IntegerField(default=0)
class Meta:
abstract = True
......@@ -63,6 +65,7 @@ class UnDVotedMixin(models.Model):
diff_score = 1
self.und_score += diff_score
self.und_score_up += diff_score
# Update self score, use update and filter to avoid triggering signals
self.__class__.objects.filter(id=self.id).update(
und_score=F("und_score") + diff_score)
......@@ -87,13 +90,19 @@ class UnDVotedMixin(models.Model):
diff_score = -1
self.und_score += diff_score
self.und_score_down += diff_score
# Update self score, use update and filter to avoid triggering signals
self.__class__.objects.filter(id=self.id).update(
und_score=F("und_score") + diff_score)
def update_und_score(self):
"""Reset score to the correct count (should not be necessary)"""
score = self.und_votes.aggregate(Sum("score"))["score__sum"] or 0
score_up = self.und_votes.filter(score=1).count()
score_down = self.und_votes.filter(score=-1).count()
score = score_up + score_down
self.und_score_up = score_up
self.und_score_down = score_down
self.und_score = score
# Update self score, use update and filter to avoid triggering signals
self.__class__.objects.filter(id=self.id).update(und_score=score)
......@@ -61,8 +61,8 @@
<td class="title-cell"><a target="_blank" href="{{article.url}}"><strong>{{article.title}}</strong></a></td>
<td>
<ul class="votes-list">
<li><a href="">+3 <i class="fa fa-thumbs-up" aria-hidden="true" style="color:green"></i></a></li>
<li><a href="">-2 <i class="fa fa-thumbs-down" aria-hidden="true" style="color:red"></i></a></li>
<li><a href="">+{{article.und_score_up}} <i class="fa fa-thumbs-up" aria-hidden="true" style="color:green"></i></a></li>
<li><a href="">-{{article.und_score_down}} <i class="fa fa-thumbs-down" aria-hidden="true" style="color:red"></i></a></li>
</ul>
</td>
<td>{{article.und_votes.first.username}}</td>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment