Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tdt4242-group16
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ole-Christian Bjerkeset
tdt4242-group16
Commits
87dd9b98
Commit
87dd9b98
authored
3 years ago
by
Ole-Christian Bjerkeset
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring comments/views.py
parent
3e70b633
Branches
refactor/comments
Branches containing commit
No related tags found
1 merge request
!7
merging refactor/gallery into master
Pipeline
#172174
passed
3 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/secfit/comments/views.py
+4
-32
4 additions, 32 deletions
backend/secfit/comments/views.py
with
4 additions
and
32 deletions
backend/secfit/comments/views.py
+
4
−
32
View file @
87dd9b98
from
rest_framework
import
generics
,
mixins
from
rest_framework.filters
import
OrderingFilter
from
django.db.models
import
Q
from
rest_framework
import
permissions
from
comments.models
import
Comment
,
Like
from
comments.permissions
import
IsCommentVisibleToUser
...
...
@@ -16,9 +15,11 @@ class CommentList(
HTTP methods: GET, POST
"""
#
queryset = Comment.objects.all()
queryset
=
Comment
.
objects
.
all
()
serializer_class
=
CommentSerializer
permission_classes
=
[
permissions
.
IsAuthenticated
]
permission_classes
=
[
permissions
.
IsAuthenticated
&
IsCommentVisibleToUser
&
(
IsOwner
|
IsReadOnly
)
]
filter_backends
=
[
OrderingFilter
]
ordering_fields
=
[
"
timestamp
"
]
...
...
@@ -31,35 +32,6 @@ class CommentList(
def
perform_create
(
self
,
serializer
):
serializer
.
save
(
owner
=
self
.
request
.
user
)
def
get_queryset
(
self
):
workout_pk
=
self
.
kwargs
.
get
(
"
pk
"
)
qs
=
Comment
.
objects
.
none
()
if
workout_pk
:
qs
=
Comment
.
objects
.
filter
(
workout
=
workout_pk
)
elif
self
.
request
.
user
:
"""
A comment should be visible to the requesting user if any of the following hold:
- The comment is on a public visibility workout
- The comment was written by the user
- The comment is on a coach visibility workout and the user is the workout owner
'
s coach
- The comment is on a workout owned by the user
"""
# The code below is kind of duplicate of the one in ./permissions.py
# We should replace it with a better solution.
# Or maybe not.
qs
=
Comment
.
objects
.
filter
(
Q
(
workout__visibility
=
"
PU
"
)
|
Q
(
owner
=
self
.
request
.
user
)
|
(
Q
(
workout__visibility
=
"
CO
"
)
&
Q
(
workout__owner__coach
=
self
.
request
.
user
)
)
|
Q
(
workout__owner
=
self
.
request
.
user
)
).
distinct
()
return
qs
# Details of comment
class
CommentDetail
(
mixins
.
RetrieveModelMixin
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment