Skip to content

Commit

Permalink
Merge pull request #14 from jowilf/dev
Browse files Browse the repository at this point in the history
Add py.typed(PEP 561)
  • Loading branch information
jowilf authored Oct 15, 2022
2 parents a4274d5 + cd21a49 commit 5ee62a9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 32 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.5] - 2022-10-15

---

### Added

- Add py.typed(PEP 561) by @jowilf https://github.com/jowilf/sqlalchemy-file/pull/14

## [0.1.4] - 2022-08-30

---
Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.5] - 2022-10-15

---

### Added

- Add py.typed(PEP 561) in [#14](https://github.com/jowilf/sqlalchemy-file/pull/14)

## [0.1.4] - 2022-08-30

---
Expand Down
57 changes: 32 additions & 25 deletions examples/fastapi/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import List, Optional, Union
from typing import Generator, List, Optional, Union

import uvicorn
from fastapi import Depends, FastAPI
Expand All @@ -26,8 +26,7 @@
StreamingResponse,
)

engine = create_engine("sqlite:////tmp/example.db?check_same_thread=False", echo=True)

engine = create_engine("sqlite:///example.db?check_same_thread=False", echo=True)

os.makedirs("/tmp/storage", 0o777, exist_ok=True)
driver = get_driver(Provider.LOCAL)("/tmp/storage")
Expand Down Expand Up @@ -80,7 +79,7 @@ class CategoryOut(CategoryBase):


def category_form(
name: str = Form(...),
name: str = Form(..., min_length=3),
image: Optional[UploadFile] = FormFile(None),
):
return Category(name=name, image=image)
Expand All @@ -89,37 +88,45 @@ def category_form(
app = FastAPI(title="SQLAlchemy-file Example", debug=True)


def get_session() -> Generator[Session, None, None]:
session: Session = Session(engine, expire_on_commit=False)
try:
yield session
except Exception as e:
session.rollback()
raise e
finally:
session.close()


@app.get("/categories", response_model=List[CategoryOut])
def get_all():
with Session(engine) as session:
return session.execute(select(Category)).all()
async def get_all(session: Session = Depends(get_session)):
return session.execute(select(Category)).scalars().all()


@app.get("/categories/{id}", response_model=CategoryOut)
def get_one(id: int = Path(...)):
with Session(engine) as session:
category = session.get(Category, id)
if category is not None:
return category
return JSONResponse({"detail": "Not found"}, status_code=404)
async def get_one(id: int = Path(...), session: Session = Depends(get_session)):
category = session.get(Category, id)
if category is not None:
return category
return JSONResponse({"detail": "Not found"}, status_code=404)


@app.post("/categories", response_model=CategoryOut)
def create_new(category: Category = Depends(category_form)):
with Session(engine) as session:
try:
session.add(category)
session.commit()
session.refresh(category)
return category
except ValidationError as e:
return JSONResponse(
dict(error={"key": e.key, "msg": e.msg}), status_code=422
)
async def create_new(
category: Category = Depends(category_form), session: Session = Depends(get_session)
):
try:
session.add(category)
session.commit()
session.refresh(category)
return category
except ValidationError as e:
return JSONResponse(dict(error={"key": e.key, "msg": e.msg}), status_code=422)


@app.get("/medias/{storage}/{file_id}", response_class=FileResponse)
def serve_files(storage: str = Path(...), file_id: str = Path(...)):
async def serve_files(storage: str = Path(...), file_id: str = Path(...)):
try:
file = StorageManager.get_file(f"{storage}/{file_id}")
if isinstance(file.object.driver, LocalStorageDriver):
Expand Down
2 changes: 1 addition & 1 deletion examples/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Book(db.Model):
ImageField(
upload_storage="images",
thumbnail_size=(50, 50),
validators=[SizeValidator("16M")],
validators=[SizeValidator("1M")],
)
)
document = db.Column(
Expand Down
11 changes: 6 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sqlalchemy-file"
version = "0.1.4"
version = "0.1.5"
description = "SQLAlchemy-file is a SQLAlchemy extension for attaching files to SQLAlchemy model and uploading them to various storage."
authors = ["Jocelin Hounon <hounonj@gmail.com>"]
license = "MIT"
Expand Down Expand Up @@ -29,27 +29,28 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.7"
SQLAlchemy = ">=1.4,<1.5.0"
apache-libcloud = "^3.6.0"
apache-libcloud = ">=3.6.0,<3.7"

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
sqlmodel = "^0.0.8"
Pillow = "^9.2.0"
fasteners = "^0.17.3"
fasteners = "^0.18"
black = "^22.6.0"
coverage = { extras = ["toml"], version = "^6.4.2" }
flake8 = "^3.9.2"
mypy = "^0.971"
mypy = "^0.982"
isort = "^5.10.1"
mkdocs-material = "^8.4.3"
PyMySQL = { extras = ["rsa"], version = "^1.0.2" }
psycopg2-binary = "^2.9.3"
mkdocstrings = { extras = ["python"], version = "^0.19.0" }
fastapi = "^0.82.0"
fastapi = "^0.85.1"
uvicorn = "^0.18.2"
python-multipart = "^0.0.5"
Flask = "^2.2.2"
Flask-SQLAlchemy = "^2.5.1"
importlib-metadata = { version = "<5.0", python = "<=3.7" }

[tool.coverage.report]
fail_under = 95
Expand Down
2 changes: 1 addition & 1 deletion sqlalchemy_file/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.4"
__version__ = "0.1.5"

from .file import File as File
from .types import FileField as FileField
Expand Down
Empty file added sqlalchemy_file/py.typed
Empty file.
2 changes: 2 additions & 0 deletions sqlalchemy_file/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class ImageField(FileField):
but also validates that the uploaded object is a valid image.
"""

cache_ok = True

def __init__(
self,
*args: Tuple[Any],
Expand Down

0 comments on commit 5ee62a9

Please sign in to comment.