Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added uploadFile support for File and Image Field setter
  • Loading branch information
eadwinCode committed Jun 8, 2024
commit 491c16fcb7530b896b2fbf0aabbd70d50e1454c5
12 changes: 12 additions & 0 deletions docs/models/file-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# **File & Image Column Types**

## **FileField Column**
## **ImageField Column**
### **Uploading File**
#### Save file object
#### Retrieve file object
#### Extra and Headers
#### Metadata
## **Validators**
## **Processors**
## **Multiple Files**
2 changes: 1 addition & 1 deletion ellar_sql/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,4 @@ def get_db_session(


class Model(ModelBase, metaclass=ModelMeta):
__base_config__: t.Union[ModelBaseConfig, t.Dict[str, t.Any]]
__base_config__: t.ClassVar[t.Union[ModelBaseConfig, t.Dict[str, t.Any]]]
9 changes: 9 additions & 0 deletions ellar_sql/model/typeDecorator/file/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ellar.app import current_injector
from ellar_storage import StorageService, StoredFile
from sqlalchemy_file.file import File as BaseFile
from starlette.datastructures import UploadFile

from ellar_sql.constant import DEFAULT_STORAGE_PLACEHOLDER

Expand Down Expand Up @@ -39,6 +40,14 @@ def __init__(
content_path: t.Optional[str] = None,
**kwargs: t.Dict[str, t.Any],
) -> None:
if isinstance(content, UploadFile):
filename = content.filename
content_type = content.content_type

kwargs.setdefault("headers", dict(content.headers))

content = content.file

super().__init__(
content=content,
filename=filename,
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ nav:
- Models:
- index: models/index.md
- Extra Fields: models/extra-fields.md
- File and Image Fields: models/file-fields.md
- Pagination: pagination/index.md
- Multiple Database: multiple/index.md
- Migrations:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_type_decorators/test_files/test_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from contextlib import asynccontextmanager

import pytest
from ellar.common.datastructures import ContentFile
from ellar_storage import StorageService
from libcloud.storage.types import ObjectDoesNotExistError

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

async def test_create_rollback_with_uploadFile(
self, fake_file, fake_content, app_setup
) -> None:
async with self.init_app(app_setup) as (app, db_service, session):
session.add(
Attachment(
name="Create rollback",
content=ContentFile(b"UploadFile should work just fine"),
)
)
session.flush()
attachment = session.execute(
model.select(Attachment).where(Attachment.name == "Create rollback")
).scalar_one()
file_id = attachment.content.file_id

storage_service: StorageService = app.injector.get(StorageService)

assert storage_service.get_container().get_object(file_id) is not None
session.rollback()
with pytest.raises(ObjectDoesNotExistError):
storage_service.get_container().get_object(file_id)

async def test_edit_existing(self, fake_file, app_setup) -> None:
async with self.init_app(app_setup) as (app, db_service, session):
session.add(Attachment(name="Editing test", content=fake_file))
Expand Down