Skip to content

Commit 491c16f

Browse files
committed
Added uploadFile support for File and Image Field setter
1 parent 8374c09 commit 491c16f

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

docs/models/file-fields.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# **File & Image Column Types**
2+
3+
## **FileField Column**
4+
## **ImageField Column**
5+
### **Uploading File**
6+
#### Save file object
7+
#### Retrieve file object
8+
#### Extra and Headers
9+
#### Metadata
10+
## **Validators**
11+
## **Processors**
12+
## **Multiple Files**

ellar_sql/model/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,4 @@ def get_db_session(
174174

175175

176176
class Model(ModelBase, metaclass=ModelMeta):
177-
__base_config__: t.Union[ModelBaseConfig, t.Dict[str, t.Any]]
177+
__base_config__: t.ClassVar[t.Union[ModelBaseConfig, t.Dict[str, t.Any]]]

ellar_sql/model/typeDecorator/file/file.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ellar.app import current_injector
77
from ellar_storage import StorageService, StoredFile
88
from sqlalchemy_file.file import File as BaseFile
9+
from starlette.datastructures import UploadFile
910

1011
from ellar_sql.constant import DEFAULT_STORAGE_PLACEHOLDER
1112

@@ -39,6 +40,14 @@ def __init__(
3940
content_path: t.Optional[str] = None,
4041
**kwargs: t.Dict[str, t.Any],
4142
) -> None:
43+
if isinstance(content, UploadFile):
44+
filename = content.filename
45+
content_type = content.content_type
46+
47+
kwargs.setdefault("headers", dict(content.headers))
48+
49+
content = content.file
50+
4251
super().__init__(
4352
content=content,
4453
filename=filename,

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ nav:
7070
- Models:
7171
- index: models/index.md
7272
- Extra Fields: models/extra-fields.md
73+
- File and Image Fields: models/file-fields.md
7374
- Pagination: pagination/index.md
7475
- Multiple Database: multiple/index.md
7576
- Migrations:

tests/test_type_decorators/test_files/test_file_upload.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from contextlib import asynccontextmanager
33

44
import pytest
5+
from ellar.common.datastructures import ContentFile
56
from ellar_storage import StorageService
67
from libcloud.storage.types import ObjectDoesNotExistError
78

@@ -143,6 +144,29 @@ async def test_create_rollback(self, fake_file, fake_content, app_setup) -> None
143144
with pytest.raises(ObjectDoesNotExistError):
144145
storage_service.get_container().get_object(file_id)
145146

147+
async def test_create_rollback_with_uploadFile(
148+
self, fake_file, fake_content, app_setup
149+
) -> None:
150+
async with self.init_app(app_setup) as (app, db_service, session):
151+
session.add(
152+
Attachment(
153+
name="Create rollback",
154+
content=ContentFile(b"UploadFile should work just fine"),
155+
)
156+
)
157+
session.flush()
158+
attachment = session.execute(
159+
model.select(Attachment).where(Attachment.name == "Create rollback")
160+
).scalar_one()
161+
file_id = attachment.content.file_id
162+
163+
storage_service: StorageService = app.injector.get(StorageService)
164+
165+
assert storage_service.get_container().get_object(file_id) is not None
166+
session.rollback()
167+
with pytest.raises(ObjectDoesNotExistError):
168+
storage_service.get_container().get_object(file_id)
169+
146170
async def test_edit_existing(self, fake_file, app_setup) -> None:
147171
async with self.init_app(app_setup) as (app, db_service, session):
148172
session.add(Attachment(name="Editing test", content=fake_file))

0 commit comments

Comments
 (0)