Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
rp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
14
Issues
14
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
La Quadrature du Net
rpteam
rp
Commits
95060813
Commit
95060813
authored
Apr 22, 2017
by
luxcem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
up and down votes
parent
7f4d4bec
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
0 deletions
+96
-0
apps/core/test_app.py
apps/core/test_app.py
+5
-0
apps/rp/models/__init__.py
apps/rp/models/__init__.py
+0
-0
apps/rp/models/votes.py
apps/rp/models/votes.py
+91
-0
No files found.
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
Markdown
is supported
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