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

♻️ webserver: fixes mypy issues in studies_dispatcher plugin #4188

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import uuid
from collections import deque
from functools import lru_cache
from typing import Optional

from aiohttp import web
from pydantic import ValidationError
Expand All @@ -29,13 +28,13 @@ def compose_uuid_from(*values) -> uuid.UUID:


async def list_viewers_info(
app: web.Application, file_type: Optional[str] = None, *, only_default: bool = False
app: web.Application, file_type: str | None = None, *, only_default: bool = False
) -> list[ViewerInfo]:
#
# TODO: These services MUST be shared with EVERYBODY! Setup check on startup and fill
# with !?
#
consumers = deque()
consumers: deque = deque()

async with app[APP_DB_ENGINE_KEY].acquire() as conn:

Expand Down Expand Up @@ -71,7 +70,7 @@ async def list_viewers_info(
async def get_default_viewer(
app: web.Application,
file_type: str,
file_size: Optional[int] = None,
file_size: int | None = None,
) -> ViewerInfo:
try:
viewers = await list_viewers_info(app, file_type, only_default=True)
Expand All @@ -93,9 +92,9 @@ async def get_default_viewer(
async def validate_requested_viewer(
app: web.Application,
file_type: str,
file_size: Optional[int] = None,
service_key: Optional[str] = None,
service_version: Optional[str] = None,
file_size: int | None = None,
service_key: str | None = None,
service_version: str | None = None,
) -> ViewerInfo:

if not service_key and not service_version:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import uuid
from typing import cast

from aiopg.sa.result import RowProxy
from models_library.services import ServiceKey, ServiceVersion
Expand All @@ -18,7 +19,9 @@ class ServiceInfo(BaseModel):

label: str = Field(..., description="Display name")

thumbnail: HttpUrl = Field(default="https://via.placeholder.com/170x120.png")
thumbnail: HttpUrl = Field(
default=cast(HttpUrl, "https://via.placeholder.com/170x120.png")
matusdrobuliak66 marked this conversation as resolved.
Show resolved Hide resolved
)

is_guest_allowed: bool = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def _create_project(
name=name,
description=description,
thumbnail=thumbnail,
prjOwner=owner.email, # type: ignore
prjOwner=owner.email,
accessRights={owner.primary_gid: access_rights},
creationDate=now_str(),
lastChangeDate=now_str(),
Expand Down Expand Up @@ -161,11 +161,11 @@ def _create_project_with_filepicker_and_service(
description=f"Autogenerated study with file-picker and service {viewer_info.footprint}",
thumbnail=viewer_info.thumbnail,
owner=owner,
workbench={ # type: ignore
workbench={
f"{file_picker_id}": file_picker,
f"{viewer_id}": viewer_service,
},
workbench_ui={ # type: ignore
workbench_ui={
f"{file_picker_id}": {"position": {"x": 305, "y": 229}},
f"{viewer_id}": {"position": {"x": 633, "y": 229}},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async def get_authorized_user(request: web.Request) -> dict:

db: AsyncpgStorage = get_plugin_storage(request.app)
userid = await authorized_userid(request)
user = await db.get_user({"id": userid})
user: dict = await db.get_user({"id": userid})
return user


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
from ..utils_aiohttp import create_redirect_response
from ._catalog import validate_requested_service
from ._constants import MSG_UNEXPECTED_ERROR
from ._core import StudyDispatcherError, ViewerInfo, validate_requested_viewer
from ._models import ServiceInfo
from ._core import validate_requested_viewer
from ._exceptions import StudyDispatcherError
from ._models import ServiceInfo, ViewerInfo
from ._projects import acquire_project_with_service, acquire_project_with_viewer
from ._users import UserInfo, acquire_user, ensure_authentication

Expand Down Expand Up @@ -117,7 +118,7 @@ def compose_service_dispatcher_prefix_url(
request: web.Request, service_key: str, service_version: str
) -> HttpUrl:
params = ViewerQueryParams(
viewer_key=service_key, viewer_version=service_version # type: ignore
viewer_key=service_key, viewer_version=service_version
).dict(exclude_none=True, exclude_unset=True)
absolute_url = request.url.join(
request.app.router["get_redirection_to_viewer"].url_for().with_query(**params)
Expand Down Expand Up @@ -188,7 +189,7 @@ async def get_redirection_to_viewer(request: web.Request):
params = parse_request_query_parameters_as(RedirectionQueryParams, request)

logger.debug("Requesting viewer %s", params)

user: UserInfo
if params.file_type and params.download_link:
# TODO: Cannot check file_size from HEAD
# removed await params.check_download_link()
Expand All @@ -203,9 +204,7 @@ async def get_redirection_to_viewer(request: web.Request):
logger.debug("Validated viewer %s", viewer)

# Retrieve user or create a temporary guest
user: UserInfo = await acquire_user(
request, is_guest_allowed=viewer.is_guest_allowed
)
user = await acquire_user(request, is_guest_allowed=viewer.is_guest_allowed)
logger.debug("User acquired %s", user)

# Generate one project per user + download_link + viewer
Expand Down Expand Up @@ -242,17 +241,15 @@ async def get_redirection_to_viewer(request: web.Request):
logger.debug("Validated service %s", valid_service)

# Retrieve user or create a temporary guest
user: UserInfo = await acquire_user(
request, is_guest_allowed=valid_service.is_public
)
user = await acquire_user(request, is_guest_allowed=valid_service.is_public)
logger.debug("User acquired %s", user)

project_id, viewer_id = await acquire_project_with_service(
request.app,
user,
service_info=ServiceInfo(
key=valid_service.key, # type: ignore
version=valid_service.version, # type: ignore
key=valid_service.key,
version=valid_service.version,
label=valid_service.title,
),
product_name=get_product_name(request),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta

from aiohttp import web
from pydantic import HttpUrl, validator
from pydantic import HttpUrl, parse_obj_as, validator
from pydantic.fields import Field
from servicelib.aiohttp.application_keys import APP_SETTINGS_KEY
from settings_library.base import BaseCustomSettings
Expand All @@ -21,7 +21,7 @@ class StudiesDispatcherSettings(BaseCustomSettings):
)

STUDIES_DEFAULT_SERVICE_THUMBNAIL: HttpUrl = Field(
default="https://via.placeholder.com/170x120.png",
default=parse_obj_as(HttpUrl, "https://via.placeholder.com/170x120.png"),
matusdrobuliak66 marked this conversation as resolved.
Show resolved Hide resolved
description="Default servcie thumbnails in the service response",
)

Expand Down