Skip to content

Commit

Permalink
wip: refactor ensure_package_and_question_state_exist
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Aug 20, 2024
1 parent 3fd5485 commit 47b871a
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 195 deletions.
4 changes: 2 additions & 2 deletions questionpy_server/api/routes/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aiohttp import web
from aiohttp.web_exceptions import HTTPNotImplemented

from questionpy_server.decorators import ensure_package_and_question_state_exist
from questionpy_server.decorators import ensure_package
from questionpy_server.package import Package
from questionpy_server.worker.runtime.package_location import ZipPackageLocation

Expand All @@ -18,7 +18,7 @@


@file_routes.post(r"/packages/{package_hash}/file/{namespace}/{short_name}/{path:static/.*}") # type: ignore[arg-type]
@ensure_package_and_question_state_exist
@ensure_package
async def post_attempt_start(request: web.Request, package: Package) -> web.Response:
qpy_server: QPyServer = request.app["qpy_server_app"]
namespace = request.match_info["namespace"]
Expand Down
4 changes: 2 additions & 2 deletions questionpy_server/api/routes/_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from questionpy_common.environment import RequestUser
from questionpy_server.api.models import QuestionCreateArguments, QuestionEditFormResponse, RequestBaseData
from questionpy_server.decorators import ensure_package_and_question_state_exist
from questionpy_server.decorators import ensure_package, 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
Expand Down Expand Up @@ -84,7 +84,7 @@ async def post_question_migrate(_request: web.Request) -> web.Response:


@package_routes.post(r"/package-extract-info") # type: ignore[arg-type]
@ensure_package_and_question_state_exist
@ensure_package
async def package_extract_info(_request: web.Request, package: Package) -> web.Response:
"""Get package information."""
return json_response(data=package.get_info(), status=201)
18 changes: 9 additions & 9 deletions questionpy_server/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ class File(NamedTuple):
size: int


class SizeError(Exception):
def __init__(self, message: str = "", max_size: int = 0, actual_size: int = 0):
super().__init__(message)
class CacheItemTooLargeError(Exception):
def __init__(self, key: str, actual_size: int, max_size: int):
readable_actual = ByteSize(actual_size).human_readable()
readable_max = ByteSize(max_size).human_readable()
super().__init__(
f"Unable to cache item '{key}' with size '{readable_actual}' because it exceeds the maximum "
f"allowed size of '{readable_max}'"
)

self.max_size = max_size
self.actual_size = actual_size
Expand Down Expand Up @@ -146,12 +151,7 @@ async def put(self, key: str, value: bytes) -> Path:
if size > self.max_size:
# If we allowed this, the loop at the end would remove all items from the dictionary,
# so we raise an error to allow exceptions for this case.
msg = f"Item itself exceeds maximum allowed size of {ByteSize(self.max_size).human_readable()}"
raise SizeError(
msg,
max_size=self.max_size,
actual_size=size,
)
raise CacheItemTooLargeError(key, size, self.max_size)

async with self._lock:
# Save the bytes on filesystem.
Expand Down
3 changes: 3 additions & 0 deletions questionpy_server/collector/package_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def get(self, package_hash: str) -> "Package":
Returns:
path to the package
Raises:
FileNotFoundError: if no package with the given hash exists
"""
# Check if package was indexed
if package := self._indexer.get_by_hash(package_hash):
Expand Down
Loading

0 comments on commit 47b871a

Please sign in to comment.