Newer
Older
"""
Serializers creating JSONs for every Model from .models
"""
from rest_framework import serializers
from .models import Sport, Criterion, Question, validate_rating
class SportListSerializer(serializers.ModelSerializer):
"""
Serializes all sports.
"""
class Meta:
model = Sport
class QuestionListSerializer(serializers.ModelSerializer):
"""
Serializes all Questions.
"""
class Meta:
model = Question
class CriterionListSerializer(serializers.ModelSerializer):
"""
Serializes Criterions
"""
class Meta:
model = Criterion
class SmallSportListSerializer(serializers.BaseSerializer):
"""
Serializes Lists of Sport Objects in a "Simple" Manner, with Criterions being represented in a Bool.
"""
def to_representation(self, sport_filled_tuples):
"""
Takes a List of (Sport, bool) tuples to Serialize.
The Bool Represents whether the Sport is Filled or not.
"""
serialized_data = []
serialized_data.append(
{
"pk": sport.pk,
"name": sport.name,
"url": sport.url,
"is_filled": boolean,
}
)
return serialized_data
class SingleSportSerializer(serializers.BaseSerializer):
"""
Serializes and Deserializes a Single Sport Object
"""
def to_representation(self, sport):
"""
Takes a Single Sport Object and Serializes it and all its Criteria
"""
serialized_data = {}
serialized_data["pk"] = sport.pk
serialized_data["name"] = sport.name
serialized_data["url"] = sport.url
criteria = []
for criterion in sport.criteria_ratings.iterator():
criterion_data = {}
criterion_data["pk"] = criterion.pk
criterion_data["name"] = criterion.name
criterion_data["value"] = sport.get_rating(criterion)
criteria.append(criterion_data)
serialized_data["criteria"] = criteria
return serialized_data
def to_internal_value(self, request):

borzechof99
committed
"""
The Data in the Request is taken and written to another Dictionary.
During this process, the Data is Validated on whether the Rating Value and Criterion ID are valid.
If the Request is PATCHing or PUTting an existing Sport, not every field must be existant.
So, the existance is explicitly checked.
TODO: Different Functions based on PUT or PATCH?

borzechof99
committed
"""
sport_dictionary = {}
# If The Sport is Being Patched, Name or URL may not be changed.
# That means that those Fields might not be sent in the Request,
# leading to needing to check whether they exist.
if "name" in request.data.keys():
sport_dictionary["name"] = request.data["name"]
if "url" in request.data.keys():
sport_dictionary["url"] = request.data["url"]

borzechof99
committed
# A Number of Criteria may be sent with the Sport
sport_dictionary["criteria"] = []

borzechof99
committed
# For every Sent Criterion, the ID of the Criterion and the Rating Value is being tested for Validity

borzechof99
committed
for criterion in request.data["criteria"]:
value = criterion["value"]
try:
validate_rating(value)
crit = Criterion.objects.get(pk=criterion["id"])
return None
sport_dictionary["criteria"].append((crit, value))
return sport_dictionary
class IncompleteSportSerializer(serializers.BaseSerializer):
"""
Serializes every Sport Object with Incomplete Criteria Ratings.
Includes the Name and ID of both the Sport and the Criteria.
"""
def to_representation(self, incomplete_sports):
"""
Serializes Every given Sport Object and goes through every Criterium to serialize those that are unrated.
"""
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
incomplete_sport_list = []
for sport in incomplete_sports:
incomplete_sport = {
"id": sport.pk,
"name": sport.name,
"criteria": [],
}
for crit in sport.criteria_ratings.iterator():
if sport.get_rating(crit) != -1:
continue
incomplete_sport["criteria"].append(
{
"id": crit.pk,
"name": crit.name,
}
)
incomplete_sport_list.append(incomplete_sport)
return incomplete_sport_list
class CriteriaSerializer(serializers.BaseSerializer):
"""
Serializes Every Criterium and Metadata
"""
def to_representation(self, data):
"""
Takes Tuples of (Criterium, Int, Int),
where the Integers are the Number of Sports in which the Rating is >1
and the cumulated sum of Ratings >1, respectively
"""
criteria_list = []
for crit, active_sports, sum_of_weights in data:
criterion_dict = {}
criterion_dict["id"] = crit.pk
criterion_dict["name"] = crit.name
criterion_dict["number_of_sports_active"] = active_sports
criterion_dict["sum_of_weights"] = sum_of_weights

borzechof99
committed
criteria_list.append(criterion_dict)

borzechof99
committed
return criteria_list