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 @@ -36,7 +36,7 @@ async def list_viewers_info(
# 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from aiopg.sa.result import RowProxy
from models_library.services import ServiceKey, ServiceVersion
from pydantic import BaseModel, Field, HttpUrl, PositiveInt
from pydantic import BaseModel, Field, HttpUrl, PositiveInt, parse_obj_as


class ServiceInfo(BaseModel):
Expand All @@ -9,7 +9,9 @@ class ServiceInfo(BaseModel):

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

thumbnail: HttpUrl = Field(default="https://via.placeholder.com/170x120.png")
thumbnail: HttpUrl = Field(
default=parse_obj_as(HttpUrl, "https://via.placeholder.com/170x120.png")
)

is_guest_allowed: bool = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _create_file_picker(download_link: str):
inputNodes=[],
outputs={
# NOTE: Empty label checked with @odeimaiz
output_id: DownloadLink( # type: ignore
output_id: DownloadLink(
downloadLink=parse_obj_as(AnyUrl, download_link),
label="",
)
Expand Down Expand Up @@ -90,13 +90,13 @@ def _create_project(
uuid=project_id,
name=name,
description=description,
thumbnail=thumbnail, # type: ignore
prjOwner=owner.email, # type: ignore
accessRights={owner.primary_gid: access_rights}, # type: ignore
creationDate=now_str(), # type: ignore
lastChangeDate=now_str(), # type: ignore
workbench=workbench, # type: ignore
ui=StudyUI(workbench=workbench_ui), # type: ignore
thumbnail=thumbnail,
prjOwner=owner.email,
accessRights={owner.primary_gid: access_rights},
creationDate=now_str(),
lastChangeDate=now_str(),
workbench=workbench,
ui=StudyUI(workbench=workbench_ui),
)

return project
Expand Down Expand Up @@ -149,7 +149,7 @@ def _create_project_with_filepicker_and_service(
version=viewer_info.version,
label=viewer_info.label,
inputs={
viewer_info.input_port_key: PortLink( # type: ignore
viewer_info.input_port_key: PortLink(
nodeUuid=file_picker_id,
output=file_picker_output_id,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
from ..utils_aiohttp import create_redirect_response
from ._catalog import ValidService, validate_requested_service
from ._constants import MSG_UNEXPECTED_ERROR
from ._core import ViewerInfo, validate_requested_file, validate_requested_viewer
from ._core import validate_requested_file, validate_requested_viewer
from ._errors import InvalidRedirectionParams, StudyDispatcherError
from ._models import FileParams, ServiceInfo, ServiceParams
from ._models import FileParams, ServiceInfo, ServiceParams, ViewerInfo
from ._projects import (
get_or_create_project_with_file,
get_or_create_project_with_file_and_service,
Expand Down Expand Up @@ -77,7 +77,7 @@ def _create_service_info_from(service: ValidService) -> ServiceInfo:
)
if service.thumbnail:
values_map["thumbnail"] = service.thumbnail
return ServiceInfo.construct(_fields_set=set(values_map.keys()), **values_map)
return ServiceInfo.construct(_fields_set=set(values_map.keys()), **values_map) # type: ignore


def _handle_errors_with_error_page(handler: Handler):
Expand Down Expand Up @@ -220,9 +220,12 @@ async def get_redirection_to_viewer(request: web.Request):

NOTE: Can be set as login_required programatically with STUDIES_ACCESS_ANONYMOUS_ALLOWED env var.
"""
query_params = parse_request_query_parameters_as(RedirectionQueryParams, request)
query_params: RedirectionQueryParams = parse_request_query_parameters_as(
RedirectionQueryParams, request
)
_logger.debug("Requesting viewer %s [%s]", query_params, type(query_params))

user: UserInfo
if isinstance(query_params, ServiceAndFileParams):
file_params = service_params = query_params

Expand All @@ -236,7 +239,7 @@ async def get_redirection_to_viewer(request: web.Request):
)

# Retrieve user or create a temporary guest
user: UserInfo = await get_or_create_user(
user = await get_or_create_user(
request, is_guest_allowed=viewer.is_guest_allowed
)

Expand All @@ -258,15 +261,15 @@ async def get_redirection_to_viewer(request: web.Request):
)

elif isinstance(query_params, ServiceQueryParams):
service_params = query_params
service_params_ = query_params

valid_service: ValidService = await validate_requested_service(
app=request.app,
service_key=service_params.viewer_key,
service_version=service_params.viewer_version,
service_key=service_params_.viewer_key,
service_version=service_params_.viewer_version,
)

user: UserInfo = await get_or_create_user(
user = await get_or_create_user(
request, is_guest_allowed=valid_service.is_public
)

Expand All @@ -286,20 +289,20 @@ async def get_redirection_to_viewer(request: web.Request):
)

elif isinstance(query_params, FileQueryParams):
file_params = query_params
file_params_ = query_params

validate_requested_file(
app=request.app,
file_type=file_params.file_type,
file_size=file_params.file_size,
file_type=file_params_.file_type,
file_size=file_params_.file_size,
)

user: UserInfo = await get_or_create_user(request, is_guest_allowed=True)
user = await get_or_create_user(request, is_guest_allowed=True)

project_id, file_picker_id = await get_or_create_project_with_file(
request.app,
user,
file_params=file_params,
file_params=file_params_,
project_thumbnail=get_plugin_settings(
app=request.app
).STUDIES_DEFAULT_FILE_THUMBNAIL,
Expand All @@ -310,8 +313,8 @@ async def get_redirection_to_viewer(request: web.Request):
request.app,
project_id=project_id,
viewer_node_id=file_picker_id, # TODO: ask odei about this?
file_name=file_params.file_name,
file_size=file_params.file_size,
file_name=file_params_.file_name,
file_size=file_params_.file_size,
)

else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ def _compose_file_and_service_dispatcher_prefix_url(
absolute_url = request.url.join(
request.app.router["get_redirection_to_viewer"].url_for().with_query(**params)
)
return parse_obj_as(HttpUrl, f"{absolute_url}")
absolute_url_: HttpUrl = parse_obj_as(HttpUrl, f"{absolute_url}")
return absolute_url_


def _compose_service_only_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)
)
return parse_obj_as(HttpUrl, f"{absolute_url}")
absolute_url_: HttpUrl = parse_obj_as(HttpUrl, f"{absolute_url}")
return absolute_url_


#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,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 @@ -21,12 +21,12 @@ 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 thumbnail for services or dispatch project with a service",
)

STUDIES_DEFAULT_FILE_THUMBNAIL: HttpUrl = Field(
default="https://via.placeholder.com/170x120.png",
default=parse_obj_as(HttpUrl, "https://via.placeholder.com/170x120.png"),
description="Default thumbnail for dispatch projects with only data (i.e. file-picker)",
)

Expand Down