Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
rev: v6.0.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version
rev: v0.4.4
rev: v0.15.12
hooks:
# Run linter
- id: ruff
- id: ruff-check
args: [ --fix ]
# Run formatter
- id: ruff-format
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ If you're planning to read through the source code, please check out this projec
4. Install developer dependencies
```bash
# Install main dependencies
pip install . # or: uv pip install .
pip install . # or: uv sync

# Install with dev dependencies
pip install ".[dev]" # or: uv pip install ".[dev]"
pip install ".[dev]" # or: uv sync --extra dev

# Install with test dependencies
pip install ".[test]" # or: uv pip install ".[test]"
pip install ".[test]" # or: uv sync --extra test

# Install with all dependencies
pip install ".[dev, test]" # or: uv pip install ".[dev, test]"
pip install ".[dev, test]" # or: uv sync --all-extras
```

5. Follow the database setup instructions on the [wiki](https://github.com/CSSS/csss-site-backend/wiki/1.-Local-Setup#database-setup). The recommended way is to do it through Docker, but both should work.
6. You will need to set the following environment variables
```bash
export DB_PORT=5444 # The port your database is listening at
export DB_PORT=5444 # If you're using Docker
export LOCAL=true # Should be true if you're running this locally
```

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies = [
[project.optional-dependencies]
dev = [
"ruff==0.15.12", # linting and formatter
"pre-commit"
]

test = [
Expand Down
4 changes: 2 additions & 2 deletions src/auth/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ async def login_user(
service_url = body.service
service = urllib.parse.quote(service_url)
service_validate_url = f"https://cas.sfu.ca/cas/serviceValidate?service={service}&ticket={body.ticket}"
async with httpx.AsyncClient() as client:
response = await client.get(service_validate_url)
client = request.app.state.http_client
response = await client.get(service_validate_url)
cas_response = xmltodict.parse(response.text)

if "cas:authenticationFailure" in cas_response["cas:serviceResponse"]:
Expand Down
16 changes: 6 additions & 10 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import asyncio
import contextlib
import os
from collections.abc import AsyncIterator
from collections.abc import AsyncGenerator
from typing import Annotated, Any

import asyncpg
import httpx
import sqlalchemy
from fastapi import Depends, FastAPI
from sqlalchemy import MetaData
from sqlalchemy.ext.asyncio import (
AsyncConnection,
AsyncSession,
)
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase

convention = {
Expand All @@ -31,8 +27,8 @@ class Base(DeclarativeBase):
# from: https://medium.com/@tclaitken/setting-up-a-fastapi-app-with-async-sqlalchemy-2-0-pydantic-v2-e6c540be4308
class DatabaseSessionManager:
def __init__(self, db_url: str, engine_kwargs: dict[str, Any], check_db=True):
self._engine = sqlalchemy.ext.asyncio.create_async_engine(db_url, **engine_kwargs)
self._sessionmaker = sqlalchemy.ext.asyncio.async_sessionmaker(autocommit=False, bind=self._engine)
self._engine = create_async_engine(db_url, **engine_kwargs)
self._sessionmaker = async_sessionmaker(autocommit=False, bind=self._engine)

if check_db:
# check if the database exists by making a test connection
Expand Down Expand Up @@ -63,7 +59,7 @@ async def close(self):
self._sessionmaker = None

@contextlib.asynccontextmanager
async def connect(self) -> AsyncIterator[AsyncConnection]:
async def connect(self) -> AsyncGenerator[AsyncConnection]:
if self._engine is None:
raise Exception("DatabaseSessionManager is not initialized")

Expand All @@ -75,7 +71,7 @@ async def connect(self) -> AsyncIterator[AsyncConnection]:
raise

@contextlib.asynccontextmanager
async def session(self) -> AsyncIterator[AsyncSession]:
async def session(self) -> AsyncGenerator[AsyncSession]:
if self._sessionmaker is None:
raise Exception("DatabaseSessionManager is not initialized")

Expand Down
100 changes: 100 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading