Skip to content

Commit

Permalink
Author filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
mraron committed Jun 23, 2024
1 parent 987e14d commit 5c504a7
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 207 deletions.
8 changes: 8 additions & 0 deletions internal/njudge/memory/problemlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ func (p *ProblemListQuery) filterInvisible(ctx context.Context, req njudge.Probl
return true, nil
}

func (p *ProblemListQuery) filterAuthor(ctx context.Context, req njudge.ProblemListRequest, pr njudge.Problem) (bool, error) {
if req.AuthorFilter != nil {
return strings.Contains(pr.Author, *req.AuthorFilter), nil
}
return true, nil
}

func (p *ProblemListQuery) GetProblemList(ctx context.Context, req njudge.ProblemListRequest) (*njudge.ProblemList, error) {
allProblems, err := p.ps.GetAll(ctx)
if err != nil {
Expand All @@ -144,6 +151,7 @@ func (p *ProblemListQuery) GetProblemList(ctx context.Context, req njudge.Proble
p.filterTitle,
p.filterCategory,
p.filterInvisible,
p.filterAuthor,
}

problems := make([]njudge.Problem, 0)
Expand Down
3 changes: 2 additions & 1 deletion internal/njudge/problemlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ type ProblemListRequest struct {
PerPage int
TitleFilter string
TagFilter []string
AuthorFilter *string
CategoryFilter CategoryFilter
User *User
}

func (r ProblemListRequest) IsFiltered() bool {
return r.TitleFilter != "" || len(r.TagFilter) > 0 || r.CategoryFilter.Type != CategoryFilterNone
return r.TitleFilter != "" || len(r.TagFilter) > 0 || r.CategoryFilter.Type != CategoryFilterNone || r.AuthorFilter != nil
}

type ProblemList struct {
Expand Down
8 changes: 8 additions & 0 deletions internal/web/handlers/problemset/problemset.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ProblemListRequest struct {
TitleFilter string `query:"title"`
CategoryFilter int `query:"category"`
TagFilter string `query:"tags"`
FilterAuthor string `query:"filterAuthor"`
Author string `query:"author"`

Problemset string `param:"name"`
}
Expand Down Expand Up @@ -105,6 +107,10 @@ func GetProblemList(store problems.Store, ps njudge.Problems, cs njudge.Categori
User: c.Get("user").(*njudge.User),
}

if data.FilterAuthor == "on" {
listRequest.AuthorFilter = &data.Author
}

if data.TagFilter != "" {
listRequest.TagFilter = strings.Split(data.TagFilter, ",")
}
Expand Down Expand Up @@ -228,6 +234,8 @@ func GetProblemList(store problems.Store, ps njudge.Problems, cs njudge.Categori
result.CategoryFilterOptions = []templates.CategoryFilterOption{
{Name: "-"},
}
result.FilterAuthor = data.FilterAuthor == "on"
result.AuthorFilter = data.Author

result.CategoryFilterOptions = append(result.CategoryFilterOptions,
makeCategoryFilterOptions(tr, categories, data.CategoryFilter, categoryNameByID, par)...)
Expand Down
14 changes: 14 additions & 0 deletions internal/web/templates/problemset.templ
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type ProblemListViewModel struct {
TitleFilter string
TagsFilter string
CategoryFilterOptions []CategoryFilterOption
FilterAuthor bool
AuthorFilter string
}

templ tagList(tags []njudge.Tag) {
Expand Down Expand Up @@ -87,6 +89,18 @@ templ problemListFilter(vm ProblemListViewModel) {
</select>
<input name="tags" value={vm.TagsFilter} class="form-control" id="tagsFilterHidden" type="hidden">
</div>
<div class="form-row">
<div class="form-group col-md-2">
<div class="form-check">
<input type="checkbox" name="filterAuthor" id="filterAuthor" class="form-check-input" checked?={vm.FilterAuthor}>
<label for="filterAuthor">{Tr(ctx, "Filter author?")}</label>
</div>
</div>
<div class="form-group col-md-10">
<label for="author">{Tr(ctx, "Author")}</label>
<input type="text" id="author" name="author" class="form-control" value={vm.AuthorFilter} onkeydown="$('#filterAuthor').prop('checked', true);">
</div>
</div>
<button type="submit" class="btn btn-primary">{Tr(ctx, "Filter")}</button>
if vm.Filtered {
<a class="btn btn-danger" href=".">{Tr(ctx, "Clear")}</a>
Expand Down
257 changes: 154 additions & 103 deletions internal/web/templates/problemset_templ.go

Large diffs are not rendered by default.

161 changes: 83 additions & 78 deletions internal/web/translations/catalog.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5c504a7

Please sign in to comment.