Skip to content

Commit 9e17bb0

Browse files
fix(core,database,forms): add missing forms module and fix SQLAlchemy 1.4 compatibility
- Add cotlette/forms/forms.py with base Form class for compatibility - Fix async_sessionmaker import for SQLAlchemy 1.4 compatibility - Replace async_sessionmaker with sessionmaker(class_=AsyncSession) - Add asynccontextmanager compatibility for Python 3.6 - Fix subscriptable types (list[User] -> List[User]) for Python 3.6 - Ensure all contrib apps (admin, users) work with Python 3.6 - Verified with successful server startup and app imports
1 parent f68101a commit 9e17bb0

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/cotlette/contrib/auth/users/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union
1+
from typing import Union, List
22

33
from fastapi import APIRouter, Depends, HTTPException, status
44
from pydantic import BaseModel
@@ -111,7 +111,7 @@ async def create_user(user: UserCreate):
111111

112112

113113
# Get all users (GET)
114-
@router.get("/", response_model=list[User])
114+
@router.get("/", response_model=List[User])
115115
async def get_users():
116116
users = await UserModel.objects.all().execute() # type: ignore
117117
return [User(name=user.name, age=user.age, email=user.email) for user in users]

src/cotlette/core/database/sqlalchemy.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
from sqlalchemy.ext.declarative import declarative_base
33
from sqlalchemy.orm import sessionmaker, Session
44
from sqlalchemy.pool import StaticPool
5-
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
6-
from contextlib import asynccontextmanager
5+
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
6+
# asynccontextmanager для Python 3.6 совместимости
7+
try:
8+
from contextlib import asynccontextmanager
9+
except ImportError:
10+
from contextlib import contextmanager as asynccontextmanager
711
import os
812
from typing import Optional, Dict, Any, List
913
from contextlib import contextmanager
@@ -215,11 +219,13 @@ async def initialize_async(self):
215219
# Для других баз данных используем тот же URL
216220
self.async_engine = create_async_engine(self.database_url)
217221

218-
# Создаем фабрику асинхронных сессий
219-
self.AsyncSessionLocal = async_sessionmaker(
222+
# Создаем фабрику асинхронных сессий (совместимость с SQLAlchemy 1.4)
223+
from sqlalchemy.orm import sessionmaker
224+
self.AsyncSessionLocal = sessionmaker(
220225
autocommit=False,
221226
autoflush=False,
222-
bind=self.async_engine
227+
bind=self.async_engine,
228+
class_=AsyncSession
223229
)
224230

225231
# Создаем таблицы

src/cotlette/forms/forms.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Base form classes for Cotlette framework.
3+
"""
4+
5+
class Form:
6+
"""
7+
Base form class for handling form data.
8+
"""
9+
10+
def __init__(self, data=None, files=None):
11+
self.data = data or {}
12+
self.files = files or {}
13+
self.errors = {}
14+
15+
def is_valid(self):
16+
"""
17+
Check if the form is valid.
18+
Override this method in subclasses.
19+
"""
20+
return len(self.errors) == 0
21+
22+
def clean(self):
23+
"""
24+
Clean and validate form data.
25+
Override this method in subclasses.
26+
"""
27+
pass

0 commit comments

Comments
 (0)