Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tdt4242-base-APU
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
Tor Martin Frøberg Wang
tdt4242-base-APU
Commits
e84e2988
Commit
e84e2988
authored
4 years ago
by
Tobias Ørstad
Browse files
Options
Downloads
Patches
Plain Diff
Fix comments codesmells
parent
877d6e88
No related branches found
Branches containing commit
No related tags found
2 merge requests
!31
Complete exercise 3
,
!24
Backendcommentssmells
Pipeline
#126640
passed
4 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend/secfit/comments/tests.py
+27
-1
27 additions, 1 deletion
backend/secfit/comments/tests.py
backend/secfit/comments/views.py
+17
-30
17 additions, 30 deletions
backend/secfit/comments/views.py
with
44 additions
and
31 deletions
backend/secfit/comments/tests.py
+
27
−
1
View file @
e84e2988
from
django.test
import
TestCase
from
rest_framework.test
import
APIClient
import
json
from
workouts.models
import
Workout
from
users.models
import
User
from
django.utils
import
timezone
# Create your tests here.
class
CommentsTestCase
(
TestCase
):
def
setUp
(
self
):
User
.
objects
.
create
(
id
=
"
1
"
,
username
=
"
Bill
"
,
password
=
"
secret
"
,
email
=
"
hei
"
)
self
.
user_1
=
User
.
objects
.
get
(
id
=
"
1
"
)
self
.
client
=
APIClient
()
self
.
client
.
force_authenticate
(
user
=
self
.
user_1
)
self
.
commentURL
=
"
http://testserver/api/comments/
"
self
.
workout1URL
=
"
http://testserver/api/workouts/1/
"
def
testPUComment
(
self
):
Workout
.
objects
.
create
(
id
=
"
1
"
,
name
=
"
workout
"
,
date
=
timezone
.
now
(),
owner
=
self
.
user_1
,
visibility
=
"
PU
"
)
post
=
self
.
client
.
post
(
self
.
commentURL
,({
"
workout
"
:
self
.
workout1URL
,
"
content
"
:
"
asd
"
}),
format
=
'
json
'
)
self
.
assertEquals
(
post
.
status_code
,
201
)
def
testGetComments
(
self
):
Workout
.
objects
.
create
(
id
=
"
1
"
,
name
=
"
workout
"
,
date
=
timezone
.
now
(),
owner
=
self
.
user_1
,
visibility
=
"
PU
"
)
post
=
self
.
client
.
post
(
self
.
commentURL
,({
"
workout
"
:
self
.
workout1URL
,
"
content
"
:
"
asd
"
}),
format
=
'
json
'
)
self
.
assertEquals
(
post
.
status_code
,
201
)
get
=
self
.
client
.
get
(
"
http://testserver/api/comments/
"
)
datadict
=
dict
(
get
.
data
)
self
.
assertEquals
(
len
(
datadict
[
"
results
"
]),
1
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
backend/secfit/comments/views.py
+
17
−
30
View file @
e84e2988
...
...
@@ -8,11 +8,10 @@ from comments.serializers import CommentSerializer, LikeSerializer
from
django.db.models
import
Q
from
rest_framework.filters
import
OrderingFilter
# Create your views here.
class
CommentList
(
mixins
.
ListModelMixin
,
mixins
.
CreateModelMixin
,
generics
.
GenericAPIView
):
# queryset = Comment.objects.all()
serializer_class
=
CommentSerializer
permission_classes
=
[
permissions
.
IsAuthenticated
]
filter_backends
=
[
OrderingFilter
]
...
...
@@ -28,35 +27,25 @@ class CommentList(
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
()
"""
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
"""
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
,
mixins
.
UpdateModelMixin
,
...
...
@@ -79,7 +68,6 @@ class CommentDetail(
return
self
.
destroy
(
request
,
*
args
,
**
kwargs
)
# List of likes
class
LikeList
(
mixins
.
ListModelMixin
,
mixins
.
CreateModelMixin
,
generics
.
GenericAPIView
):
serializer_class
=
LikeSerializer
permission_classes
=
[
permissions
.
IsAuthenticated
]
...
...
@@ -97,7 +85,6 @@ class LikeList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericA
return
Like
.
objects
.
filter
(
owner
=
self
.
request
.
user
)
# Details of like
class
LikeDetail
(
mixins
.
RetrieveModelMixin
,
mixins
.
UpdateModelMixin
,
...
...
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