Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Backend
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor 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
swp-unisport
team-warumkeinrust
Backend
Commits
b9c77897
Commit
b9c77897
authored
3 years ago
by
borzechof99
Browse files
Options
Downloads
Patches
Plain Diff
Implement first APIView and Serializer for Sportlist
parent
80033041
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
unisportomat/quiz/serializers.py
+18
-0
18 additions, 0 deletions
unisportomat/quiz/serializers.py
unisportomat/quiz/views.py
+64
-0
64 additions, 0 deletions
unisportomat/quiz/views.py
unisportomat/unisportomat/urls.py
+2
-0
2 additions, 0 deletions
unisportomat/unisportomat/urls.py
with
84 additions
and
0 deletions
unisportomat/quiz/serializers.py
+
18
−
0
View file @
b9c77897
...
@@ -34,3 +34,21 @@ class CriterionListSerializer(serializers.ModelSerializer):
...
@@ -34,3 +34,21 @@ class CriterionListSerializer(serializers.ModelSerializer):
class
Meta
:
class
Meta
:
model
=
Criterion
model
=
Criterion
fields
=
(
"
id
"
,
"
name
"
)
fields
=
(
"
id
"
,
"
name
"
)
class
SmallSportListSerializer
(
serializers
.
BaseSerializer
):
def
to_representation
(
self
,
sport_instances
):
serialized_data
=
[]
for
sport
,
boolean
in
sport_instances
:
serialized_data
.
append
(
{
"
pk
"
:
sport
.
pk
,
"
name
"
:
sport
.
name
,
"
url
"
:
sport
.
url
,
"
is_filled
"
:
boolean
,
}
)
return
serialized_data
This diff is collapsed.
Click to expand it.
unisportomat/quiz/views.py
+
64
−
0
View file @
b9c77897
...
@@ -4,7 +4,10 @@ Defines the views for the API
...
@@ -4,7 +4,10 @@ Defines the views for the API
# from django.shortcuts import render
# from django.shortcuts import render
from
rest_framework
import
viewsets
from
rest_framework
import
viewsets
from
rest_framework.views
import
APIView
from
rest_framework.response
import
Response
from
.serializers
import
(
from
.serializers
import
(
SmallSportListSerializer
,
SportListSerializer
,
SportListSerializer
,
CriterionListSerializer
,
CriterionListSerializer
,
QuestionListSerializer
,
QuestionListSerializer
,
...
@@ -39,3 +42,64 @@ class QuestionListView(viewsets.ModelViewSet): # pylint: disable=too-many-ances
...
@@ -39,3 +42,64 @@ class QuestionListView(viewsets.ModelViewSet): # pylint: disable=too-many-ances
serializer_class
=
QuestionListSerializer
serializer_class
=
QuestionListSerializer
queryset
=
Question
.
objects
.
all
()
queryset
=
Question
.
objects
.
all
()
# Dev Notes:
# - If we want to include a View in the Router in urls.py, the View needs to be a Viewset
# - Those are mostly meant for Lists of Objects, so instead of get() and post(), list() and create() are used respectively
# https://stackoverflow.com/questions/30389248/how-can-i-register-a-single-view-not-a-viewset-on-my-router
class
SmallSportListView
(
viewsets
.
ViewSet
):
"""
View for the List of Sports on the Sport Homepage
TODO: More Documentation
"""
def
list
(
self
,
request
):
return
Response
({
"
test
"
:
0
})
# Dev Notes:
# - This is a singular APIView which isn't meant to expose complete lists
# - It cannot be written into the Router, as it isn't a Viewset, so it is written directly into the urls.py's urlspatterns like this:
# path(r"api/admin/single-small-sport-list", views.SmallSportListAPIView.as_view())
# - The API isn't in the list in /api/admin because of that, but needs to be called manually here:
# http://localhost:8000/api/admin/single-small-sport-list
class
SmallSportListAPIView
(
APIView
):
"""
View for the List of Sports on the Sport Homepage
TODO: More Documentation
"""
def
get
(
self
,
request
):
sports
=
Sport
.
objects
.
all
()
criteria
=
Criterion
.
objects
.
all
()
is_filled_tuples
=
[]
for
sport
in
sports
.
iterator
():
filled_criteria_pks
=
[]
# Get pks of Criteria which are connected to the sport
for
criterion
in
sport
.
criteria_ratings
.
iterator
():
filled_criteria_pks
.
append
(
criterion
.
pk
)
is_filled
=
True
# Check every existing criterion whether it is in the generated list
for
criterion
in
criteria
.
all
():
if
criterion
.
pk
not
in
filled_criteria_pks
:
# A criterion isn't filled out, so stop and set to false
is_filled
=
False
break
is_filled_tuples
.
append
((
sport
,
is_filled
))
serializer
=
SmallSportListSerializer
(
is_filled_tuples
)
return
Response
(
serializer
.
data
)
This diff is collapsed.
Click to expand it.
unisportomat/unisportomat/urls.py
+
2
−
0
View file @
b9c77897
...
@@ -22,8 +22,10 @@ router = routers.DefaultRouter()
...
@@ -22,8 +22,10 @@ router = routers.DefaultRouter()
router
.
register
(
r
"
sport-list
"
,
views
.
SportListView
,
"
sport-list
"
)
router
.
register
(
r
"
sport-list
"
,
views
.
SportListView
,
"
sport-list
"
)
router
.
register
(
r
"
criterion-list
"
,
views
.
CriterionListView
,
"
criterion-list
"
)
router
.
register
(
r
"
criterion-list
"
,
views
.
CriterionListView
,
"
criterion-list
"
)
router
.
register
(
r
"
question-list
"
,
views
.
QuestionListView
,
"
question-list
"
)
router
.
register
(
r
"
question-list
"
,
views
.
QuestionListView
,
"
question-list
"
)
router
.
register
(
r
"
small-sport-list
"
,
views
.
SmallSportListView
,
"
small-sport-list
"
)
urlpatterns
=
[
urlpatterns
=
[
path
(
"
admin/
"
,
admin
.
site
.
urls
),
path
(
"
admin/
"
,
admin
.
site
.
urls
),
path
(
"
api/admin/
"
,
include
(
router
.
urls
)),
path
(
"
api/admin/
"
,
include
(
router
.
urls
)),
path
(
r
"
api/admin/single-small-sport-list
"
,
views
.
SmallSportListAPIView
.
as_view
()),
]
]
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