Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
rpteam
Revue de Press
Commits
e5551869
Commit
e5551869
authored
Apr 23, 2017
by
cynddl
Browse files
Add scores for up and down vote
parent
9f652de1
Changes
2
Show whitespace changes
Inline
Side-by-side
apps/rp/models/vote.py
View file @
e5551869
...
...
@@ -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
)
apps/rp/templates/rp/article_list.html
View file @
e5551869
...
...
@@ -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>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment