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
b58cd610
Commit
b58cd610
authored
3 years ago
by
borzechof99
Browse files
Options
Downloads
Patches
Plain Diff
Implemented TODOs: is_filled is now function of Sport. Also some Serializer Error Catching
parent
83aebaec
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
unisportomat/quiz/models.py
+12
-2
12 additions, 2 deletions
unisportomat/quiz/models.py
unisportomat/quiz/serializers.py
+30
-3
30 additions, 3 deletions
unisportomat/quiz/serializers.py
unisportomat/quiz/views.py
+7
-24
7 additions, 24 deletions
unisportomat/quiz/views.py
with
49 additions
and
29 deletions
unisportomat/quiz/models.py
+
12
−
2
View file @
b58cd610
...
@@ -10,8 +10,7 @@ def validate_rating(value):
...
@@ -10,8 +10,7 @@ def validate_rating(value):
TODO: Validations are only run when using Model Forms.
TODO: Validations are only run when using Model Forms.
"""
"""
print
(
"
Validating
"
)
if
not
(
value
<=
10
and
value
>=
1
or
value
==
-
1
):
if
not
(
value
<=
10
or
value
>=
1
or
value
==
-
1
):
raise
ValidationError
(
u
"
%s is not a valid rating!
"
%
value
)
raise
ValidationError
(
u
"
%s is not a valid rating!
"
%
value
)
...
@@ -79,6 +78,17 @@ class Sport(models.Model):
...
@@ -79,6 +78,17 @@ class Sport(models.Model):
criterion_rating
=
CriterionRating
.
objects
.
get
(
sport
=
self
,
criterion
=
criterion
)
criterion_rating
=
CriterionRating
.
objects
.
get
(
sport
=
self
,
criterion
=
criterion
)
return
criterion_rating
.
rating
return
criterion_rating
.
rating
def
is_filled
(
self
):
"""
Returns a Boolean whether all Criterions are given a valid rating (unequal to -1)
"""
for
crit
in
self
.
criteria_ratings
.
iterator
():
if
self
.
get_rating
(
crit
)
==
-
1
:
return
False
return
True
class
CriterionManager
(
models
.
Manager
):
class
CriterionManager
(
models
.
Manager
):
"""
"""
...
...
This diff is collapsed.
Click to expand it.
unisportomat/quiz/serializers.py
+
30
−
3
View file @
b58cd610
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
Serializers creating JSONs for every Model from .models
Serializers creating JSONs for every Model from .models
"""
"""
from
rest_framework
import
serializers
from
rest_framework
import
serializers
from
.models
import
Sport
,
Criterion
,
Question
,
CriterionRating
from
.models
import
Sport
,
Criterion
,
Question
,
CriterionRating
,
validate_rating
class
SportListSerializer
(
serializers
.
ModelSerializer
):
class
SportListSerializer
(
serializers
.
ModelSerializer
):
...
@@ -79,7 +79,34 @@ class SingleSportSerializer(serializers.BaseSerializer):
...
@@ -79,7 +79,34 @@ class SingleSportSerializer(serializers.BaseSerializer):
return
serialized_data
return
serialized_data
def
to_internal_value
(
self
,
request
):
def
to_internal_value
(
self
,
request
):
"""
The Data in the Request is taken and written to another Dictionary.
During this process, the Data is Validated on:
- whether Name and URL for Sport exist
- whether the Rating Value and Criterion ID are valid
"""
# Probably do some Testing and Data Verification here?
data
=
{
"
valid_request
"
:
True
}
return
request
.
data
for
field_value
in
[
"
name
"
,
"
url
"
]:
try
:
data
[
field_value
]
=
request
.
data
[
field_value
]
except
:
data
[
"
valid_request
"
]
=
False
return
data
data
[
"
criteria
"
]
=
[]
for
criterion
in
request
.
data
[
"
criteria
"
]:
value
=
criterion
[
"
value
"
]
try
:
validate_rating
(
value
)
crit
=
Criterion
.
objects
.
get
(
pk
=
criterion
[
"
id
"
])
except
:
data
[
"
valid_request
"
]
=
False
return
data
data
[
"
criteria
"
].
append
((
crit
,
value
))
return
data
This diff is collapsed.
Click to expand it.
unisportomat/quiz/views.py
+
7
−
24
View file @
b58cd610
...
@@ -7,7 +7,7 @@ from rest_framework import viewsets
...
@@ -7,7 +7,7 @@ from rest_framework import viewsets
from
rest_framework.views
import
APIView
from
rest_framework.views
import
APIView
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
django.shortcuts
import
get_object_or_404
from
django.shortcuts
import
get_object_or_404
from
django.http
import
JsonResponse
from
django.http
import
JsonResponse
,
HttpResponse
from
.pagination
import
PageNumberWithPageSizePagination
from
.pagination
import
PageNumberWithPageSizePagination
from
.serializers
import
(
from
.serializers
import
(
...
@@ -91,24 +91,7 @@ class SmallSportListView(viewsets.ViewSet):
...
@@ -91,24 +91,7 @@ class SmallSportListView(viewsets.ViewSet):
for
sport
in
sports
:
for
sport
in
sports
:
filled_criteria_pks
=
[]
is_filled_tuples
.
append
((
sport
,
sport
.
is_filled
()))
# TODO: Change the way that unfilled Criteria are detected (look for value -1)
# TODO: is_filled as function of sport
# 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
)
serializer
=
SmallSportListSerializer
(
is_filled_tuples
)
...
@@ -119,7 +102,9 @@ class SmallSportListView(viewsets.ViewSet):
...
@@ -119,7 +102,9 @@ class SmallSportListView(viewsets.ViewSet):
data_dict
=
SingleSportSerializer
().
to_internal_value
(
request
)
data_dict
=
SingleSportSerializer
().
to_internal_value
(
request
)
print
(
data_dict
)
# A Try at Error Catching
if
not
data_dict
[
"
valid_request
"
]:
return
HttpResponse
(
status
=
400
)
new_sport
=
Sport
.
objects
.
create_sport
()
new_sport
=
Sport
.
objects
.
create_sport
()
...
@@ -129,10 +114,8 @@ class SmallSportListView(viewsets.ViewSet):
...
@@ -129,10 +114,8 @@ class SmallSportListView(viewsets.ViewSet):
# Before writing other Database Entries for Rating, the Sport needs to be saved once
# Before writing other Database Entries for Rating, the Sport needs to be saved once
new_sport
.
save
()
new_sport
.
save
()
for
criterion
in
data_dict
[
"
criteria
"
]:
for
criterion
,
value
in
data_dict
[
"
criteria
"
]:
new_sport
.
rate
(
new_sport
.
rate
(
criterion
,
value
)
Criterion
.
objects
.
get
(
pk
=
criterion
[
"
id
"
]),
criterion
[
"
value
"
]
)
new_sport
.
save
()
new_sport
.
save
()
...
...
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