From da4920fcb07205a232cc4498608f0bb5c574db78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Stani=C3=B3w?= Date: Thu, 28 Dec 2017 16:44:06 +0100 Subject: [PATCH] feat(Matches): Invalidate score 0-0 --- api/serializers.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index c94da75..ae60a87 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -76,6 +76,13 @@ class MatchSerializer(serializers.ModelSerializer): blue_def = serializers.SlugRelatedField(slug_field='username', queryset=Member.objects.all()) points = serializers.IntegerField(required=False) + class Meta: + model = Match + fields = ( + 'id', 'red_att', 'red_def', 'blue_att', 'blue_def', 'date', + 'red_score', 'blue_score', 'points' + ) + def __init__(self, *args, **kwargs): # Ensure that we use only members within team context, if present ctx = kwargs.get('context', None) @@ -90,9 +97,9 @@ def __init__(self, *args, **kwargs): field.queryset = field.queryset.filter(team_id=team_id) super(MatchSerializer, self).__init__(*args, **kwargs) - class Meta: - model = Match - fields = ( - 'id', 'red_att', 'red_def', 'blue_att', 'blue_def', 'date', - 'red_score', 'blue_score', 'points' - ) + def validate(self, data): + rs = data.get('red_score', None) + bs = data.get('blue_score', None) + if rs == 0 and bs == 0: + raise serializers.ValidationError('Cannot add match with score 0-0. Was it a typo?') + return data