-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
307 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file is part of the QuestionPy Server. (https://questionpy.org) | ||
# The QuestionPy Server is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
from ._attempts import attempt_routes | ||
from ._files import file_routes | ||
from ._packages import package_routes | ||
from ._status import status_routes | ||
|
||
routes = (*attempt_routes, *file_routes, *package_routes, *status_routes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# This file is part of the QuestionPy Server. (https://questionpy.org) | ||
# The QuestionPy Server is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from aiohttp import web | ||
|
||
from questionpy_common.environment import RequestUser | ||
from questionpy_server.api.models import AttemptScoreArguments, AttemptStartArguments, AttemptViewArguments | ||
from questionpy_server.decorators import ensure_package_and_question_state_exist | ||
from questionpy_server.package import Package | ||
from questionpy_server.web import json_response | ||
from questionpy_server.worker.runtime.package_location import ZipPackageLocation | ||
|
||
if TYPE_CHECKING: | ||
from questionpy_server.app import QPyServer | ||
from questionpy_server.worker.worker import Worker | ||
|
||
|
||
attempt_routes = web.RouteTableDef() | ||
|
||
|
||
@attempt_routes.post(r"/packages/{package_hash:\w+}/attempt/start") # type: ignore[arg-type] | ||
@ensure_package_and_question_state_exist | ||
async def post_attempt_start( | ||
request: web.Request, package: Package, question_state: bytes, data: AttemptStartArguments | ||
) -> web.Response: | ||
qpyserver: "QPyServer" = request.app["qpy_server_app"] | ||
|
||
package_path = await package.get_path() | ||
worker: Worker | ||
async with qpyserver.worker_pool.get_worker(ZipPackageLocation(package_path), 0, data.context) as worker: | ||
attempt = await worker.start_attempt(RequestUser(["de", "en"]), question_state.decode(), data.variant) | ||
|
||
return json_response(data=attempt, status=201) | ||
|
||
|
||
@attempt_routes.post(r"/packages/{package_hash:\w+}/attempt/view") # type: ignore[arg-type] | ||
@ensure_package_and_question_state_exist | ||
async def post_attempt_view( | ||
request: web.Request, package: Package, question_state: bytes, data: AttemptViewArguments | ||
) -> web.Response: | ||
qpyserver: "QPyServer" = request.app["qpy_server_app"] | ||
|
||
package_path = await package.get_path() | ||
worker: Worker | ||
async with qpyserver.worker_pool.get_worker(ZipPackageLocation(package_path), 0, data.context) as worker: | ||
attempt = await worker.get_attempt( | ||
request_user=RequestUser(["de", "en"]), | ||
question_state=question_state.decode(), | ||
attempt_state=data.attempt_state, | ||
scoring_state=data.scoring_state, | ||
response=data.response, | ||
) | ||
|
||
return json_response(data=attempt, status=201) | ||
|
||
|
||
@attempt_routes.post(r"/packages/{package_hash:\w+}/attempt/score") # type: ignore[arg-type] | ||
@ensure_package_and_question_state_exist | ||
async def post_attempt_score( | ||
request: web.Request, package: Package, question_state: bytes, data: AttemptScoreArguments | ||
) -> web.Response: | ||
qpyserver: "QPyServer" = request.app["qpy_server_app"] | ||
|
||
package_path = await package.get_path() | ||
worker: Worker | ||
async with qpyserver.worker_pool.get_worker(ZipPackageLocation(package_path), 0, data.context) as worker: | ||
attempt_scored = await worker.score_attempt( | ||
request_user=RequestUser(["de", "en"]), | ||
question_state=question_state.decode(), | ||
attempt_state=data.attempt_state, | ||
scoring_state=data.scoring_state, | ||
response=data.response, | ||
) | ||
|
||
return json_response(data=attempt_scored, status=201) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# This file is part of the QuestionPy Server. (https://questionpy.org) | ||
# The QuestionPy Server is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
from typing import TYPE_CHECKING | ||
|
||
from aiohttp import web | ||
|
||
from questionpy_server.decorators import ensure_package_and_question_state_exist | ||
from questionpy_server.package import Package | ||
from questionpy_server.worker.runtime.package_location import ZipPackageLocation | ||
|
||
if TYPE_CHECKING: | ||
from questionpy_server.app import QPyServer | ||
from questionpy_server.worker.worker import Worker | ||
|
||
file_routes = web.RouteTableDef() | ||
|
||
|
||
@file_routes.post(r"/packages/{package_hash}/file/{namespace}/{short_name}/{path:static/.*}") # type: ignore[arg-type] | ||
@ensure_package_and_question_state_exist | ||
async def post_attempt_start(request: web.Request, package: Package) -> web.Response: | ||
qpy_server: "QPyServer" = request.app["qpy_server_app"] | ||
path = request.match_info["path"] | ||
|
||
worker: Worker | ||
async with qpy_server.worker_pool.get_worker(ZipPackageLocation(await package.get_path()), 0, None) as worker: | ||
try: | ||
# TODO: Support static files in non-main packages by using namespace and short_name. | ||
file = await worker.get_static_file(path) | ||
except FileNotFoundError: | ||
return web.HTTPNotFound(reason="File not found.") | ||
|
||
return web.Response( | ||
body=file.data, content_type=file.mime_type, | ||
# Set a lifetime of 1 year, i.e. effectively never expire. Since the package hash is part of the URL, cache | ||
# busting is automatic. | ||
headers={"Cache-Control": "public, immutable, max-age=31536000"} | ||
) |
Oops, something went wrong.