summaryrefslogtreecommitdiff
path: root/kpov_judge/web
diff options
context:
space:
mode:
Diffstat (limited to 'kpov_judge/web')
-rwxr-xr-xkpov_judge/web/kpov_judge/kpov_judge.py158
-rw-r--r--kpov_judge/web/kpov_judge/templates/index.html6
2 files changed, 99 insertions, 65 deletions
diff --git a/kpov_judge/web/kpov_judge/kpov_judge.py b/kpov_judge/web/kpov_judge/kpov_judge.py
index 1bd42c5..2b1d3c6 100755
--- a/kpov_judge/web/kpov_judge/kpov_judge.py
+++ b/kpov_judge/web/kpov_judge/kpov_judge.py
@@ -29,6 +29,7 @@ def gen_params(user_id, meta):
params_meta = {'neznano_ime_naloge': {'opis':'NEZNANA NALOGA', 'w': False}}
"""
+
@app.before_request
def before_request():
g.db = pymongo.MongoClient(app.config['DB_HOST']).kpov
@@ -39,21 +40,40 @@ def before_request():
except Exception as e:
raise e #no auth or auth config?
+
@app.route('/')
def root():
student_id = flask.app.request.environ.get('REMOTE_USER', 'Nobody')
# env = flask.app.request.environ
- tasks = g.db.tasks.find({}, {'task_id': 1}).sort('task_id')
+ classes = g.db.classes.find({}, {'class_id': 1, 'name': 1}).sort('class_id')
+ return render_template('index.html', student_id=student_id, classes=classes)
+
+
+@app.route('/classes/<class_id>/')
+def class_tasks(class_id):
+ student_id = flask.app.request.environ.get('REMOTE_USER', 'Nobody')
+ # env = flask.app.request.environ
+ clas = g.db.classes.find_one({'class_id': class_id})
+ tasks = g.db.tasks.find({'class_id': class_id}, {'task_id': 1}).sort('task_id')
if tasks is not None:
task_list = [i['task_id'] for i in tasks]
else:
task_list = []
- return render_template('index.html', student_id=student_id, tasks=task_list)
+ return render_template('class_tasks.html', student_id=student_id, tasks=task_list, clas=clas)
+
-def results_post(task_id, results):
+@app.route('/classes/')
+def class_list():
+ student_id = flask.app.request.environ.get('REMOTE_USER', 'Nobody')
+ # env = flask.app.request.environ
+ classes = g.db.classes.find({}, {'class_id': 1, 'name': 1,}).sort('name')
+ return render_template('class_list.html', student_id=student_id, classes=classes)
+
+
+def results_post(class_id, task_id, results):
student_id = flask.app.request.environ.get('REMOTE_USER', 'Nobody')
db = g.db
- params = db.task_params.find_one({'task_id': task_id, 'student_id': student_id})['params']
+ params = db.task_params.find_one({'class_id': class_id, 'task_id': task_id, 'student_id': student_id})['params']
if params is None:
# params = {}
#else:
@@ -69,7 +89,7 @@ def results_post(task_id, results):
if param_meta.get('w', False) and param_name in user_params:
params[param_name] = user_params[param_name]
try:
- task_check_source = db.task_checkers.find_one({'task_id': task_id})['source']
+ task_check_source = db.task_checkers.find_one({'class_id': class_id, 'task_id': task_id})['source']
task_check_code = compile(task_check_source, 'checker.py', 'exec')
exec(task_check_code)
# raise Exception(str(params))
@@ -81,15 +101,16 @@ def results_post(task_id, results):
res_status = 'OK'
else:
res_status = 'NOT OK'
- db.results.insert({'task_id': task_id, 'result': res, 'hints': hints, 'status': res_status,'student_id': student_id, 'response': results, 'time': datetime.datetime.now()})
+ db.results.insert({'class_id': class_id, 'task_id': task_id, 'result': res, 'hints': hints, 'status': res_status,'student_id': student_id, 'response': results, 'time': datetime.datetime.now()})
return {'result': res, 'hints': hints, 'status': res_status}
-def results_dict(task_id):
+
+def results_dict(class_id, task_id):
student_id = flask.app.request.environ.get('REMOTE_USER', 'Nobody')
db = g.db
try:
entry = db.results.find_one(
- {'$query': {'task_id': task_id, 'student_id': student_id}, # vsi uporabniki brez nastavljenega REMOTE_USER (i.e. Apache basic auth) imajo skupne rezultate, napaka?
+ {'$query': {'class_id': class_id, 'task_id': task_id, 'student_id': student_id}, # vsi uporabniki brez nastavljenega REMOTE_USER (i.e. Apache basic auth) imajo skupne rezultate, napaka?
'$orderby': {'time': -1}},
{'result': 1, 'status': 1, 'hints': 1, '_id': 0})
if entry is None:
@@ -98,34 +119,38 @@ def results_dict(task_id):
except Exception as e:
return {'Error': str(e)}
-@app.route('/tasks/<task_id>/results.json', methods=['GET', 'POST'])
-def results_json(task_id):
+
+@app.route('/tasks/<class_id>/<task_id>/results.json', methods=['GET', 'POST'])
+def results_json(class_id, task_id):
if flask.app.request.method == 'POST':
- return json.dumps(results_post(task_id,
+ return json.dumps(results_post(class_id, task_id,
json.loads(flask.app.request.form['results'])))
- return json.dumps(results_dict(task_id))
+ return json.dumps(results_dict(class_id, task_id))
-@app.route('/tasks/<task_id>/<lang>/setup.<ending>', methods=['GET'])
-def setup_svg(task_id, lang, ending):
+
+@app.route('/tasks/<class_id>/<task_id>/<lang>/setup.<ending>', methods=['GET'])
+def setup_svg(class_id, task_id, lang, ending):
db = g.db
fmt, mimetype = {
'svg':('svg', 'image/svg+xml'),
'png':('png', 'image/png'),
}[ending]
- networks = list(db.networks.find({'task_id': task_id}))
- computers = list(db.computers_meta.find({'task_id': task_id}))
+ networks = list(db.networks.find({'class_id': class_id, 'task_id': task_id}))
+ computers = list(db.computers_meta.find({'class_id': class_id, 'task_id': task_id}))
return Response(draw_setup(computers, networks, format=fmt,
icon_path=app.config['STATIC_DIR']),
mimetype=mimetype)
-@app.route('/tasks/<task_id>/<lang>/results.html')
-def results_html(task_id, lang):
- return render_template('results.html', results=results_dict(task_id))
-def public_meta(db, task_id):
+@app.route('/tasks/<class_id>/<task_id>/<lang>/results.html')
+def results_html(class_id, task_id, lang):
+ return render_template('results.html', results=results_dict(class_id, task_id))
+
+
+def public_meta(db, class_id, task_id):
shown = {}
try:
- meta = db.task_params_meta.find_one({'task_id': task_id})['params']
+ meta = db.task_params_meta.find_one({'class_id': class_id, 'task_id': task_id})['params']
for k, v in meta.iteritems():
try:
if v['public']:
@@ -137,42 +162,44 @@ def public_meta(db, task_id):
return shown
-@app.route('/tasks/<task_id>/task.py')
-def task_source(task_id):
+@app.route('/tasks/<class_id>/<task_id>/task.py')
+def task_source(class_id, task_id):
db = g.db
try:
- task_str = db.tasks.find_one({'task_id': task_id})['source']
+ task_str = db.tasks.find_one({'class_id': class_id, 'task_id': task_id})['source']
except Exception:
return dummy_task
return task_str
-@app.route('/tasks/<task_id>/task.html')
-def task_html(task_id):
- return render_template('task.html', task=task_source(task_id))
-def get_params(task_id, student_id, db):
+@app.route('/tasks/<class_id>/<task_id>/task.html')
+def task_html(class_id, task_id):
+ return render_template('task.html', task=task_source(class_id, task_id))
+
+
+def get_params(class_id, task_id, student_id, db):
try:
- meta = db.task_params_meta.find_one({'task_id': task_id})['params']
+ meta = db.task_params_meta.find_one({'class_id': class_id, 'task_id': task_id})['params']
except Exception:
return {'mama': 'ZAKVAJ?'}, {'mama': {'public': True}}
- params = db.task_params.find_one({'task_id': task_id, 'student_id': student_id})
+ params = db.task_params.find_one({'class_id': class_id, 'task_id': task_id, 'student_id': student_id})
if params is None:
try:
- gen_params_source = db.gen_params.find_one({'task_id': task_id})['source']
+ gen_params_source = db.gen_params.find_one({'class_id': class_id, 'task_id': task_id})['source']
gen_params_code = compile(gen_params_source, 'generator.py', 'exec')
exec(gen_params_code)
params = gen_params(student_id, meta)
- db.task_params.update({'task_id': task_id, 'student_id': student_id},
+ db.task_params.update({'class_id': class_id, 'task_id': task_id, 'student_id': student_id},
{'$set': {'params': params}}, upsert=True)
params = gen_params(student_id, meta)
- for computer in db.computers_meta.find({'task_id': task_id}):
+ for computer in db.computers_meta.find({'class_id': class_id, 'task_id': task_id}):
try:
name = computer.pop('name')
del computer['_id']
del computer['task_id']
except Exception:
pass
- db.student_computers.update({'task_id': task_id, 'student_id': student_id, 'name': name},
+ db.student_computers.update({'class_id': class_id, 'task_id': task_id, 'student_id': student_id, 'name': name},
{'$set': computer}, upsert=True)
except Exception as e:
meta = {'crash': {'public': True}}
@@ -182,29 +209,33 @@ def get_params(task_id, student_id, db):
params = params['params']
return params, meta
-@app.route('/tasks/<task_id>/')
-def task_lang_redirect(task_id):
- return redirect(url_for('task_greeting', task_id=task_id, lang=app.config['DEFAULT_LANG']))
-@app.route('/tasks/<task_id>/<lang>/howto/')
-def task_howto(task_id, lang):
+@app.route('/tasks/<class_id>/<task_id>/')
+def task_lang_redirect(class_id, task_id):
+ return redirect(url_for('task_greeting', class_id=class_id, task_id=task_id, lang=app.config['DEFAULT_LANG']))
+
+
+@app.route('/tasks/<class_id>/<task_id>/<lang>/howto/')
+def task_howto(class_id, task_id, lang):
db = g.db
- return db.howtos.find({'task_id': task_id, 'lang': lang})[0].get('text', '')
+ return db.howtos.find_one({'class_id': class_id, 'task_id': task_id, 'lang': lang}).get('text', '')
-@app.route('/tasks/<task_id>/<lang>/images/<fname>')
-def task_image(task_id, lang, fname):
+
+@app.route('/tasks/<class_id>/<task_id>/<lang>/images/<fname>')
+def task_image(class_id, task_id, lang, fname):
db = g.db
- return db.howto_images.find({'task_id': task_id, 'fname': fname})[0].get('data', '')
+ return db.howto_images.find_one({'class_id': class_id, 'task_id': task_id, 'fname': fname}).get('data', '')
+
-@app.route('/tasks/<task_id>/<lang>/')
-def task_greeting(task_id, lang):
+@app.route('/tasks/<class_id>/<task_id>/<lang>/')
+def task_greeting(class_id, task_id, lang):
student_id = flask.app.request.environ.get('REMOTE_USER', 'Nobody')
db = g.db
# generate the parameters as soon as the student visits
- params, meta = get_params(task_id, student_id, db)
+ params, meta = get_params(class_id, task_id, student_id, db)
instr_ok = True
try:
- instructions = db.task_instructions.find({'task_id': task_id})[0]
+ instructions = db.task_instructions.find_one({'class_id': class_id, 'task_id': task_id})
instructions = instructions.get(lang, instructions[app.config['DEFAULT_LANG']])
except Exception:
try:
@@ -212,7 +243,6 @@ def task_greeting(task_id, lang):
except Exception as e:
instructions = str(e)
instr_ok = False
- instructions = instructions
if instr_ok:
try:
public_params = {}
@@ -225,24 +255,25 @@ def task_greeting(task_id, lang):
# instructions = instructions.format(**public_params).encode('utf8')
except Exception as e:
instructions = str(e)
- computer_list = db.student_computers.find({'task_id': task_id, 'student_id': student_id})
+ computer_list = db.student_computers.find({'class_id': class_id, 'task_id': task_id, 'student_id': student_id})
if request.args.get('narediStack', 'false') == 'true':
#db.student_tasks.update({'task_id': task_id, 'student_id': student_id}, {'$set': {'create_openstack': True}}, upsert = True)
openstackCreated = False # Spremeni na True, ko odkomentiras zgornjo vrstico.
else:
- if db.student_tasks.find({'task_id': task_id, 'student_id': student_id, 'openstack_created': True}).count() > 0:
+ if db.student_tasks.find({'class_id': class_id, 'task_id': task_id, 'student_id': student_id, 'openstack_created': True}).count() > 0:
openstackCreated = True
- elif db.student_tasks.find({'task_id': task_id, 'student_id': student_id, 'create_openstack': True}).count() > 0:
+ elif db.student_tasks.find({'class_id': class_id, 'task_id': task_id, 'student_id': student_id, 'create_openstack': True}).count() > 0:
openstackCreated = True
else:
openstackCreated = False
return render_template('task_greeting.html', computers=computer_list, lang=lang, openstack=openstackCreated, instructions=instructions)
-@app.route('/tasks/<task_id>/params.json', methods=['GET', 'POST'])
-def params_json(task_id):
+
+@app.route('/tasks/<class_id>/<task_id>/params.json', methods=['GET', 'POST'])
+def params_json(class_id, task_id):
student_id = flask.app.request.environ.get('REMOTE_USER', 'Nobody')
db = g.db
- params, meta = get_params(task_id, student_id, db)
+ params, meta = get_params(class_id, task_id, student_id, db)
shown_params = {}
if flask.app.request.method == 'POST':
try:
@@ -254,18 +285,19 @@ def params_json(task_id):
params[name] = new_params[name]
if meta.get(name, {'public': False})['public']:
shown_params[name] = params[name]
- db.task_params.update({'task_id': task_id, 'student_id': student_id}, {'$set': {'params': params}})
+ db.task_params.update({'class_id': class_id, 'task_id': task_id, 'student_id': student_id}, {'$set': {'params': params}})
else:
for name, param in params.iteritems():
if meta.get(name, {'public': False})['public']:
shown_params[name] = param
return json.dumps(shown_params)
-@app.route('/tasks/<task_id>/<lang>/params.html', methods=['GET', 'POST'])
-def params_html(task_id, lang):
+
+@app.route('/tasks/<class_id>/<task_id>/<lang>/params.html', methods=['GET', 'POST'])
+def params_html(class_id, task_id, lang):
student_id = flask.app.request.environ.get('REMOTE_USER', 'Nobody')
db = g.db
- params, meta = get_params(task_id, student_id, db)
+ params, meta = get_params(class_id, task_id, student_id, db)
shown_params = {}
for name, meta_param in meta.iteritems():
if meta_param.get('public', False):
@@ -278,18 +310,20 @@ def params_html(task_id, lang):
params[k] = flask.app.request.form[k]
if v.get('public', False):
shown_params[name] = params.get(name, '')
- db.task_params.update({'task_id': task_id, 'student_id': student_id}, {'$set': {'params': params}})
+ db.task_params.update({'class_id': class_id, 'task_id': task_id, 'student_id': student_id}, {'$set': {'params': params}})
return render_template('params.html', params=shown_params, params_meta=meta)
-@app.route('/tasks/<task_id>/params_meta.json')
-def params_meta(task_id):
+@app.route('/tasks/<class_id>/<task_id>/params_meta.json')
+def params_meta(class_id, task_id):
db = g.db
- return json.dumps(public_meta(db, task_id))
+ return json.dumps(public_meta(db, class_id, task_id))
+
@app.route('/static/<path:filename>')
def send_foo(filename):
return send_from_directory(app.config.STATIC_FILES, filename)
+
if __name__ == '__main__':
app.run(host='0.0.0.0')
diff --git a/kpov_judge/web/kpov_judge/templates/index.html b/kpov_judge/web/kpov_judge/templates/index.html
index 193f419..b5149ab 100644
--- a/kpov_judge/web/kpov_judge/templates/index.html
+++ b/kpov_judge/web/kpov_judge/templates/index.html
@@ -7,10 +7,10 @@ Zdravo, {{student_id}}
{{env}}
</p>
<p>
-Trenutno so na voljo naloge:
+Trenutno so na voljo predmeti:
<ul>
-{% for t in tasks %}
-<li><a href="tasks/{{t}}">{{t}}</a></li>
+{% for c in classes %}
+<li><a href="{{url_for('class_tasks', class_id=c.class_id)}}">{{c.name}}</a></li>
{% endfor %}
</ul>
</li>