def get_player_games(self, session, id_, replay_ids=None, filter_private=True): query = session.query(PlayerGame).join(Game) if replay_ids is not None: query = query.filter(PlayerGame.game.in_(replay_ids)) if isinstance(id_, list): query = query.filter( Game.players.contains(cast(id_, postgresql.ARRAY(String)))).filter( PlayerGame.player == id_[0]) else: query = query.filter(PlayerGame.player == id_).filter( PlayerGame.game != None) if filter_private: if UserManager.get_current_user() is not None: if not is_admin(): query = query.filter( or_(Game.visibility != GameVisibilitySetting.PRIVATE, Game.players.any(get_current_user_id()))) else: query = query.filter( Game.visibility != GameVisibilitySetting.PRIVATE) return query
def get_logs(page, limit, search, session=None): if not is_admin(): raise AuthorizationException() logs = session.query(ReplayLog).order_by(desc(ReplayLog.id)) if search is not None: search = f"%{search}%" logs = logs.filter(or_( ReplayLog.uuid.like(search), ReplayLog.game.like(search), ReplayLog.error_type.like(search), ReplayLog.log.like(search), ReplayLog.params.like(search) )) return { 'logs': [ { 'id': log.id, 'uuid': log.uuid, 'game': log.game, 'errorType': log.error_type, 'log': log.log, 'params': log.params, 'result': log.result.value } for log in logs[page * limit: (page + 1) * limit] ], 'count': logs.count() }
def update_self(code): if code != update_code or not is_admin(): raise AuthorizationException() script = os.path.join(BASE_FOLDER, 'update_run.sh') if update_code == 1234 or is_local_dev(): subprocess.call([script, 'test']) else: subprocess.call([script])
def get_games(self, session, id_, replay_ids=None, filter_private=True): query = session.query(Game).filter(Game.players.contains(cast([id_], postgresql.ARRAY(String)))) if replay_ids is not None: query = query.filter(Game.hash.in_(replay_ids)) if filter_private: if UserManager.get_current_user() is not None: if not is_admin(): query = query.filter(or_(Game.visibility != GameVisibilitySetting.PRIVATE, Game.players.any(get_current_user_id()))) else: query = query.filter(Game.visibility != GameVisibilitySetting.PRIVATE) return query
def remove_group_from_user(id_: str, group: int, session=None): if not is_admin( ): # putting this here so that it *can't* be run without being admin from anywhere raise AuthorizationException() player = session.query(Player).filter( Player.platformid == id_).one_or_none() if player is None: raise PlayerNotFound() if player.groups is None: raise CalculatedError(400, "Player groups is None") if group not in player.groups: raise CalculatedError(400, "Group is not assigned to user") player.groups.remove(group) # SQLAlchemy needs this flag to be set to actually update the DB for whatever reason flag_modified(player, "groups") session.add(player) session.commit() return jsonify({"status": "Success", "groups": player.groups})
def add_group_to_user(id_: str, group: int, session=None): if not is_admin( ): # putting this here so that it *can't* be run without being admin from anywhere raise AuthorizationException() player = session.query(Player).filter( Player.platformid == id_).one_or_none() if player is None: raise PlayerNotFound() if player.groups is None: player.groups = [] if group in player.groups: raise CalculatedError(400, "Group is already assigned to user") player.groups.append(group) flag_modified(player, "groups") session.add(player) session.commit() return jsonify({"status": "Success"})
def build_query(self, session): """ Builds a query given the current state, returns the result. This method does not modify state of this object at all :return: A filtered query. """ has_joined_game = False if self.initial_query is None: if self.is_game and self.stats_query is None: filtered_query = session.query(Game) else: filtered_query = session.query(*self.stats_query) else: filtered_query = self.initial_query if (self.start_time is not None or self.end_time is not None or self.team_size is not None): if not self.is_game: filtered_query = filtered_query.join(Game) has_joined_game = True if self.is_game or has_joined_game: # Do visibility check if not is_admin(): filtered_query = filtered_query.filter( or_(Game.visibility != GameVisibilitySetting.PRIVATE, Game.players.any(get_current_user_id()))) if self.start_time is not None: filtered_query = filtered_query.filter( Game.match_date >= self.start_time) if self.end_time is not None: filtered_query = filtered_query.filter( Game.match_date <= self.end_time) if self.rank is not None: filtered_query = filtered_query.filter( PlayerGame.rank == self.rank) if self.team_size is not None: filtered_query = filtered_query.filter( Game.teamsize == self.team_size) if self.playlists is not None: filtered_query = filtered_query.filter( Game.playlist.in_(self.playlists)) if self.safe_checking: filtered_query = filtered_query.filter( PlayerGame.total_hits > 0).filter(PlayerGame.time_in_game > 0) if self.players is not None and len(self.players) > 0: filtered_query = filtered_query.filter( self.handle_list(PlayerGame.player, self.players)) if self.contains_all_players is not None and len( self.contains_all_players) > 0: filtered_query = filtered_query.filter( self.handle_union(Game.players, self.contains_all_players)) if self.replay_ids is not None and len(self.replay_ids) > 0: if self.is_game or has_joined_game: filtered_query = filtered_query.filter( self.handle_list(Game.hash, self.replay_ids)) else: filtered_query = filtered_query.filter( self.handle_list(PlayerGame.game, self.replay_ids)) elif self.replay_ids is not None and len(self.replay_ids) == 1: if self.is_game or has_joined_game: filtered_query = filtered_query.filter( Game.hash == self.replay_ids) else: filtered_query = filtered_query.filter( PlayerGame.game == self.replay_ids) # Todo: implement tags remember to handle table joins correctly return filtered_query