Skip to content

Commit

Permalink
ruff setup
Browse files Browse the repository at this point in the history
  • Loading branch information
depocoder committed Sep 24, 2024
1 parent 05b2118 commit 52406a6
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 19 deletions.
8 changes: 6 additions & 2 deletions backend/app/controllers/modeus.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ async def get_modeus_cookies(self, item: FromJson[models.ModeusCreds]) -> Respon

@post("/events/")
async def get_modeus_events(
self, auth: FromAuthorizationHeader, item: FromJson[models.ModeusSearchEvents]
self,
auth: FromAuthorizationHeader,
item: FromJson[models.ModeusSearchEvents],
) -> Response:
"""
Get events from Modeus.
Expand All @@ -54,6 +56,8 @@ async def get_modeus_events(
jwt = auth.value.split()[1]
return self.json(await modeus.get_events(jwt, item.value))
except IndexError as exception:
return self.json({"error": f"cannot parse authorization header {exception}"})
return self.json(
{"error": f"cannot parse authorization header {exception}"},
)
except (RequestException, ModeusError) as exception:
return self.json({"error": f"can't authenticate {exception}"}, status=400)
3 changes: 2 additions & 1 deletion backend/app/controllers/netology.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def class_name(cls) -> str:

@post()
async def get_netology_cookies(
self, item: FromJson[models.NetologyCreds]
self,
item: FromJson[models.NetologyCreds],
) -> Response:
"""
Auth in Netology and return cookies.
Expand Down
6 changes: 4 additions & 2 deletions backend/app/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

def configure_error_handlers(app: Application) -> None:
async def not_found_handler(
app: Application, request: Request, exception: Exception
app: Application,
request: Request,
exception: Exception,
) -> Response:
return text(str(exception) or "Not found", 404)

Expand All @@ -37,5 +39,5 @@ async def accepted(*args: Any) -> Response:
UnauthorizedException: unauthorized,
ForbiddenException: forbidden,
AcceptedException: accepted,
}
},
)
2 changes: 1 addition & 1 deletion backend/app/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def get_copy(settings: Settings) -> str:
"""Get copy."""
now = datetime.now()
return "{0} {1}".format(now.year, settings.site.copyright)
return f"{now.year} {settings.site.copyright}"


def configure_templating(application: Application, settings: Settings) -> None:
Expand Down
29 changes: 19 additions & 10 deletions backend/integration/modeus.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""Modeus API implementation."""

from __future__ import annotations

import re
from secrets import token_hex
from typing import Any, Dict
from typing import Any

import httpx
from bs4 import BeautifulSoup, Tag
Expand Down Expand Up @@ -73,16 +71,21 @@ async def get_auth_form(session: AsyncClient, username: str, password: str) -> T
return form


async def login(username: str, __password: str, timeout: int = 15) -> Dict[str, Any]:
async def login(username: str, __password: str, timeout: int = 15) -> dict[str, Any]:
"""
Log in Modeus.
Raises:
CannotAuthenticateError: if something changed in API
"""
async with httpx.AsyncClient(base_url="https://utmn.modeus.org/", timeout=timeout, headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0',
}, follow_redirects=True,) as session:
async with httpx.AsyncClient(
base_url="https://utmn.modeus.org/",
timeout=timeout,
headers={
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0",
},
follow_redirects=True,
) as session:
form = await get_auth_form(session, username, __password)
auth_data = {}
continue_auth_url = "https://auth.modeus.org/commonauth"
Expand Down Expand Up @@ -113,7 +116,11 @@ def _extract_token_from_url(url: str, match_index: int = 1) -> str | None:
return match[match_index]


async def get_events(__jwt: str, body: ModeusSearchEvents, timeout: int = 15) -> list[str] | None:
async def get_events(
__jwt: str,
body: ModeusSearchEvents,
timeout: int = 15,
) -> list[str] | None:
"""Get events for student in modeus"""
session = AsyncClient(
http2=True,
Expand All @@ -122,6 +129,8 @@ async def get_events(__jwt: str, body: ModeusSearchEvents, timeout: int = 15) ->
)
session.headers["Authorization"] = f"Bearer {__jwt}"
session.headers["content-type"] = "application/json"
response = await session.post("/schedule-calendar-v2/api/calendar/events/search",
content=body.model_dump_json(by_alias=True))
response = await session.post(
"/schedule-calendar-v2/api/calendar/events/search",
content=body.model_dump_json(by_alias=True),
)
return response.json()
Loading

0 comments on commit 52406a6

Please sign in to comment.