Skip to content

Commit

Permalink
Fix: Resolve error in production environment (#95)
Browse files Browse the repository at this point in the history
* fix: clicking the link should not reload the page for each link

* fix: should not auto escape `set_page_title()`

* impr: update the proclass creator name during `map()`

* fix: order the proclass by proclass_id
Why did you drop it in the wrong spot? 🤣

* refactor: make pyright happier

* impr(proset): only show the proclass info button when a proclass is selected

* fix: close info dialog before navigating to another page

* impr: add check for problem existence

* impr: introduce table-driven methods to enhance e2e tests

* impr: simplify the true condition check

* fix: correct the incorrect sentence tense and form
  • Loading branch information
tobiichi3227 authored Oct 10, 2024
1 parent 5a25ea0 commit 55b4aab
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 288 deletions.
134 changes: 109 additions & 25 deletions src/handlers/manage/pro.py

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/handlers/pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,16 @@ async def post(self):
if reqtype == "listproclass":
_, accts = await UserService.inst.list_acct(UserConst.ACCTTYPE_KERNEL)
accts = {acct.acct_id: acct.name for acct in accts}

_, proclass_list = await ProClassService.inst.get_proclass_list()
proclass_list = list(map(dict, proclass_list))
for proclass in proclass_list:
def _set_creator_name(proclass):
proclass = dict(proclass)
if proclass['acct_id']:
proclass['creator_name'] = accts[proclass['acct_id']]

return proclass
proclass_list = list(map(_set_creator_name, proclass_list))

proclass_cata = {
"official": list(filter(lambda proclass: proclass['type'] == ProClassConst.OFFICIAL_PUBLIC, proclass_list)),
"shared": list(filter(lambda proclass: proclass['type'] == ProClassConst.USER_PUBLIC, proclass_list)),
Expand Down
20 changes: 6 additions & 14 deletions src/services/chal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from services.pro import ProService
from services.user import Account

TZ = datetime.timezone(datetime.timedelta(hours=+8))

class ChalConst:
STATE_AC = 1
Expand Down Expand Up @@ -108,12 +109,12 @@ def get_sql_query_str(self):
query += f' AND "challenge_state"."state" = {self.state} '

if self.compiler != 'all':
query += f' AND \"challenge\".\"compiler_type\"=\'{self.compiler}\' '
query += f' AND "challenge"."compiler_type"=\'{self.compiler}\' '

if self.contest != 0:
query += f' AND "challenge"."contest_id"={self.contest} '
else:
query += ' AND "challenge"."contest_id"=0 '
query += f' AND "challenge"."contest_id"=0 '

return query

Expand Down Expand Up @@ -270,10 +271,6 @@ async def get_chal(self, chal_id):
}
)

owner = await self.rs.get(f'{pro_id}_owner')
unlock = [1]

tz = datetime.timezone(datetime.timedelta(hours=+8))

return (
None,
Expand All @@ -283,7 +280,7 @@ async def get_chal(self, chal_id):
'acct_id': acct_id,
'contest_id': contest_id,
'acct_name': acct_name,
'timestamp': timestamp.astimezone(tz),
'timestamp': timestamp.astimezone(TZ),
'testl': testl,
'response': final_response,
'comp_type': comp_type,
Expand All @@ -309,13 +306,8 @@ async def emit_chal(self, chal_id, pro_id, testm_conf, comp_type, pri: int):

acct_id, contest_id, timestamp = int(result['acct_id']), int(result['contest_id']), result['timestamp']
limit = testm_conf['limit']

if comp_type in limit:
timelimit = limit[comp_type]['timelimit']
memlimit = limit[comp_type]['memlimit']
else:
timelimit = limit['default']['timelimit']
memlimit = limit['default']['memlimit']
timelimit = limit.get(comp_type, limit['default'])['timelimit']
memlimit = limit.get(comp_type, limit['default'])['memlimit']

async with self.db.acquire() as con:
testl = []
Expand Down
12 changes: 6 additions & 6 deletions src/services/pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async def get_pro(self, pro_id, acct: Account | None = None, is_contest: bool =
},
)

async def list_pro(self, acct: Account = None, is_contest=False):
async def list_pro(self, acct: Account | None = None, is_contest=False):
if acct is None:
max_status = ProService.STATUS_ONLINE

Expand Down Expand Up @@ -424,10 +424,10 @@ def __init__(self, db, rs):
self.rs = rs
ProClassService.inst = self

async def get_proclass(self, proclass_id):
async def get_proclass(self, proclass_id: int):
async with self.db.acquire() as con:
res = await con.fetch(
'SELECT "proclass_id", "name", "desc", "list", "acct_id", "type" FROM "proclass" WHERE "proclass_id" = $1 ORDER BY "proclass_id" ASC;',
'SELECT "proclass_id", "name", "desc", "list", "acct_id", "type" FROM "proclass" WHERE "proclass_id" = $1;',
int(proclass_id),
)

Expand All @@ -438,11 +438,11 @@ async def get_proclass(self, proclass_id):

async def get_proclass_list(self):
async with self.db.acquire() as con:
res = await con.fetch('SELECT "proclass_id", "name", "acct_id", "type" FROM "proclass";')
res = await con.fetch('SELECT "proclass_id", "name", "acct_id", "type" FROM "proclass" ORDER BY "proclass_id" ASC;')

return None, res

async def add_proclass(self, name, p_list, desc, acct_id, proclass_type):
async def add_proclass(self, name: str, p_list: list[int], desc: str, acct_id: int, proclass_type: int):
async with self.db.acquire() as con:
res = await con.fetchrow(
"""
Expand All @@ -458,7 +458,7 @@ async def add_proclass(self, name, p_list, desc, acct_id, proclass_type):

return None, res[0]

async def remove_proclass(self, proclass_id):
async def remove_proclass(self, proclass_id: int):
async with self.db.acquire() as con:
await con.execute('DELETE FROM "proclass" WHERE "proclass_id" = $1', int(proclass_id))

Expand Down
7 changes: 4 additions & 3 deletions src/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ var index = new function() {

$(document).on('click', 'a', function(e) {
let cur_href = location.href;

let href = $(this).attr('href');
let target = $(this).attr('target');
if (href == undefined || href.length == 0) return;
if ($(this).attr('target') !== "") {
return;
}
if (target) return

window.history.pushState(null, document.title, $(this).attr('href'));

if (href.startsWith('?')) {
Expand Down
2 changes: 1 addition & 1 deletion src/static/templ/acct/acct-config.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ set_page_title("Edit Account") }}
{% raw set_page_title("Edit Account") %}

{% if acct.acct_id != user.acct_id and not user.is_kernel() %}
You don't have permission.
Expand Down
10 changes: 9 additions & 1 deletion src/static/templ/proset.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
$("#infoProClass").on('click', function(e) {
let body = info_dialog.querySelector('.modal-body');
body.innerHTML = DOMPurify.sanitize(marked.parse(cur_proclass_desc));

// NOTE: close info dialog before navigating to another page.
info_dialog.querySelectorAll('a').forEach(el => {
el.addEventListener('click', _ => info_dialog_modal.dispose());
})

MathJax.Hub.Queue(["Typeset", MathJax.Hub, body]);
info_dialog_modal.show();
});
Expand Down Expand Up @@ -262,7 +268,9 @@ <h2>Created By: Official</h2>
{% end %}
<div>
<button type="button" class="btn btn-primary" id="switchProClass">Switch</button>
<button type="button" class="btn btn-success" id="infoProClass">Info</button>
{% if cur_proclass %}
<button type="button" class="btn btn-success" id="infoProClass">Info</button>
{% end %}
<a class="btn btn-warning" href="/oj/acct/proclass/{{ user.acct_id }}/">Manage</a>
</div>
</form>
Expand Down
Loading

0 comments on commit 55b4aab

Please sign in to comment.