Skip to content

Commit

Permalink
Validate max player numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianSW committed Oct 27, 2022
1 parent feeed2d commit 95569d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions models/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ class Match(db.Document):
"queryset_class": CustomQuerySet
}

def clean(self):
if self.player_dist1 is not None or self.player_dist2 is not None:
if sum(self.player_dist1) > 50 or sum(self.player_dist2) > 50:
raise ValueError("player distributions cannot exceed 50 players")
if self.players > 100:
raise ValueError("players cannot exceed 100 players")
if (self.player_dist1 is not None or self.player_dist2 is not None) and self.players != 0:
raise ValueError("players and player distributions cannot both be set")

def needs_confirmations(self):
if (self.conf1 != "" and self.conf1 is not None) and (self.conf2 != "" and self.conf2 is not None):
# do the calcs then
Expand Down
2 changes: 2 additions & 0 deletions rest/matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ def post(self):
return handle_error(f"match already exists in database", 400)
except ValidationError as e:
return handle_error(f"required field is empty: {e}")
except ValueError as e:
return handle_error(f"request is invalid: {e}")
except Exception as e:
return handle_error(f"error creating match in database, terminated with error: {e}", 500)
else:
Expand Down

0 comments on commit 95569d0

Please sign in to comment.