Skip to content
Snippets Groups Projects
Commit b58cd610 authored by borzechof99's avatar borzechof99 :whale2:
Browse files

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
......@@ -10,8 +10,7 @@ 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):
if not (value <= 10 and value >= 1 or value == -1):
raise ValidationError(u"%s is not a valid rating!" % value)
......@@ -79,6 +78,17 @@ class Sport(models.Model):
criterion_rating = CriterionRating.objects.get(sport=self, criterion=criterion)
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):
"""
......
......@@ -2,7 +2,7 @@
Serializers creating JSONs for every Model from .models
"""
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):
......@@ -79,7 +79,34 @@ class SingleSportSerializer(serializers.BaseSerializer):
return serialized_data
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
......@@ -7,7 +7,7 @@ from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from django.http import JsonResponse, HttpResponse
from .pagination import PageNumberWithPageSizePagination
from .serializers import (
......@@ -91,24 +91,7 @@ class SmallSportListView(viewsets.ViewSet):
for sport in sports:
filled_criteria_pks = []
# 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))
is_filled_tuples.append((sport, sport.is_filled()))
serializer = SmallSportListSerializer(is_filled_tuples)
......@@ -119,7 +102,9 @@ class SmallSportListView(viewsets.ViewSet):
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()
......@@ -129,10 +114,8 @@ class SmallSportListView(viewsets.ViewSet):
# Before writing other Database Entries for Rating, the Sport needs to be saved once
new_sport.save()
for criterion in data_dict["criteria"]:
new_sport.rate(
Criterion.objects.get(pk=criterion["id"]), criterion["value"]
)
for criterion, value in data_dict["criteria"]:
new_sport.rate(criterion, value)
new_sport.save()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment