Commit a6809e27 authored by Zheng Yile's avatar Zheng Yile
Browse files

增加`get_data`的信息,可以指定 before 或 after.

parent 05166e57
......@@ -7,7 +7,6 @@ __pycache__/
.idea/
*.sav
ui2py.bat
build.bat
build/
/build*
dist/
*.spec
\ No newline at end of file
......@@ -363,11 +363,11 @@ def submit(eid, pid, lang, solutioncode):
else:
return _submit_not_from_pack(eid, lang, solutioncode)
def get_data(eid, pid, codecnt=None):
def get_data(eid, pid, before=None, after=None, cnt=None):
if pid:
return _get_data_from_pack(eid, pid, codecnt)
return _get_data_from_pack(eid=eid, pid=pid, before=before, after=after, cnt=cnt)
else:
return _get_data_not_from_pack(eid, codecnt)
return _get_data_not_from_pack(eid=eid, before=before, after=after, cnt=cnt)
def get_exercise(eid, pid, lang, feedback='dict'):
if pid:
......@@ -608,116 +608,154 @@ mutation ($eid: ID!, $pid: ID, $lang: Language!, $sol: String!, $a: JSONObject)
if not res: return False
return json.loads(res.text)
def _get_data_not_from_pack(eid, codecnt=None):
if codecnt == None: codecnt = 1
if type(codecnt) == str:
def _get_data_not_from_pack(eid, before=None, after=None, cnt=None):
if cnt == None: cnt = 1
if type(cnt) == str:
try:
codecnt = int(codecnt)
cnt = int(cnt)
except:
pass
if type(codecnt) != int:
if type(cnt) != int:
report(
'_get_data_not_from_pack: Variable `codecnt` type error. (should be `int`, not `{}`)'.format(type(codecnt)),
'_get_data_not_from_pack: Variable `cnt` type error. (should be `int`, not `{}`)'.format(type(cnt)),
1)
return False
headers = coding_base_headers.copy()
data = json.dumps({
data = {
"operationName": "codingExercise",
"variables": {
"eid": eid,
"codecnt": codecnt
},
"query": '''
query codingExercise($eid: ID!, $codecnt: Int!) {
node(id: $eid) {
... on CodingExercise {
viewerStatus {
exerciseStatuses(last: $codecnt) {
nodes {
... on CodingExerciseStatus {
}
}
if before:
data['variables']['before'] = before
data['variables']['lastcnt'] = cnt
queryargs = r"$eid: ID!, $lastcnt: Int!, $before: String"
funargs = r"last: $lastcnt, before: $before"
elif after:
data['variables']['after'] = after
data['variables']['firstcnt'] = cnt
queryargs = r"$eid: ID!, $firstcnt: Int!, $after: String"
funargs = r"first: $firstcnt, after: $after"
else:
data['variables']['lastcnt'] = cnt
queryargs = r"$eid: ID!, $lastcnt: Int!, $before: String"
funargs = r"last: $lastcnt, before: $before"
data['query'] = f'''
query codingExercise({queryargs}) {{
node(id: $eid) {{
... on CodingExercise {{
viewerStatus {{
exerciseStatuses({funargs}) {{
nodes {{
... on CodingExerciseStatus {{
id
scoreRate
submission {
submission {{
id
reports {
reports {{
key
value
}
}
solution {
}}
}}
solution {{
lang
asset { content }
}
}
}
}
}
}
}
}'''})
asset {{ content }}
}}
}}
}}
}}
}}
}}
}}
}}'''
data = json.dumps(data)
res = post(url=url, headers=headers, data=data)
if not res: return False
return json.loads(res.text)['data']['node']['viewerStatus']['exerciseStatuses']['nodes']
try:
return json.loads(res.text)['data']['node']['viewerStatus']['exerciseStatuses']['nodes']
except:
return False
def _get_data_from_pack(eid, pid, codecnt=None):
if codecnt == None: codecnt = 1
if type(codecnt) == str:
def _get_data_from_pack(eid, pid, before=None, after=None, cnt=None):
if cnt == None: cnt = 1
if type(cnt) == str:
try:
codecnt = int(codecnt)
cnt = int(cnt)
except:
pass
if type(codecnt) != int:
report('_get_data_from_pack: Variable `codecnt` type error. (should be `int`, not `{}`)'.format(type(codecnt)),
if type(cnt) != int:
report('_get_data_from_pack: Variable `cnt` type error. (should be `int`, not `{}`)'.format(type(cnt)),
1)
return False
headers = coding_base_headers.copy()
data = json.dumps({
data = {
"operationName": "codingExercise",
"variables": {
"eid": eid,
"pid": pid,
"codecnt": codecnt
},
"query": '''
query codingExercise($eid: ID!, $pid: ID, $codecnt: Int!) {
node(id: $pid) {
... on ExercisePack {
}
}
if before:
data['variables']['before'] = before
data['variables']['lastcnt'] = cnt
queryargs = r"$eid: ID!, $pid: ID, $lastcnt: Int!, $before: String"
funargs = r"last: $lastcnt, before: $before"
elif after:
data['variables']['after'] = after
data['variables']['firstcnt'] = cnt
queryargs = r"$eid: ID!, $pid: ID, $firstcnt: Int!, $after: String"
funargs = r"first: $firstcnt, after: $after"
else:
data['variables']['lastcnt'] = cnt
queryargs = r"$eid: ID!, $pid: ID, $lastcnt: Int!, $before: String"
funargs = r"last: $lastcnt, before: $before"
data['query'] = f'''
query codingExercise({queryargs}) {{
node(id: $pid) {{
... on ExercisePack {{
id
codingExercise(id: $eid) {
codingExercise(id: $eid) {{
id
title
tags
viewerStatus {
viewerStatus {{
passedCount
totalCount
exerciseStatuses(last: $codecnt) {
nodes {
... on CodingExerciseStatus {
exerciseStatuses({funargs}) {{
nodes {{
... on CodingExerciseStatus {{
id
scoreRate
submission {
submission {{
id
reports {
reports {{
key
value
}
}
solution {
}}
}}
solution {{
lang
asset { content }
}
asset {{ content }}
}}
time
}
}
}
}
}
}
}
}'''})
}}
}}
}}
}}
}}
}}
}}
}}'''
data = json.dumps(data)
res = post(url=url, headers=headers, data=data)
if not res: return False
return json.loads(res.text)['data']['node']['codingExercise']['viewerStatus']['exerciseStatuses']['nodes']
try:
return json.loads(res.text)['data']['node']['codingExercise']['viewerStatus']['exerciseStatuses']['nodes']
except:
return False
def _get_exercise_not_from_pack(eid, lang, feedback='dict'):
headers = coding_base_headers.copy()
......@@ -756,13 +794,25 @@ query codingExercise($eid: ID!, $lang: Language!) {
res = post(url=url, headers=headers, data=data)
if not res: return False
if feedback == 'Response': return res
res_data = json.loads(res.text)['data']['exercise']
try:
res_data = json.loads(res.text)['data']['exercise']
except:
return False
res_dic = {}
res_dic['title'] = res_data['title']
res_dic['tags'] = res_data['tags']
res_dic['description-content'] = res_data['description']['content']
res_dic['inputDescription-content'] = res_data['inputDescription']['content']
res_dic['outputDescription-content'] = res_data['outputDescription']['content']
if res_data['description'] != None:
res_dic['description-content'] = res_data['description']['content']
else:
res_dic['description-content'] = None
if res_data['inputDescription'] != None:
res_dic['inputDescription-content'] = res_data['inputDescription']['content']
else:
res_dic['inputDescription-content'] = None
if res_data['outputDescription'] != None:
res_dic['outputDescription-content'] = res_data['outputDescription']['content']
else:
res_dic['outputDescription-content'] = None
res_dic['sampleData'] = []
for x in res_data['sampleData']:
toappend = {}
......@@ -837,13 +887,25 @@ query codingExercise($eid: ID!, $pid: ID, $lang: Language!) {
res = post(url=url, headers=headers, data=data)
if not res: return False
if feedback == 'Response': return res
res_data = json.loads(res.text)['data']['pack']['codingExercise']
try:
res_data = json.loads(res.text)['data']['pack']['codingExercise']
except:
return False
res_dic = {}
res_dic['title'] = res_data['title']
res_dic['tags'] = res_data['tags']
res_dic['description-content'] = res_data['description']['content']
res_dic['inputDescription-content'] = res_data['inputDescription']['content']
res_dic['outputDescription-content'] = res_data['outputDescription']['content']
if res_data['description'] != None:
res_dic['description-content'] = res_data['description']['content']
else:
res_dic['description-content'] = None
if res_data['inputDescription'] != None:
res_dic['inputDescription-content'] = res_data['inputDescription']['content']
else:
res_dic['inputDescription-content'] = None
if res_data['outputDescription'] != None:
res_dic['outputDescription-content'] = res_data['outputDescription']['content']
else:
res_dic['outputDescription-content'] = None
res_dic['sampleData'] = []
for x in res_data['sampleData']:
toappend = {}
......
......@@ -49,7 +49,7 @@ class Requests:
if len(conf) == 2:
variables[conf[0]] = conf[1]
else:
report('Use `show VAR` to show the variables.', 1)
report('Use `show VAR` to show the variables, or `del VAR` to reset the variables.', 1)
elif conf[0] in ['del', 'reset']:
if len(conf) == 2:
if conf[1] in variables or conf[1] in aliases:
......@@ -107,9 +107,9 @@ class Requests:
report(e, 1)
else:
try:
res = get_data(eid=variables['e'], pid=variables['p'], codecnt=int(conf[1]))
res = get_data(eid=variables['e'], pid=variables['p'], cnt=int(conf[1]))
except ValueError:
report('Invalid request: type(codecnt) should be int.', 1)
report('Invalid request: type(cnt) should be int.', 1)
else:
if res == None:
report('Invalid request.', 1)
......@@ -129,9 +129,9 @@ class Requests:
report(e, 1)
elif conf[2] == 'to':
try:
res = get_data(eid=variables['e'], pid=variables['p'], codecnt=int(conf[1]))
res = get_data(eid=variables['e'], pid=variables['p'], cnt=int(conf[1]))
except ValueError:
report('Invalid request: type(codecnt) should be int.', 1)
report('Invalid request: type(cnt) should be int.', 1)
else:
try:
with open('./tmp.txt', 'w', encoding='utf-8') as f:
......@@ -143,9 +143,9 @@ class Requests:
elif len(conf) == 4:
if conf[2] == 'to':
try:
res = get_data(eid=variables['e'], pid=variables['p'], codecnt=int(conf[1]))
res = get_data(eid=variables['e'], pid=variables['p'], cnt=int(conf[1]))
except ValueError:
report('Invalid request: type(codecnt) should be int.', 1)
report('Invalid request: type(cnt) should be int.', 1)
else:
try:
with open(conf[3], 'w', encoding='utf-8') as f:
......@@ -222,22 +222,51 @@ class Requests:
report('Invalid request.', 1)
else:
report('Invalid request.', 1)
elif conf[0] in ['gr', 'getr', 'getres', 'getreport', 'getreports']:
if len(conf) <= 2:
if variables['e']:
if len(conf) == 1:
res = get_data(eid=variables['e'], pid=variables['p'])
elif len(conf) == 2:
elif conf[0] in ['gr', 'getr', 'getres', 'getreport', 'getreports']: # getpack [N] [before|after ID]
if len(conf) <= 4:
res = None
if len(conf) <= 2:
if variables['e']:
if len(conf) == 1:
res = get_data(eid=variables['e'], pid=variables['p'])
elif len(conf) == 2:
try:
res = get_data(eid=variables['e'], pid=variables['p'], cnt=int(conf[1]))
except ValueError:
if conf[1].lower() == 'all':
self.show_msg(json.dumps(get_data(eid=variables['e'], pid=variables['p'])))
else:
report('Invalid request: type(cnt) should be int.', 1)
return
else:
pass
else:
report("No eid specified.", 1)
elif len(conf) == 3:
if conf[1] in ['bf', 'before']:
res = get_data(eid=variables['e'], pid=variables['p'], before=conf[2])
elif conf[1] in ['af', 'after']:
res = get_data(eid=variables['e'], pid=variables['p'], after=conf[2])
else:
report('Invalid request.', 1)
elif len(conf) == 4:
if conf[2] in ['bf', 'before']:
try:
res = get_data(eid=variables['e'], pid=variables['p'], codecnt=int(conf[1]))
res = get_data(eid=variables['e'], pid=variables['p'], before=conf[3], cnt=int(conf[1]))
except ValueError:
if conf[1].lower() == 'all':
self.show_msg(json.dumps(get_data(eid=variables['e'], pid=variables['p'])))
else:
report('Invalid request: type(codecnt) should be int.', 1)
return
report('Invalid request: type(cnt) should be int.', 1)
if conf[2] in ['af', 'after']:
try:
res = get_data(eid=variables['e'], pid=variables['p'], after=conf[3], cnt=int(conf[1]))
except ValueError:
report('Invalid request: type(cnt) should be int.', 1)
else:
pass
report('Invalid request.', 1)
else:
pass
if not res:
report("No result.", 1)
else:
res = {x['key']: x['value'] for x in res[0]['submission']['reports']}
try:
score = res['score'].split('/')
......@@ -252,8 +281,6 @@ class Requests:
}))
else:
self.show_msg(json.dumps(res))
else:
report("No eid specified.", 1)
else:
report('Invalid request.', 1)
elif conf[0] in ['sp', 'showp', 'showpack']:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment