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
737df68c
Commit
737df68c
authored
3 years ago
by
fu2662cw
Browse files
Options
Downloads
Patches
Plain Diff
Add docstrings and fix other linter warnings
parent
c98b1826
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
unisportomat/quiz/models.py
+23
-2
23 additions, 2 deletions
unisportomat/quiz/models.py
unisportomat/quiz/tests.py
+26
-14
26 additions, 14 deletions
unisportomat/quiz/tests.py
with
49 additions
and
16 deletions
unisportomat/quiz/models.py
+
23
−
2
View file @
737df68c
"""
Model definitions for the quiz
"""
from
django.core.validators
import
MaxValueValidator
,
MinValueValidator
from
django.db
import
models
class
CriterionRating
(
models
.
Model
):
"""
This is the relation between Sport and Criterion.
You can use it to add a rating for a specific criterion to a sport.
To see it
'
s usage check Sport.rate() and Sport.get_rating()
"""
rating
=
models
.
IntegerField
(
validators
=
[
MaxValueValidator
(
10
),
MinValueValidator
(
0
)]
# validation on user level (not db level)
validators
=
[
MaxValueValidator
(
10
),
MinValueValidator
(
0
)]
)
criterion
=
models
.
ForeignKey
(
"
Criterion
"
,
on_delete
=
models
.
CASCADE
)
sport
=
models
.
ForeignKey
(
"
Sport
"
,
on_delete
=
models
.
CASCADE
)
class
Sport
(
models
.
Model
):
"""
Defines a Sport with name, url that leads to the booking page.
A sport includes ratings for all criterions.
(e.g. How much it corresponds to the criterion
"
Martial Arts
"
)
"""
name
=
models
.
TextField
()
url
=
models
.
URLField
()
criteria_ratings
=
models
.
ManyToManyField
(
"
Criterion
"
,
through
=
"
CriterionRating
"
)
...
...
@@ -19,13 +33,20 @@ class Sport(models.Model):
return
self
.
name
def
rate
(
self
,
criterion
,
rating
):
rating
=
CriterionRating
(
sport
=
self
,
criterion
=
criterion
,
rating
=
10
)
"""
Defines how much (rating) the sport meets the given criterion
"""
rating
=
CriterionRating
(
sport
=
self
,
criterion
=
criterion
,
rating
=
rating
)
rating
.
save
()
def
get_rating
(
self
,
criterion
):
"""
Returns how much the sport meets the given criterion
"""
criterion_rating
=
CriterionRating
.
objects
.
get
(
sport
=
self
,
criterion
=
criterion
)
return
criterion_rating
.
rating
class
Criterion
(
models
.
Model
):
"""
Defines a Sport property that is used a a criterion for our quiz.
(e.g. Individual or Team sport)
"""
name
=
models
.
TextField
()
This diff is collapsed.
Click to expand it.
unisportomat/quiz/tests.py
+
26
−
14
View file @
737df68c
from
django.test
import
TestCase
"""
This module tests all our quiz models
"""
from
django.test
import
TestCase
from
.models
import
Sport
,
Criterion
class
SportModelTest
(
TestCase
):
"""
Tests the sport model
"""
def
setUp
(
self
):
self
.
name
=
"
HIIT
"
self
.
url
=
"
https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_-_High_Intensity_Interval_Training___HOME.html
"
self
.
url
=
"
https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_
"
\
"
-_High_Intensity_Interval_Training___HOME.html
"
self
.
test
S
port
=
Sport
(
self
.
test
_s
port
=
Sport
(
name
=
self
.
name
,
url
=
self
.
url
,
)
self
.
test
S
port
.
save
()
self
.
test
_s
port
.
save
()
def
test_sport_can_be_created
(
self
):
self
.
assertEquals
(
self
.
testSport
.
name
,
self
.
name
)
self
.
assertEquals
(
self
.
testSport
.
url
,
self
.
url
)
"""
New sport is written to the database
"""
test_sport
=
Sport
.
objects
.
first
()
self
.
assertEqual
(
test_sport
.
name
,
self
.
name
)
self
.
assertEqual
(
test_sport
.
url
,
self
.
url
)
class
CriterionRatingTest
(
TestCase
):
"""
Tests the Relation between Sport and Criterion
"""
def
setUp
(
self
):
self
.
name
=
"
HIIT
"
self
.
url
=
"
https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_-_High_Intensity_Interval_Training___HOME.html
"
self
.
url
=
"
https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_
"
\
"
-_High_Intensity_Interval_Training___HOME.html
"
self
.
test
S
port
=
Sport
(
self
.
test
_s
port
=
Sport
(
name
=
self
.
name
,
url
=
self
.
url
,
)
self
.
test
S
port
.
save
()
self
.
test
_s
port
.
save
()
self
.
criterion
=
Criterion
(
name
=
"
Einzelsport
"
)
self
.
criterion
.
save
()
def
test_can_rate_criterion_for_sport
(
self
):
self
.
testSport
.
rate
(
self
.
criterion
,
10
)
self
.
assertEquals
(
self
.
testSport
.
criteria_ratings
.
first
(),
self
.
criterion
)
self
.
assertEquals
(
self
.
testSport
.
get_rating
(
self
.
criterion
),
10
)
"""
A rating for a specific criterion can be added to a sport
"""
self
.
test_sport
.
rate
(
self
.
criterion
,
10
)
self
.
assertEqual
(
self
.
test_sport
.
criteria_ratings
.
first
(),
self
.
criterion
)
self
.
assertEqual
(
self
.
test_sport
.
get_rating
(
self
.
criterion
),
10
)
class
CriterionModelTest
(
TestCase
):
"""
Tests the Criterion model
"""
def
test_criterion_can_be_created
(
self
):
"""
New criterion is saved to the db
"""
name
=
"
Einzelsport
"
test_criterion
=
Criterion
(
name
=
name
)
self
.
assertEquals
(
test_criterion
.
name
,
name
)
Criterion
(
name
=
name
).
save
()
test_criterion
=
Criterion
.
objects
.
first
()
self
.
assertEqual
(
test_criterion
.
name
,
name
)
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