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
95060813
Commit
95060813
authored
Apr 22, 2017
by
luxcem
Browse files
up and down votes
parent
7f4d4bec
Changes
3
Hide whitespace changes
Inline
Side-by-side
apps/core/test_app.py
0 → 100644
View file @
95060813
from
core.apps
import
CoreConfig
def
test_init
():
assert
CoreConfig
.
name
==
'core'
apps/rp/models/__init__.py
0 → 100644
View file @
95060813
apps/rp/models/votes.py
0 → 100644
View file @
95060813
from
django.db
import
models
from
django.db.models
import
Sum
,
F
from
django.contrib.contenttypes.fields
import
GenericForeignKey
from
django.contrib.contenttypes.fields
import
GenericRelation
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.auth.models
import
User
class
UnDVotes
(
models
.
Model
):
"""
Up and down vote model
"""
user
=
models
.
ForeignKey
(
User
,
on_delete
=
models
.
CASCADE
,
)
#: Upvote, True for upvote, false for downvote
score
=
models
.
IntegerField
(
default
=
True
)
# Django generic relation
content_type
=
models
.
ForeignKey
(
ContentType
,
on_delete
=
models
.
CASCADE
)
object_id
=
models
.
PositiveIntegerField
()
#: The Voted object
content_object
=
GenericForeignKey
(
"content_type"
,
"object_id"
)
def
__str__
(
self
):
return
"{}:{}:{}"
.
format
(
self
.
user
,
self
.
content_object
,
self
.
score
)
class
UnDVotedMixin
(
models
.
Model
):
"""
A mixin to attach to a model that has up and down votes
"""
#: Votes
und_votes
=
GenericRelation
(
UnDVotes
)
#: Score of the model
und_score
=
models
.
IntegerField
(
default
=
0
)
def
upvote
(
self
,
user
):
diff_score
=
0
try
:
# Already voted content
vote
=
self
.
und_votes
.
get
(
user
=
user
)
if
vote
.
score
==
1
:
# Cancel previous upvote
vote
.
delete
()
diff_score
=
-
1
else
:
# Previously downvoted
vote
.
score
=
1
vote
.
save
()
diff_score
=
2
except
:
vote
=
UnDVotes
(
content_object
=
self
,
user
=
user
,
score
=
1
)
vote
.
save
()
diff_score
=
1
self
.
__class__
.
objects
.
filter
(
id
=
self
.
id
).
update
(
und_score
=
F
(
"und_score"
)
+
diff_score
)
def
downvote
(
self
,
user
):
diff_score
=
0
try
:
# Already voted content
vote
=
self
.
und_votes
.
get
(
user
=
user
)
if
vote
.
score
==
-
1
:
# Cancel previous downvote
vote
.
delete
()
diff_score
=
1
else
:
# Previously upvoted
vote
.
score
=
-
1
vote
.
save
()
diff_score
=
-
2
except
:
vote
=
UnDVotes
(
content_object
=
self
,
user
=
user
,
score
=-
1
)
vote
.
save
()
diff_score
=
-
1
# 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
# Update self score, use update and filter to avoid triggering signals
self
.
__class__
.
objects
.
filter
(
id
=
self
.
id
).
update
(
und_score
=
score
)
class
Meta
:
abstract
=
True
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