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
83aebaec
Commit
83aebaec
authored
3 years ago
by
borzechof99
Browse files
Options
Downloads
Patches
Plain Diff
Add Managers to automatically create CriterionRatings on Creation of Sport and Criterion
parent
eed968ed
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/management/commands/seed_db.py
+2
-2
2 additions, 2 deletions
unisportomat/quiz/management/commands/seed_db.py
unisportomat/quiz/models.py
+57
-3
57 additions, 3 deletions
unisportomat/quiz/models.py
unisportomat/quiz/views.py
+1
-1
1 addition, 1 deletion
unisportomat/quiz/views.py
with
60 additions
and
6 deletions
unisportomat/quiz/management/commands/seed_db.py
+
2
−
2
View file @
83aebaec
...
...
@@ -74,7 +74,7 @@ class Command(BaseCommand):
"
Bouldern
"
,
]
for
name
in
sports_names
:
Sport
(
name
=
name
).
save
()
Sport
.
objects
.
create_sport
(
name
=
name
).
save
()
# Create criteria
criteria_names
=
[
...
...
@@ -85,7 +85,7 @@ class Command(BaseCommand):
"
Kampfsport
"
,
]
for
name
in
criteria_names
:
Criterion
(
name
=
name
).
save
()
Criterion
.
objects
.
create_criterion
(
name
=
name
).
save
()
# Create ratings for all sports and criterions
for
sport
in
Sport
.
objects
.
all
():
...
...
This diff is collapsed.
Click to expand it.
unisportomat/quiz/models.py
+
57
−
3
View file @
83aebaec
"""
Model definitions for the quiz
"""
from
django.core.validators
import
MaxValueValidator
,
MinValueValidator
from
django.core.exceptions
import
ValidationError
from
django.db
import
models
def
validate_rating
(
value
):
"""
TODO: Validations are only run when using Model Forms.
"""
print
(
"
Validating
"
)
if
not
(
value
<=
10
or
value
>=
1
or
value
==
-
1
):
raise
ValidationError
(
u
"
%s is not a valid rating!
"
%
value
)
class
CriterionRating
(
models
.
Model
):
"""
This is the relation between Sport and Criterion.
...
...
@@ -11,9 +22,7 @@ class CriterionRating(models.Model):
To see it
'
s usage check Sport.rate() and Sport.get_rating()
"""
rating
=
models
.
IntegerField
(
validators
=
[
MaxValueValidator
(
10
),
MinValueValidator
(
1
)]
)
rating
=
models
.
IntegerField
(
validators
=
[
validate_rating
])
criterion
=
models
.
ForeignKey
(
"
Criterion
"
,
on_delete
=
models
.
CASCADE
)
sport
=
models
.
ForeignKey
(
"
Sport
"
,
on_delete
=
models
.
CASCADE
)
...
...
@@ -21,6 +30,25 @@ class CriterionRating(models.Model):
return
str
(
self
.
sport
)
+
"
-
"
+
str
(
self
.
criterion
)
+
"
:
"
+
str
(
self
.
rating
)
class
SportManager
(
models
.
Manager
):
"""
Manages Creation of Sport Objects
Since every Criterion Connection needs to be present in the DB at all times,
the connections are made at creation of the Sport object. For this, Sport objects need to be
created through this Manager Class.
Docs: https://docs.djangoproject.com/en/3.2/ref/models/instances/#creating-objects
"""
def
create_sport
(
self
,
**
kwargs
):
sport
=
self
.
create
(
**
kwargs
)
for
crit
in
Criterion
.
objects
.
iterator
():
sport
.
rate
(
crit
,
-
1
)
return
sport
class
Sport
(
models
.
Model
):
"""
Defines a Sport with name, url that leads to the booking page.
...
...
@@ -32,6 +60,8 @@ class Sport(models.Model):
url
=
models
.
URLField
()
criteria_ratings
=
models
.
ManyToManyField
(
"
Criterion
"
,
through
=
"
CriterionRating
"
)
objects
=
SportManager
()
def
__str__
(
self
):
return
self
.
name
...
...
@@ -50,6 +80,28 @@ class Sport(models.Model):
return
criterion_rating
.
rating
class
CriterionManager
(
models
.
Manager
):
"""
Manages Creation of Criterion Objects
Since every Sport Object needs to be rated in every criterion,
when a new Criterion is created, every Sport object needs to be rated for that Criterion.
As a default Value, -1 is entered so that it can be recognized that no true value is given.
Docs: https://docs.djangoproject.com/en/3.2/ref/models/instances/#creating-objects
"""
def
create_criterion
(
self
,
**
kwargs
):
crit
=
Criterion
(
**
kwargs
)
# Criterion needs to be saved before it can be connected to a sport
crit
.
save
()
for
sport
in
Sport
.
objects
.
iterator
():
sport
.
rate
(
crit
,
-
1
)
return
crit
class
Criterion
(
models
.
Model
):
"""
Defines a Sport property that is used a a criterion for our quiz.
...
...
@@ -58,6 +110,8 @@ class Criterion(models.Model):
name
=
models
.
TextField
()
objects
=
CriterionManager
()
def
__str__
(
self
):
return
self
.
name
...
...
This diff is collapsed.
Click to expand it.
unisportomat/quiz/views.py
+
1
−
1
View file @
83aebaec
...
...
@@ -121,7 +121,7 @@ class SmallSportListView(viewsets.ViewSet):
print
(
data_dict
)
new_sport
=
Sport
()
new_sport
=
Sport
.
objects
.
create_sport
()
new_sport
.
name
=
data_dict
[
"
name
"
]
new_sport
.
url
=
data_dict
[
"
url
"
]
...
...
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