Пример #1
0
    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
Пример #2
0
 def rename_entry(uuid, name, session=None):
     current_user = UserManager.get_current_user().platformid
     entry: GroupEntry = session.query(GroupEntry).filter(
         GroupEntry.uuid == uuid).first()
     if entry.owner != current_user:
         raise AuthorizationException()
     entry.name = name
     session.add(entry)
     session.commit()
     return entry.uuid
Пример #3
0
    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
Пример #4
0
 def delete_entry(uuid, session=None):
     current_user = UserManager.get_current_user().platformid
     entry = session.query(GroupEntry).filter(
         GroupEntry.uuid == uuid).first()
     if entry.owner != current_user:
         raise AuthorizationException()
     descendants = session.query(GroupEntry).filter(
         GroupEntry.path.descendant_of(entry.path)).all()
     for desc in descendants:
         session.delete(desc)
     session.delete(entry)
     session.commit()
     return entry.uuid
Пример #5
0
 def add_subgroup(parent_uuid, name=None, session=None):
     current_user = UserManager.get_current_user().platformid
     parent = session.query(GroupEntry).filter(
         GroupEntry.uuid == parent_uuid).first()
     if parent.owner != current_user:
         raise AuthorizationException()
     entry = GroupEntry(engine=session.get_bind(),
                        name=name,
                        owner=parent.owner,
                        type=GroupEntryType.group,
                        parent=parent)
     session.add(entry)
     session.commit()
     return entry.uuid
Пример #6
0
    def create(name, owner=None, session=None):
        if owner is None:
            current_user = UserManager.get_current_user()
            if current_user is not None:
                owner = current_user.platformid
            else:
                raise AuthorizationException()

        entry = GroupEntry(engine=session.get_bind(),
                           name=name,
                           owner=owner,
                           type=GroupEntryType.group)
        session.add(entry)
        session.commit()
        return entry.uuid
Пример #7
0
 def validate(created_query_params) -> Optional[CalculatedError]:
     for query, siblings in check_dict.items():
         if query in created_query_params:
             for value in siblings:
                 if value not in created_query_params and value not in provided_params:
                     return MissingQueryParams(siblings)
     for query, siblings in list_check.items():
         if query in created_query_params:
             for sibling_query in siblings:
                 if len(created_query_params[query]) != len(created_query_params[sibling_query.name]):
                     return MismatchedQueryParams(query, sibling_query.name,
                                                  len(created_query_params[query]),
                                                  len(created_query_params[sibling_query.name]))
     has_user = UserManager.get_current_user() is not None
     for query in require_user:
         if query in created_query_params and not has_user:
             return NotLoggedIn()
Пример #8
0
 def add_game(parent_uuid, game, name=None, session=None):
     current_user = UserManager.get_current_user().platformid
     parent = session.query(GroupEntry).filter(
         GroupEntry.uuid == parent_uuid).first()
     if parent is None:
         raise ReplayNotFound()
     if parent.owner != current_user:
         raise AuthorizationException()
     game_obj = session.query(Game).filter(Game.hash == game).first()
     if game_obj is None:
         game_obj: Game = session.query(Game).filter(
             Game.replay_id == game).first()
         if game_obj is not None:
             game = game_obj.hash
     entry = GroupEntry(engine=session.get_bind(),
                        name=name,
                        game=game,
                        owner=parent.owner,
                        type=GroupEntryType.game,
                        parent=parent)
     session.add(entry)
     session.commit()
     return entry.uuid
Пример #9
0
    def get_info(uuid, session=None) -> Group:
        if uuid is None:
            current_user = UserManager.get_current_user()
            if current_user is not None:
                peak_nodes = session.query(GroupEntry).filter(
                    GroupEntry.owner == current_user.platformid).filter(
                        func.nlevel(GroupEntry.path) == 1).order_by(
                            GroupEntry.id).all()
                children_counts = SavedGroup._children_counts(
                    peak_nodes, session)
                return Group(None, [], [
                    GroupEntryJSON.create(child, descendants).__dict__
                    for child, (
                        descendants, ) in zip(peak_nodes, children_counts)
                ])
            else:
                raise NotLoggedIn()

        entry = session.query(GroupEntry).filter(
            GroupEntry.uuid == uuid).first()
        children = session.query(GroupEntry).filter(
            GroupEntry.path.descendant_of(entry.path)).order_by(
                GroupEntry.id).filter(
                    func.nlevel(GroupEntry.path) == len(entry.path) + 1).all()
        children_counts = SavedGroup._children_counts(children, session)
        ancestors = session.query(GroupEntry).filter(
            GroupEntry.path.ancestor_of(entry.path)).filter(
                func.nlevel(GroupEntry.path) < len(entry.path)).order_by(
                    func.nlevel(GroupEntry.path)).all()
        return Group(
            GroupEntryJSON.create(entry).__dict__, [
                GroupEntryJSON.create(ancestor).__dict__
                for ancestor in ancestors
            ], [
                GroupEntryJSON.create(child, descendants).__dict__
                for child, (descendants, ) in zip(children, children_counts)
            ])
Пример #10
0
 def wrapper_require_user(*args, **kwargs):
     if UserManager.get_current_user(
     ) is None and 'internal_user' not in kwargs:
         raise NotLoggedIn()
     return decorated_function(*args, **kwargs)