Beispiel #1
0
    def file_handle(self, fh, team_id):
        """Get serialization for a file handle. Each file is associated with a
        specific team that is identified by the given id.

        Parameters
        ----------
        fh: benchengine.filestore.base.FileHandle
            Handle for uploaded file
        team_id: string
            Unique team identifier

        Returns
        -------
        dict
        """
        return {
            labels.ID:
            fh.identifier,
            labels.NAME:
            fh.name,
            labels.CREATED_AT:
            fh.created_at.isoformat(),
            labels.FILESIZE:
            fh.size,
            labels.LINKS:
            hateoas.serialize({
                hateoas.DELETE:
                self.urls.delete_file(team_id=team_id, file_id=fh.identifier),
                hateoas.DOWNLOAD:
                self.urls.download_file(team_id=team_id, file_id=fh.identifier)
            })
        }
Beispiel #2
0
    def service_descriptor(self, name, version):
        """Serialization of the service descriptor. The descriptor contains the
        service name, version, and a list of HATEOAS references.

        Parameters
        ----------
        name: string
            Service name
        version: string
            Service version number

        Returns
        -------
        dict
        """
        return {
            labels.NAME:
            name,
            labels.VERSION:
            version,
            labels.LINKS:
            hateoas.serialize({
                hateoas.SELF:
                self.urls.service_descriptor(),
                hateoas.user(hateoas.LOGIN):
                self.urls.login(),
                hateoas.user(hateoas.LOGOUT):
                self.urls.logout(),
                hateoas.user(hateoas.REGISTER):
                self.urls.logout(),
                hateoas.benchmark(hateoas.LIST):
                self.urls.list_benchmarks(),
            })
        }
Beispiel #3
0
    def team_listing(self, teams, user_id=None):
        """Get serialization of the team descriptor list. The optional user id
        identifiers the current user and determines the content of the HATEOAS
        links list.

        Parameters
        ----------
        teams: list(benchengine.user.team.base.TeamDescriptor)
            Descriptors for competition teams
        user_id: string, optional
            Unique identifier of current user

        Returns
        -------
        dict
        """
        url = self.urls.list_teams()
        return {
            labels.TEAMS:
            [self.team_descriptor(team, user_id=user_id) for team in teams],
            labels.LINKS:
            hateoas.serialize({
                hateoas.SELF: url,
                hateoas.CREATE: url
            })
        }
Beispiel #4
0
    def team_descriptor(self, team, user_id=None):
        """Get serialization for a team descriptor. The optional user identifier
        specifies the current user. If given it controls the references that are
        included in the HATEOAS links list. Only team owners, for example, are
        allowed to delete a team.

        Parameters
        ----------
        team: benchengine.user.team.base.TeamDescriptor
            Team descriptor
        user_id: string, optional
            Unique identifier of current user

        Returns
        -------
        dict
        """
        team_id = team.identifier
        team_url = self.urls.get_team(team_id)
        links = {
            hateoas.SELF: team_url,
            hateoas.UPLOAD: self.urls.upload_file(team_id)
        }
        if user_id is None or user_id == team.owner_id:
            links[hateoas.DELETE] = team_url
            links[hateoas.ADD] = self.urls.add_team_members(team_id)
        return {
            labels.ID: team_id,
            labels.NAME: team.name,
            labels.OWNER_ID: team.owner_id,
            labels.MEMBER_COUNT: team.member_count,
            labels.LINKS: hateoas.serialize(links)
        }
Beispiel #5
0
    def benchmark_descriptor(self, benchmark):
        """Get dictionary serialization containing the descriptor of a
        benchmark resource.

        Parameters
        ----------
        benchmark: benchengine.benchmark.base.BenchmarkDescriptor
            Competition handle

        Returns
        -------
        dict
        """
        benchmark_id = benchmark.identifier
        leaderboard_url = self.urls.get_leaderboard(benchmark_id)
        obj = {
            labels.ID:
            benchmark_id,
            labels.NAME:
            benchmark.name,
            labels.LINKS:
            hateoas.serialize({
                hateoas.SELF:
                self.urls.get_benchmark(benchmark_id),
                hateoas.benchmark(hateoas.LEADERBOARD):
                leaderboard_url
            })
        }
        if benchmark.has_description():
            obj[labels.DESCRIPTION] = benchmark.description
        if benchmark.has_instructions():
            obj[labels.INSTRUCTIONS] = benchmark.instructions
        return obj
Beispiel #6
0
    def success(self, links=None):
        """Simple object indicating successful operations that do not have any
        further return value. May contain an optinal list of HATEOAS references.

        Parameters
        ----------
        links: dict(), optional
            Optional list of HATEOAS references

        Returns
        -------
        dict
        """
        obj = {labels.STATE: 'SUCCESS'}
        # Add optional HATEOAS references if given
        if not links is None:
            obj[labels.LINKS] = hateoas.serialize(links)
        return obj
Beispiel #7
0
    def benchmark_listing(self, benchmarks):
        """Get dictionary serialization of a benchmark listing.

        Parameters
        ----------
        benchmarks: list(benchengine.benchmark.base.BenchmarkDescriptor)
            List of benchmark descriptors

        Returns
        -------
        dict
        """
        return {
            labels.BENCHMARKS:
            [self.benchmark_descriptor(b) for b in benchmarks],
            labels.LINKS:
            hateoas.serialize({hateoas.SELF: self.urls.list_benchmarks()})
        }
Beispiel #8
0
    def user(self, user):
        """Get serialization for a given registered user.

        Parameters
        ----------
        user: benchengine.user.base.RegisteredUser
            User object

        Returns
        -------
        dict
        """
        return {
            labels.ID: user.identifier,
            labels.USERNAME: user.username,
            labels.LINKS: hateoas.serialize({
                hateoas.user(hateoas.LOGIN): self.urls.login(),
                hateoas.user(hateoas.LOGOUT): self.urls.logout()
            })
        }
Beispiel #9
0
    def login(self, access_token):
        """Serialization for successful login. Contains tha access token and a
        list of HATEOAS references.

        Parameters
        ----------
        access_token: string
            User access token

        Returns
        -------
        dict
        """
        return {
            labels.ACCESS_TOKEN: access_token,
            labels.LINKS: hateoas.serialize({
                hateoas.SERVICE: self.urls.service_descriptor(),
                hateoas.user(hateoas.LOGOUT): self.urls.logout()
            })
        }
Beispiel #10
0
    def team_handle(self, team, user_id=None):
        """Get serialization for a team handle. The optional user identifier
        specifies the current user that controlls the list of HATEOAS links
        in the response.

        Parameters
        ----------
        team: benchengine.user.team.base.TeamHandle
            Team descriptor
        user_id: string, optional
            Unique identifier of current user

        Returns
        -------
        dict
        """
        # The serialization of the team handle is an extension of the team
        # descriptor serialization
        obj = self.team_descriptor(team, user_id=user_id)
        # Add serializations for all team members
        members = list()
        for user in team.members.values():
            members.append({
                labels.ID:
                user.identifier,
                labels.USERNAME:
                user.username,
                labels.LINKS:
                hateoas.serialize({
                    hateoas.DELETE:
                    self.urls.remove_team_member(team_id=team.identifier,
                                                 user_id=user.identifier)
                })
            })
        obj[labels.MEMBERS] = members
        return obj