core&ui: add contest/homework group filter (#589)

This commit is contained in:
panda authored and GitHub committed 2023-06-09 16:01:26 +08:00
1 parent 93bf79e6ad
commit 6f6bdee077
5 files changed
+46 -11

No files matched your search

+10 -5
View File
@@ -73,32 +73,37 @@ ScheduleModel.Worker.addHandler('contest', async (doc) => {
export class ContestListHandler extends Handler { export class ContestListHandler extends Handler {
@param('rule', Types.Range(contest.RULES), true) @param('rule', Types.Range(contest.RULES), true)
@param('group', Types.Name, true)
@param('page', Types.PositiveInt, true) @param('page', Types.PositiveInt, true)
async get(domainId: string, rule = '', page = 1) { async get(domainId: string, rule = '', group = '', page = 1) {
if (rule && contest.RULES[rule].hidden) throw new BadRequestError(); if (rule && contest.RULES[rule].hidden) throw new BadRequestError();
const groups = (await user.listGroup(domainId, this.user._id)).map((i) => i.name);
if (group && !groups.includes(group)) throw new NotAssignedError(group);
const rules = Object.keys(contest.RULES).filter((i) => !contest.RULES[i].hidden); const rules = Object.keys(contest.RULES).filter((i) => !contest.RULES[i].hidden);
const q = { const q = {
...this.user.hasPerm(PERM.PERM_VIEW_HIDDEN_CONTEST) ...this.user.hasPerm(PERM.PERM_VIEW_HIDDEN_CONTEST) && !group
? {} ? {}
: { : {
$or: [ $or: [
{ maintainer: this.user._id }, { maintainer: this.user._id },
{ owner: this.user._id }, { owner: this.user._id },
{ assign: { $in: this.user.group } }, { assign: { $in: groups } },
{ assign: { $size: 0 } }, { assign: { $size: 0 } },
], ],
}, },
...rule ? { rule } : { rule: { $in: rules } }, ...rule ? { rule } : { rule: { $in: rules } },
...group ? { assign: { $in: [group] } } : {},
}; };
const cursor = contest.getMulti(domainId, q); const cursor = contest.getMulti(domainId, q);
const qs = rule ? `rule=${rule}` : ''; let qs = rule ? `rule=${rule}` : '';
if (group) qs += qs ? `&group=${group}` : `group=${group}`;
const [tdocs, tpcount] = await paginate<Tdoc>(cursor, page, system.get('pagination.contest')); const [tdocs, tpcount] = await paginate<Tdoc>(cursor, page, system.get('pagination.contest'));
const tids = []; const tids = [];
for (const tdoc of tdocs) tids.push(tdoc.docId); for (const tdoc of tdocs) tids.push(tdoc.docId);
const tsdict = await contest.getListStatus(domainId, this.user._id, tids); const tsdict = await contest.getListStatus(domainId, this.user._id, tids);
this.response.template = 'contest_main.html'; this.response.template = 'contest_main.html';
this.response.body = { this.response.body = {
page, tpcount, qs, rule, tdocs, tsdict, page, tpcount, qs, rule, tdocs, tsdict, groups, group,
}; };
} }
} }
+9 -4
View File
@@ -21,20 +21,24 @@ const validatePenaltyRules = (input: string) => yaml.load(input);
const convertPenaltyRules = validatePenaltyRules; const convertPenaltyRules = validatePenaltyRules;
class HomeworkMainHandler extends Handler { class HomeworkMainHandler extends Handler {
@param('group', Types.Name, true)
@param('page', Types.PositiveInt, true) @param('page', Types.PositiveInt, true)
async get(domainId: string, page = 1) { async get(domainId: string, group = '', page = 1) {
const groups = (await user.listGroup(domainId, this.user._id)).map((i) => i.name);
if (group && !groups.includes(group)) throw new NotAssignedError(group);
const cursor = contest.getMulti(domainId, { const cursor = contest.getMulti(domainId, {
rule: 'homework', rule: 'homework',
...this.user.hasPerm(PERM.PERM_VIEW_HIDDEN_HOMEWORK) ...this.user.hasPerm(PERM.PERM_VIEW_HIDDEN_HOMEWORK) && !group
? {} ? {}
: { : {
$or: [ $or: [
{ maintainer: this.user._id }, { maintainer: this.user._id },
{ owner: this.user._id }, { owner: this.user._id },
{ assign: { $in: this.user.group } }, { assign: { $in: groups } },
{ assign: { $size: 0 } }, { assign: { $size: 0 } },
], ],
}, },
...group ? { assign: { $in: [group] } } : {},
}); });
const [tdocs, tpcount] = await paginate<Tdoc>(cursor, page, system.get('pagination.contest')); const [tdocs, tpcount] = await paginate<Tdoc>(cursor, page, system.get('pagination.contest'));
const calendar = []; const calendar = [];
@@ -46,8 +50,9 @@ class HomeworkMainHandler extends Handler {
} else cal.endAt = tdoc.penaltySince; } else cal.endAt = tdoc.penaltySince;
calendar.push(cal); calendar.push(cal);
} }
const qs = group ? `group=${group}` : '';
this.response.body = { this.response.body = {
tdocs, calendar, tpcount, page, tdocs, calendar, tpcount, page, qs, groups, group,
}; };
this.response.template = 'homework_main.html'; this.response.template = 'homework_main.html';
} }
@@ -12,3 +12,6 @@ $highlight-button-color = #F6DF45
.homework__date .homework__date
margin-right: rem(10px) margin-right: rem(10px)
white-space: nowrap white-space: nowrap
.filter-form
display: inline-block
@@ -60,10 +60,18 @@
<div class="section__tools"> <div class="section__tools">
<form method="get" name="filter-form"> <form method="get" name="filter-form">
{{ _('Show') }}: {{ _('Show') }}:
{% if groups.length %}
<select class="inline compact select" name="group">
<option value=""{% if not group %} selected{% endif %}>{{ _('All') }}</option>
{%- for g in groups -%}
<option value="{{ g }}"{% if group == g %} selected{% endif %}>{{ g }}</option>
{%- endfor -%}
</select>
{% endif %}
<select class="inline compact select" name="rule"> <select class="inline compact select" name="rule">
<option value=""{% if not rule %} selected{% endif %}>{{ _('All') }}</option> <option value=""{% if not rule %} selected{% endif %}>{{ _('All') }}</option>
{%- for k, v in model.contest.RULES -%} {%- for k, v in model.contest.RULES -%}
{% if k != 'homework' %} {% if not k.hidden %}
<option value="{{ k }}"{% if rule == k %} selected{% endif %}>{{ _('All {0} Contests').format(v.TEXT) }}</option> <option value="{{ k }}"{% if rule == k %} selected{% endif %}>{{ _('All {0} Contests').format(v.TEXT) }}</option>
{% endif %} {% endif %}
{%- endfor -%} {%- endfor -%}
@@ -14,6 +14,20 @@
<option value="list" selected>{{ _('List View') }}</option> <option value="list" selected>{{ _('List View') }}</option>
</select> </select>
</span> </span>
{% if groups.length %}
<form method="get" class="filter-form" name="filter-form">
{{ _('Show') }}:
<select class="inline compact select" name="group">
<option value=""{% if not group %} selected{% endif %}>{{ _('All') }}</option>
{%- for g in groups -%}
<option value="{{ g }}"{% if group == g %} selected{% endif %}>{{ g }}</option>
{%- endfor -%}
</select>
<button type="submit" class="compact button hasjs--hide">
{{ _('Search') }}
</button>
</form>
{% endif %}
{% if handler.user.hasPerm(perm.PERM_CREATE_HOMEWORK) %} {% if handler.user.hasPerm(perm.PERM_CREATE_HOMEWORK) %}
<a class="compact button" href="{{ url('homework_create') }}">{{ _('Create Homework') }}</a> <a class="compact button" href="{{ url('homework_create') }}">{{ _('Create Homework') }}</a>
{% endif %} {% endif %}
@@ -51,7 +65,7 @@
</li> </li>
{%- endfor -%} {%- endfor -%}
</ol> </ol>
{{ paginator.render(page, tpcount) }} {{ paginator.render(page, tpcount, add_qs=qs) }}
<div name="calendar_entry"></div> <div name="calendar_entry"></div>
{% endif %} {% endif %}
</div> </div>