Skip to content

Commit adfa083

Browse files
committed
Validate max player numbers
Fixes #78
1 parent cf50393 commit adfa083

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

models/match.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,20 @@ class Match(db.Document):
6060
"queryset_class": CustomQuerySet
6161
}
6262

63+
def clean(self):
64+
if self.player_dist1 is not None or self.player_dist2 is not None:
65+
if sum(self.player_dist1) > 50 or sum(self.player_dist2) > 50:
66+
raise ValueError("player distributions cannot exceed 50 players")
67+
if self.players > 100:
68+
raise ValueError("players cannot exceed 100 players")
69+
if (self.player_dist1 is not None or self.player_dist2 is not None) and self.players != 0:
70+
raise ValueError("players and player distributions cannot both be set")
6371

6472
def needs_confirmations(self):
6573
if (self.conf1 != "" and self.conf1 is not None) and (self.conf2 != "" and self.conf2 is not None):
6674
# do the calcs then
6775
return False
6876
return True
6977

70-
7178
def to_dict(self):
7279
return json.loads(self.to_json())

rest/matches.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ def post(self):
234234
return handle_error(f"match already exists in database", 400)
235235
except ValidationError as e:
236236
return handle_error(f"required field is empty: {e}")
237+
except ValueError as e:
238+
return handle_error(f"request is invalid: {e}")
237239
except Exception as e:
238240
return handle_error(f"error creating match in database, terminated with error: {e}", 500)
239241
else:

0 commit comments

Comments
 (0)