Skip to content

Commit

Permalink
Merge pull request #513 from battlecode/jerrym-filter
Browse files Browse the repository at this point in the history
Properly implement filtering by active submission
  • Loading branch information
j-mao authored Jan 10, 2023
2 parents e6ddc61 + 2084adf commit 9dc1887
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
7 changes: 7 additions & 0 deletions backend/siarnaq/api/teams/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ def transform(self, term):
if term.startswith("-"):
prefix, term = "-", term[1:]
return prefix + self.FIELD_TRANSFORMS.get(term, term)


class TeamActiveSubmissionFilter(filters.BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
if "has_active_submission" not in request.query_params:
return queryset
return queryset.filter(submissions__accepted=True).distinct()
8 changes: 6 additions & 2 deletions backend/siarnaq/api/teams/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from siarnaq.api.episodes.permissions import IsEpisodeAvailable
from siarnaq.api.teams.exceptions import TeamMaxSizeExceeded
from siarnaq.api.teams.filters import TeamOrderingFilter
from siarnaq.api.teams.filters import TeamActiveSubmissionFilter, TeamOrderingFilter
from siarnaq.api.teams.models import Team, TeamStatus
from siarnaq.api.teams.permissions import IsOnTeam
from siarnaq.api.teams.serializers import (
Expand All @@ -38,7 +38,11 @@ class TeamViewSet(
When creating a team, add the logged in user as the sole member.
"""

filter_backends = [filters.SearchFilter, TeamOrderingFilter]
filter_backends = [
filters.SearchFilter,
TeamOrderingFilter,
TeamActiveSubmissionFilter,
]
ordering = "pk"
ordering_fields = ["pk", "name"] + TeamOrderingFilter.ordering_fields
search_fields = ["name", "=members__username"]
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ class Api {
//----SEARCHING----

// Search teams, ordering result by ranking.
static searchTeam(query, page, episode, callback) {
static searchTeam(query, page, episode, requireActiveSubmission, callback) {
const apiURL = `${URL}/api/team/${episode}/t`;
const encQuery = encodeURIComponent(query);
const teamUrl = `${apiURL}/?ordering=-rating,name&search=${encQuery}&page=${page}`;
const teamUrl =
`${apiURL}/?ordering=-rating,name&search=${encQuery}&page=${page}` +
(requireActiveSubmission ? `&has_active_submission=true` : ``);
$.get(teamUrl, (teamData) => {
const pageLimit = Math.ceil(teamData.count / PAGE_SIZE);
callback(teamData.results, pageLimit);
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/components/rankingTeamList.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,6 @@ class RankingTeamList extends Component {
<div className="header">
<h4 className="title">{this.props.title}</h4>
</div>
<div className="content">
Teams that do not have already submitted code will not have
"request" buttons appear. Click around the pagination to find
them. (Enhanced filtering will be coming very soon!)
</div>
{this.props.canRequest && this.props.episode_info.frozen && (
<div className="content">
Scrimmages may not be requested, due to a submission freeze for
Expand Down
18 changes: 12 additions & 6 deletions frontend/src/components/rankingTeamSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ class RankingTeamSearch extends Component {

searchTeam(input, page) {
this.setState({ loading: true, teamPage: page });
Api.searchTeam(input, page, this.props.episode, (teams, pageLimit) => {
// This check handles the case where a new page is requested while a
// previous page was loading.
if (page == this.state.teamPage) {
this.setState({ teams, pageLimit, loading: false });
Api.searchTeam(
input,
page,
this.props.episode,
this.props.requireActiveSubmission,
(teams, pageLimit) => {
// This check handles the case where a new page is requested while a
// previous page was loading.
if (page == this.state.teamPage) {
this.setState({ teams, pageLimit, loading: false });
}
}
});
);
}

componentDidMount() {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/scrimmageRequestor.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ScrimmageRequestor extends Component {
search_placeholder="Search for a Team to Scrimmage..."
history={this.props.history}
canRequest={true}
requireActiveSubmission={true}
team={this.props.team}
title="Find a team to scrimmage!"
/>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/views/rankings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Rankings extends Component {
search_placeholder="Search for a Team or User..."
history={this.props.history}
canRequest={false}
requireActiveSubmission={false}
team={this.props.team}
title="Rankings"
/>
Expand Down

0 comments on commit 9dc1887

Please sign in to comment.