Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for task statement in HTML and MD #1086

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"TOKEN_MODE_DISABLED", "TOKEN_MODE_FINITE", "TOKEN_MODE_INFINITE",
"TOKEN_MODE_MIXED",
"FEEDBACK_LEVEL_FULL", "FEEDBACK_LEVEL_RESTRICTED",
"STATEMENT_TYPE_PDF", "STATEMENT_TYPE_MD", "STATEMENT_TYPE_HTML",
# log
# Nothing intended for external use, no need to advertise anything.
# conf
Expand Down Expand Up @@ -69,6 +70,12 @@
# can be omitted).
FEEDBACK_LEVEL_RESTRICTED = "restricted"

# Statement types

STATEMENT_TYPE_PDF = "pdf"
STATEMENT_TYPE_MD = "md"
STATEMENT_TYPE_HTML = "html"


from .conf import Address, ServiceCoord, ConfigError, async_config, config
from .util import mkdir, rmtree, utf8_decoder, get_safe_shard, \
Expand Down
4 changes: 3 additions & 1 deletion cms/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"Admin",
# task
"Task", "Statement", "Attachment", "Dataset", "Manager", "Testcase",
"StatementAsset",
# submission
"Submission", "File", "Token", "SubmissionResult", "Executable",
"Evaluation",
Expand Down Expand Up @@ -97,7 +98,8 @@
from .admin import Admin
from .contest import Contest, Announcement
from .user import User, Team, Participation, Message, Question
from .task import Task, Statement, Attachment, Dataset, Manager, Testcase
from .task import Task, Statement, Attachment, Dataset, Manager, Testcase, \
StatementAsset
from .submission import Submission, File, Token, SubmissionResult, \
Executable, Evaluation
from .usertest import UserTest, UserTestFile, UserTestManager, \
Expand Down
62 changes: 59 additions & 3 deletions cms/db/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Copyright © 2010-2012 Matteo Boscariol <[email protected]>
# Copyright © 2012-2018 Luca Wehrstedt <[email protected]>
# Copyright © 2013 Bernard Blackham <[email protected]>
# Copyright © 2018 William Di Luigi <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -36,7 +37,8 @@
Interval, Enum, BigInteger

from cms import TOKEN_MODE_DISABLED, TOKEN_MODE_FINITE, TOKEN_MODE_INFINITE, \
FEEDBACK_LEVEL_FULL, FEEDBACK_LEVEL_RESTRICTED
FEEDBACK_LEVEL_FULL, FEEDBACK_LEVEL_RESTRICTED, STATEMENT_TYPE_PDF, \
STATEMENT_TYPE_MD, STATEMENT_TYPE_HTML
from cmscommon.constants import \
SCORE_MODE_MAX, SCORE_MODE_MAX_SUBTASK, SCORE_MODE_MAX_TOKENED_LAST
from . import Codename, Filename, FilenameSchemaArray, Digest, Base, Contest
Expand Down Expand Up @@ -232,7 +234,14 @@ class Task(Base):

statements = relationship(
"Statement",
collection_class=attribute_mapped_collection("language"),
collection_class=attribute_mapped_collection("statement_key"),
cascade="all, delete-orphan",
passive_deletes=True,
back_populates="task")

statement_assets = relationship(
"StatementAsset",
collection_class=attribute_mapped_collection("filename"),
cascade="all, delete-orphan",
passive_deletes=True,
back_populates="task")
Expand Down Expand Up @@ -272,7 +281,7 @@ class Statement(Base):
"""
__tablename__ = 'statements'
__table_args__ = (
UniqueConstraint('task_id', 'language'),
UniqueConstraint('task_id', 'language', 'statement_type'),
)

# Auto increment primary key.
Expand Down Expand Up @@ -300,11 +309,58 @@ class Statement(Base):
Unicode,
nullable=False)

# The type of statement: this information is used by web servers to decide
# how to render the statement.
statement_type = Column(
Enum(STATEMENT_TYPE_PDF, STATEMENT_TYPE_MD, STATEMENT_TYPE_HTML,
name="statement_type"),
nullable=False)

# Digest of the file.
digest = Column(
Digest,
nullable=False)

@property
def statement_key(self):
return (self.language, self.statement_type)


class StatementAsset(Base):
"""Class to store statement assets that are supposed to be accessible from
the statement page in a way that makes it possible to link them from the
statement itself.

"""
__tablename__ = 'statement_assets'
__table_args__ = (
UniqueConstraint('task_id', 'filename'),
)

# Auto increment primary key.
id = Column(
Integer,
primary_key=True)

# Task (id and object) owning the asset.
task_id = Column(
Integer,
ForeignKey(Task.id,
onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
index=True)
task = relationship(
Task,
back_populates="statement_assets")

# Filename and digest of the provided asset.
filename = Column(
Filename,
nullable=False)
digest = Column(
Digest,
nullable=False)


class Attachment(Base):
"""Class to store additional files to give to the user together
Expand Down
5 changes: 5 additions & 0 deletions cms/server/admin/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Copyright © 2016 Myungwoo Chun <[email protected]>
# Copyright © 2016 Peyman Jabbarzade Ganje <[email protected]>
# Copyright © 2016 Amir Keivan Mohtashami <[email protected]>
# Copyright © 2018 William Di Luigi <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -87,6 +88,8 @@
StatementHandler, \
AddAttachmentHandler, \
AttachmentHandler, \
AddStatementAssetHandler, \
StatementAssetHandler, \
TaskListHandler, \
RemoveTaskHandler
from .user import \
Expand Down Expand Up @@ -171,6 +174,8 @@
(r"/task/([0-9]+)/statement/([0-9]+)", StatementHandler),
(r"/task/([0-9]+)/attachments/add", AddAttachmentHandler),
(r"/task/([0-9]+)/attachment/([0-9]+)", AttachmentHandler),
(r"/task/([0-9]+)/assets/add", AddStatementAssetHandler),
(r"/task/([0-9]+)/asset/([0-9]+)", StatementAssetHandler),

# Datasets

Expand Down
77 changes: 75 additions & 2 deletions cms/server/admin/handlers/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Copyright © 2014 Artem Iglikov <[email protected]>
# Copyright © 2014 Fabian Gundlach <[email protected]>
# Copyright © 2016 Myungwoo Chun <[email protected]>
# Copyright © 2018 William Di Luigi <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand All @@ -31,7 +32,8 @@

import tornado.web

from cms.db import Attachment, Dataset, Session, Statement, Submission, Task
from cms.db import Attachment, Dataset, Session, Statement, Submission, \
Task, StatementAsset
from cmscommon.datetime import make_datetime
from .base import BaseHandler, SimpleHandler, require_permission

Expand Down Expand Up @@ -130,7 +132,7 @@ def post(self, task_id):
primary_statements = {}
for statement in task.statements:
self.get_bool(primary_statements,
"primary_statement_%s" % statement)
"primary_statement_%s" % statement[0])
attrs["primary_statements"] = list(sorted([
k.replace("primary_statement_", "", 1)
for k in primary_statements
Expand Down Expand Up @@ -161,6 +163,7 @@ def post(self, task_id):
task.set_attrs(attrs)

except Exception as error:
logger.warning("Invalid field: %s" % (traceback.format_exc()))
self.service.add_notification(
make_datetime(), "Invalid field(s)", repr(error))
self.redirect(self.url("task", task_id))
Expand Down Expand Up @@ -295,6 +298,76 @@ def delete(self, task_id, statement_id):
self.write("%s" % task.id)


class AddStatementAssetHandler(BaseHandler):
"""Add an asset to a task.

"""
@require_permission(BaseHandler.PERMISSION_ALL)
def get(self, task_id):
task = self.safe_get_item(Task, task_id)
self.contest = task.contest

self.r_params = self.render_params()
self.r_params["task"] = task
self.render("add_asset.html", **self.r_params)

@require_permission(BaseHandler.PERMISSION_ALL)
def post(self, task_id):
fallback_page = self.url("task", task_id, "assets", "add")

task = self.safe_get_item(Task, task_id)

asset = self.request.files["asset"][0]
task_name = task.name
self.sql_session.close()

try:
digest = self.service.file_cacher.put_file_content(
asset["body"],
"Task statement asset for %s" % task_name)
except Exception as error:
self.service.add_notification(
make_datetime(),
"StatementAsset storage failed",
repr(error))
self.redirect(fallback_page)
return

# TODO verify that there's no other StatementAsset with that filename
# otherwise we'd trigger an IntegrityError for constraint violation

self.sql_session = Session()
task = self.safe_get_item(Task, task_id)

asset = StatementAsset(asset["filename"], digest, task=task)
self.sql_session.add(asset)

if self.try_commit():
self.redirect(self.url("task", task_id))
else:
self.redirect(fallback_page)


class StatementAssetHandler(BaseHandler):
"""Delete an asset.

"""
@require_permission(BaseHandler.PERMISSION_ALL)
def delete(self, task_id, asset_id):
asset = self.safe_get_item(StatementAsset, asset_id)
task = self.safe_get_item(Task, task_id)

# Protect against URLs providing incompatible parameters.
if asset.task is not task:
raise tornado.web.HTTPError(404)

self.sql_session.delete(asset)
self.try_commit()

# Page to redirect to.
self.write("%s" % task.id)


class AddAttachmentHandler(BaseHandler):
"""Add an attachment to a task.

Expand Down
13 changes: 13 additions & 0 deletions cms/server/admin/templates/add_asset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base.html" %}

{% block core %}
<div class="core_title">
<h1><a href="{{ url("task", task.id) }}">{{ task.title }} ({{ task.name }})</a> - Upload statement asset</h1>
</div>
<form enctype="multipart/form-data" action="{{ url("task", task.id, "assets", "add") }}" method="POST">
{{ xsrf_form_html|safe }}
<input type="file" name="asset"/><br/>
<input type="submit" value="Upload">
<input type="Reset" >
</form>
{% endblock core %}
27 changes: 26 additions & 1 deletion cms/server/admin/templates/task.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ <h2 id="title_task_configuration" class="toggling_on">Task configuration</h2>
<tbody>
{% for statement in task.statements.values() %}
<tr>
<td><a href="{{ url("file", statement.digest, "statement.pdf") }}">Statement for language "{{ statement.language }}"</a></td>
<td>
<a href="{{ url("file", statement.digest, "statement.pdf") }}">
{{ statement.statement_type.upper() }} Statement for language "{{ statement.language }}"
</a>
</td>
<td>
<label>
<input type="checkbox" name="primary_statement_{{ statement.language }}" {% if statement.language in primary_statements %}checked{% endif %} />
Expand All @@ -99,6 +103,27 @@ <h2 id="title_task_configuration" class="toggling_on">Task configuration</h2>
{% endif %}
</td>
</tr>
<tr>
<td>
<span class="info" title="Statement assets for this task. They are files which are accessible from the task page and possibly required by HTML statements or similar."></span>
Statement assets
{% if admin.permission_all %}
[<a href="{{ url("task", task.id, "assets", "add") }}">add</a>]
{% endif %}
</td>
<td>
{% if task.statement_assets|length == 0 %}
No statement assets.
{% else %}
{% for asset in task.statement_assets.values() %}
<div class="attachment">
<a href="{{ url("file", asset.digest, asset.filename) }}">{{ asset.filename }}</a>
- <a onclick="CMS.AWSUtils.ajax_delete('{{ url("task", task.id, "asset", asset.id) }}'); ">Delete</a>
</div>
{% endfor %}
{% endif %}
</td>
</tr>
<tr>
<td>
<span class="info" title="Attachments for this task. Contestant can view and download attachments from the task page."></span>
Expand Down
8 changes: 5 additions & 3 deletions cms/server/contest/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Copyright © 2013 Bernard Blackham <[email protected]>
# Copyright © 2014 Artem Iglikov <[email protected]>
# Copyright © 2014 Fabian Gundlach <[email protected]>
# Copyright © 2015-2016 William Di Luigi <[email protected]>
# Copyright © 2015-2018 William Di Luigi <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -36,7 +36,8 @@
from .task import \
TaskDescriptionHandler, \
TaskStatementViewHandler, \
TaskAttachmentViewHandler
TaskAttachmentViewHandler, \
TaskAssetViewHandler
from .tasksubmission import \
SubmitHandler, \
TaskSubmissionsHandler, \
Expand Down Expand Up @@ -67,8 +68,9 @@
# Tasks

(r"/tasks/(.*)/description", TaskDescriptionHandler),
(r"/tasks/(.*)/statements/(.*)", TaskStatementViewHandler),
(r"/tasks/(.*)/statements/(.*)/(.*)", TaskStatementViewHandler),
(r"/tasks/(.*)/attachments/(.*)", TaskAttachmentViewHandler),
(r"/tasks/(.*)/assets/(.*)", TaskAssetViewHandler),

# Task submissions

Expand Down
Loading