def on_put(self, req, resp, id): match = m.Match.get(id=id) Privilege.checkOrganizer(req.context['user'], match.tournamentId) data = req.context['data'] tournament = m.Tournament.get(id=match.tournamentId) activated = False if not match.active and data.get('active'): if not tournament.ready: raise ValueError("Tournament is not ready yet") self.activeMatch(id) activated = True # after active, because it rewrites score super(Match, self).on_put(req, resp, id, [ 'homeScore', 'awayScore', 'fieldId', 'startTime', 'endTime', 'description' ]) if 'homeScore' in data and 'awayScore' in data: pass # TODO: pri zmene skore smazat vsechny doposud odehrane body terminated = False if (match.active or activated) and not match.terminated and data.get('terminated'): self.terminateMatch(id) terminated = True if activated or terminated: resp.status = falcon.HTTP_200 req.context['result'] = Queries.getMatches(matchId=id)[0]
def on_get(self, req, resp, id): Privilege.checkOrganizer(req.context['user'], int(id)) tournament = m.Tournament.get(id=id) matches = Queries.getMatches(tournamentId=id) missingSpirits = [] for match in matches: if match['terminated'] != True: continue if match['homeTeam']['spirit'] is None: missingSpirits.append({ 'teamId': match['homeTeam']['id'], 'teamName': match['awayTeam']['name'], 'matchId': match['id'], 'ide': match['ide'] }) if match['awayTeam']['spirit'] is None: missingSpirits.append({ 'teamId': match['homeTeam']['id'], 'teamName': match['homeTeam']['name'], 'matchId': match['id'], 'ide': match['ide'] }) collection = {'count': len(missingSpirits), 'items': missingSpirits} req.context['result'] = collection
def on_post(self, req, resp, id): match = m.Match.get(id=id) Privilege.checkOrganizer(req.context['user'], match.tournamentId) data = req.context['data'] assistPlayerId = data.get('assistPlayerId') scorePlayerId = data.get('scorePlayerId') callahan = data.get('callahan', False) homePoint = bool(data['homePoint']) tournament = m.Tournament.get(id=match.tournamentId) if not tournament.ready: raise ValueError("Tournament isn't ready") if not match.active: raise ValueError("Match isn't active") teamId = match.homeTeamId if homePoint else match.awayTeamId self.checkPlayers(match.tournamentId, teamId, assistPlayerId, scorePlayerId, callahan) with m.db.transaction(): if not callahan: if assistPlayerId: self.updatePlayerStatistic(match.tournamentId, assistPlayerId, match.id, 'A') if scorePlayerId: self.updatePlayerStatistic(match.tournamentId, scorePlayerId, match.id, 'S') order, homeScore, awayScore = Queries.getLastPoint(match.id) order += 1 if homePoint: homeScore += 1 else: awayScore += 1 m.Point.insert( homePoint=homePoint, matchId=match.id, order=order, assistPlayerId=assistPlayerId if not callahan else None, scorePlayerId=scorePlayerId if not callahan else None, homeScore=homeScore, awayScore=awayScore, callahan=callahan).execute() self.updateMatchScore(match.id, homePoint) point = Queries.getPoints(matchId=match.id, order=order)[0] req.context['result'] = point resp.status = falcon.HTTP_201
def on_put(self, req, resp, id, order): match = m.Match.get(id=id) Privilege.checkOrganizer(req.context['user'], match.tournamentId) editableCols = ['assistPlayerId', 'scorePlayerId', 'callahan'] point = m.Point.get(matchId=match.id, order=order) data = req.context['data'] assistPlayerId = data.get('assistPlayerId') scorePlayerId = data.get('scorePlayerId') callahan = data.get('callahan', False) teamId = match.homeTeamId if point.homePoint else match.awayTeamId self.checkPlayers(match.tournamentId, teamId, assistPlayerId, scorePlayerId, callahan) params = None qr = None if editableCols is not None: params = {key: data[key] for key in data if key in editableCols} if callahan and 'assistPlayerId' in params: del params['assistPlayerId'] with m.db.transaction(): # delete players statistics, if they are changed if callahan: self.updatePlayerStatistic(match.tournamentId, point.assistPlayerId, match.id, 'A', (-1)) if assistPlayerId and point.assistPlayerId != assistPlayerId and not callahan: self.updatePlayerStatistic(match.tournamentId, point.assistPlayerId, match.id, 'A', (-1)) self.updatePlayerStatistic(match.tournamentId, assistPlayerId, match.id, 'A', 1) if scorePlayerId and point.scorePlayerId != scorePlayerId: self.updatePlayerStatistic(match.tournamentId, point.scorePlayerId, match.id, 'S', (-1)) self.updatePlayerStatistic(match.tournamentId, scorePlayerId, match.id, 'S', 1) if params: qr = m.Point.update(**params).where( m.Point.matchId == match.id, m.Point.order == order).execute() req.context['result'] = m.Point.select().where( m.Point.matchId == match.id, m.Point.order == order).get() resp.status = falcon.HTTP_200 if qr else falcon.HTTP_304
def on_post(self, req, resp, id): teamId = req.context['data']['teamId'] playerId = req.context['data']['playerId'] Privilege.checkOrganizer(req.context['user'], int(id)) Privilege.checkClub(req.context['user'], m.Team.get(id=teamId).clubId) tournament = m.Tournament.get(id=id) if not tournament.ready: raise ValueError("Tournament is not ready") newPlayer, created = m.PlayerAtTournament.create_or_get( tournamentId=int(id), teamId=teamId, playerId=playerId) resp.status = falcon.HTTP_201 if created else falcon.HTTP_200 req.context['result'] = newPlayer
def on_delete(self, req, resp, id): teamId = req.context['data']['teamId'] playerId = req.context['data']['playerId'] Privilege.checkOrganizer(req.context['user'], int(id)) Privilege.checkClub(req.context['user'], m.Team.get(id=teamId).clubId) matches = m.PlayerAtTournament.get(tournamentId=id, teamId=teamId, playerId=playerId).matches if matches == 0: player = m.PlayerAtTournament.get( tournamentId=id, teamId=teamId, playerId=playerId).delete_instance() else: raise ValueError("Player has played matches")
def on_post(self, req, resp, id): teamId = req.context['data']['teamId'] playerId = req.context['data']['playerId'] Privilege.checkOrganizer(req.context['user'], int(id)) Privilege.checkClub(req.context['user'], m.Team.get(id=teamId).clubId) tournament = m.Tournament.get(id=id) if not tournament.ready: raise ValueError("Tournament is not ready") newPlayer, created = m.PlayerAtTournament.create_or_get( tournamentId = int(id), teamId = teamId, playerId = playerId ) resp.status = falcon.HTTP_201 if created else falcon.HTTP_200 req.context['result'] = newPlayer
def on_put(self, req, resp, id): Privilege.checkOrganizer(req.context['user'], int(id)) tournament = m.Tournament.get(id=id) if tournament.ready: raise ValueError("Tournament is ready and teams can't be changed") data = req.context['data'] qr = m.TeamAtTournament.\ update( teamId = data['teamId'] ).\ where( m.TeamAtTournament.tournamentId == id, m.TeamAtTournament.seeding == data['seeding'] ).execute() resp.status = falcon.HTTP_200 if qr else falcon.HTTP_304 req.context['result'] = m.TeamAtTournament.get(tournamentId=id, seeding=data['seeding'])
def on_delete(self, req, resp, id): teamId = req.context['data']['teamId'] playerId = req.context['data']['playerId'] Privilege.checkOrganizer(req.context['user'], int(id)) Privilege.checkClub(req.context['user'], m.Team.get(id=teamId).clubId) matches = m.PlayerAtTournament.get( tournamentId = id, teamId = teamId, playerId = playerId ).matches if matches == 0: player = m.PlayerAtTournament.get( tournamentId = id, teamId = teamId, playerId = playerId ).delete_instance() else: raise ValueError("Player has played matches")
def on_put(self, req, resp, id): Privilege.checkOrganizer(req.context['user'], int(id)) data = req.context['data'] tournament = m.Tournament.select(m.Tournament).where(m.Tournament.id==id).get() super(Tournament, self).on_put(req, resp, id, ['name', 'startDate', 'endDate', 'city', 'country', 'caldTournamentId'] ) edited = False if tournament.ready is False and data.get('ready') is True: self.prepareTournament(id) edited = True if tournament.terminated is False and data.get('terminated') is True: self.terminateTournament(id) edited = True if edited: resp.status = falcon.HTTP_200
def on_put(self, req, resp, id): Privilege.checkOrganizer(req.context['user'], int(id)) tournament = m.Tournament.get(id=id) if tournament.ready: raise ValueError("Tournament is ready and teams can't be changed") data = req.context['data'] qr = m.TeamAtTournament.\ update( teamId = data['teamId'] ).\ where( m.TeamAtTournament.tournamentId == id, m.TeamAtTournament.seeding == data['seeding'] ).execute() resp.status = falcon.HTTP_200 if qr else falcon.HTTP_304 req.context['result'] = m.TeamAtTournament.get( tournamentId = id, seeding = data['seeding'] )
def on_put(self, req, resp, id): Privilege.checkOrganizer(req.context['user'], int(id)) data = req.context['data'] tournament = m.Tournament.select( m.Tournament).where(m.Tournament.id == id).get() super(Tournament, self).on_put(req, resp, id, [ 'name', 'startDate', 'endDate', 'city', 'country', 'caldTournamentId' ]) edited = False if tournament.ready is False and data.get('ready') is True: self.prepareTournament(id) edited = True if tournament.terminated is False and data.get('terminated') is True: self.terminateTournament(id) edited = True if edited: resp.status = falcon.HTTP_200
def on_delete(self, req, resp, id): match = m.Match.get(id=id) Privilege.checkOrganizer(req.context['user'], match.tournamentId) # TODO: unite this two queries order, homeScore, awayScore = Queries.getLastPoint(match.id) point = Queries.getPoints(match.id, order)[0] assistPlayerId = point['assistPlayer']['id'] scorePlayerId = point['scorePlayer']['id'] with m.db.transaction(): # delete from match table self.updateMatchScore(match.id, point['homePoint'], (-1)) # delete players statistics if assistPlayerId: self.updatePlayerStatistic(match.tournamentId, assistPlayerId, match.id, 'A', (-1)) if scorePlayerId: self.updatePlayerStatistic(match.tournamentId, scorePlayerId, match.id, 'S', (-1)) # delete point m.Point.delete().where(m.Point.matchId == match.id, m.Point.order == order).execute()
def on_get(self, req, resp, id): loggedUser = req.context['user'] match = Queries.getMatches(matchId=id)[0] Privilege.checkOrganizer(loggedUser, m.Match.get(id=id).tournamentId) homeSpirit = None awaySpirit = None try: homeSpirit = model_to_dict( m.Spirit.get(matchId=id, teamId=match['homeTeam']['id'])) del homeSpirit['matchId'], homeSpirit['teamId'], homeSpirit[ 'givingTeamId'] # if spirit is not club's team = m.Team.get(id=match['homeTeam']['id']) if loggedUser.role == "club" and not loggedUser.clubId == team.clubId: homeSpirit = None except m.Spirit.DoesNotExist: pass finally: match['homeTeam']['spirit'] = homeSpirit try: awaySpirit = model_to_dict( m.Spirit.get(matchId=id, teamId=match['awayTeam']['id'])) del awaySpirit['matchId'], awaySpirit['teamId'], awaySpirit[ 'givingTeamId'] # if spirit is not club's team = m.Team.get(id=match['awayTeam']['id']) if loggedUser.role == "club" and not loggedUser.clubId == team.clubId: homeSpirit = None except m.Spirit.DoesNotExist: pass finally: match['awayTeam']['spirit'] = awaySpirit req.context['result'] = match
def updateSpirit(self, matchId, data, user, put=False): logging.info("Match %s has new spirit" % matchId) match = m.Match.get(id=matchId) tournament = m.Tournament.get(id=match.tournamentId) if tournament.terminated: raise ValueError("Tournament is terminated") if not match.terminated: raise ValueError("Match is not terminated still") # check team ids receivingTeamId = int(data['teamId']) givingTeamId = self.getGivingTeamId(receivingTeamId, match) # check rights, if user is giving team Privilege.checkOrganizer(user, match.tournamentId) team = m.Team.get(id=givingTeamId) Privilege.checkClub(user, team.clubId) logging.info("Team %s gives spirit to team %s " % (givingTeamId, receivingTeamId)) data['givingTeamId'] = givingTeamId newSotg = Sotg(data['fair'], data['communication'], data['fouls'], data['positive'], data['rules']) with m.db.transaction(): # TODO: upravit spirit na prehledu zapasu!!! a otestovat oldSotg = Sotg() if put: # delete old spirit spirit = m.Spirit.get(matchId=matchId, teamId=receivingTeamId) oldSotg = Sotg(spirit.fair, spirit.communication, spirit.fouls, spirit.positive, spirit.rules) spirit.delete_instance() data['total'] = newSotg.total spirit, created = m.Spirit.get_or_create(matchId=matchId, teamId=receivingTeamId, defaults=data) logging.info("spirit: %s" % (spirit)) # given --------------------------------------------------------------- spiritAvg = m.SpiritAvg.get(tournamentId=match.tournamentId, teamId=givingTeamId) spiritAvg.communicationGiven = self.getNewAvg( spiritAvg.communicationGiven, spiritAvg.matchesGiven, newSotg.communication, oldSotg.communication) spiritAvg.fairGiven = self.getNewAvg(spiritAvg.fairGiven, spiritAvg.matchesGiven, newSotg.fair, oldSotg.fair) spiritAvg.foulsGiven = self.getNewAvg(spiritAvg.foulsGiven, spiritAvg.matchesGiven, newSotg.fouls, oldSotg.fouls) spiritAvg.positiveGiven = self.getNewAvg(spiritAvg.positiveGiven, spiritAvg.matchesGiven, newSotg.positive, oldSotg.positive) spiritAvg.rulesGiven = self.getNewAvg(spiritAvg.rulesGiven, spiritAvg.matchesGiven, newSotg.rules, oldSotg.rules) spiritAvg.totalGiven = self.getNewAvg(spiritAvg.totalGiven, spiritAvg.matchesGiven, newSotg.total, oldSotg.total) if not put: spiritAvg.matchesGiven = (spiritAvg.matchesGiven + 1) spiritAvg.save() # received ------------------------------------------------------------ spiritAvg = m.SpiritAvg.get(tournamentId=match.tournamentId, teamId=receivingTeamId) spiritAvg.communication = self.getNewAvg(spiritAvg.communication, spiritAvg.matches, newSotg.communication, oldSotg.communication) spiritAvg.fair = self.getNewAvg(spiritAvg.fair, spiritAvg.matches, newSotg.fair, oldSotg.fair) spiritAvg.fouls = self.getNewAvg(spiritAvg.fouls, spiritAvg.matches, newSotg.fouls, oldSotg.fouls) spiritAvg.positive = self.getNewAvg(spiritAvg.positive, spiritAvg.matches, newSotg.positive, oldSotg.positive) spiritAvg.rules = self.getNewAvg(spiritAvg.rules, spiritAvg.matches, newSotg.rules, oldSotg.rules) spiritAvg.total = self.getNewAvg(spiritAvg.total, spiritAvg.matches, newSotg.total, oldSotg.total) if not put: spiritAvg.matches = (spiritAvg.matches + 1) spiritAvg.save() if match.homeTeamId == receivingTeamId: match.spiritHome = newSotg.total else: match.spiritAway = newSotg.total match.save() # --------------------------------------------------------------------- return spirit, created