Skip to content

Commit 4d310e1

Browse files
committed
Fix upload file functionality. Fix examples.
1 parent 8c5dbe1 commit 4d310e1

34 files changed

Lines changed: 198 additions & 202 deletions

File tree

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ class UserAdmin(TortoiseModelAdmin):
504504
user.hash_password = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
505505
await user.save(update_fields=("hash_password",))
506506

507-
async def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str:
507+
async def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str:
508508
# save file to media directory or s3/filestorage, then return the file url
509509
return f"/media/{file_name}"
510510

@@ -529,8 +529,6 @@ class UserAdmin(TortoiseModelAdmin):
529529

530530

531531
```python
532-
import typing as tp
533-
534532
from django.db import models
535533

536534
from fastadmin import DjangoModelAdmin, register
@@ -563,7 +561,7 @@ class UserAdmin(DjangoModelAdmin):
563561
return None
564562
return obj.id
565563

566-
def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
564+
def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
567565
# save file to media directory or s3/filestorage, then return the file url
568566
return f"/media/{file_name}"
569567

@@ -588,7 +586,6 @@ class UserAdmin(DjangoModelAdmin):
588586

589587

590588
```python
591-
import typing as tp
592589
import uuid
593590

594591
import bcrypt
@@ -654,7 +651,7 @@ class UserAdmin(SqlAlchemyModelAdmin):
654651
await session.execute(query)
655652
await session.commit()
656653

657-
async def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str:
654+
async def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str:
658655
# save file to media directory or s3/filestorage, then return the file url
659656
return f"/media/{file_name}"
660657

@@ -679,7 +676,6 @@ class UserAdmin(SqlAlchemyModelAdmin):
679676

680677

681678
```python
682-
import typing as tp
683679
import uuid
684680

685681
import bcrypt
@@ -733,7 +729,7 @@ class UserAdmin(PonyORMModelAdmin):
733729
obj.hash_password = hash_password
734730
commit()
735731

736-
def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
732+
def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
737733
# save file to media directory or s3/filestorage, then return the file url
738734
return f"/media/{file_name}"
739735

docs/build.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ def read_cls_docstring(cls):
4242

4343
def get_versions():
4444
return [
45+
{
46+
"version": "0.4.1",
47+
"changes": [
48+
"Fix upload file functionality. Fix examples.",
49+
],
50+
},
4551
{
4652
"version": "0.4.0",
4753
"changes": [

docs/code/models/tortoise.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import typing as tp
21
from uuid import UUID
32

43
import bcrypt
@@ -77,9 +76,7 @@ async def activate(self, ids: list[int]) -> None:
7776
async def deactivate(self, ids: list[int]) -> None:
7877
await self.model_cls.filter(id__in=ids).update(is_active=False)
7978

80-
async def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str:
79+
async def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str:
8180
# save file to media directory or s3/filestorage, then return the file url
8281
url = f"/media/{file_name}"
83-
setattr(obj, field_name, url)
84-
await obj.save(update_fields=(field_name,))
8582
return url

docs/code/quick_tutorial/djangoorm.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import typing as tp
2-
31
from django.db import models
42

53
from fastadmin import DjangoModelAdmin, register
@@ -32,6 +30,6 @@ def authenticate(self, username, password):
3230
return None
3331
return obj.id
3432

35-
def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
33+
def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
3634
# save file to media directory or s3/filestorage, then return the file url
3735
return f"/media/{file_name}"

docs/code/quick_tutorial/ponyorm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import typing as tp
21
import uuid
32

43
import bcrypt
@@ -52,6 +51,6 @@ def change_password(self, id: uuid.UUID | int, password: str) -> None:
5251
obj.hash_password = hash_password
5352
commit()
5453

55-
def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
54+
def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
5655
# save file to media directory or s3/filestorage, then return the file url
5756
return f"/media/{file_name}"

docs/code/quick_tutorial/sqlalchemy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import typing as tp
21
import uuid
32

43
import bcrypt
@@ -64,6 +63,6 @@ async def change_password(self, id: uuid.UUID | int | str, password: str) -> Non
6463
await session.execute(query)
6564
await session.commit()
6665

67-
async def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str:
66+
async def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str:
6867
# save file to media directory or s3/filestorage, then return the file url
6968
return f"/media/{file_name}"

docs/code/quick_tutorial/tortoise.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ async def change_password(self, id: UUID | int | str, password: str) -> None:
5353
user.hash_password = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
5454
await user.save(update_fields=("hash_password",))
5555

56-
async def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str:
56+
async def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str:
5757
# save file to media directory or s3/filestorage, then return the file url
5858
return f"/media/{file_name}"

docs/index.html

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ <h4 class="title">FastAdmin</h4>
196196

197197
<ul class="nav flex-column">
198198

199+
<li class="nav-item">
200+
<a class="nav-link" href="#v0_4_1">v0.4.1</a>
201+
</li>
202+
199203
<li class="nav-item">
200204
<a class="nav-link" href="#v0_4_0">v0.4.0</a>
201205
</li>
@@ -355,7 +359,7 @@ <h1>FastAdmin | Documentation</h1>
355359
<div class="row">
356360
<div class="col-sm-6 col-lg-4">
357361
<ul class="list-unstyled">
358-
<li><strong>Version:</strong> 0.4.0</li>
362+
<li><strong>Version:</strong> 0.4.1</li>
359363
<li>
360364
<strong>Author:</strong>
361365
<a href="mailto:vsdudakov@gmail.com" target="_blank">
@@ -953,7 +957,7 @@ <h3>Quick Tutorial</h3>
953957
user.hash_password = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
954958
await user.save(update_fields=("hash_password",))
955959

956-
async def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str:
960+
async def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str:
957961
# save file to media directory or s3/filestorage, then return the file url
958962
return f"/media/{file_name}"
959963

@@ -980,8 +984,6 @@ <h3>Quick Tutorial</h3>
980984

981985
<pre>
982986
<code class="language-python">
983-
import typing as tp
984-
985987
from django.db import models
986988

987989
from fastadmin import DjangoModelAdmin, register
@@ -1014,7 +1016,7 @@ <h3>Quick Tutorial</h3>
10141016
return None
10151017
return obj.id
10161018

1017-
def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
1019+
def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
10181020
# save file to media directory or s3/filestorage, then return the file url
10191021
return f"/media/{file_name}"
10201022

@@ -1041,7 +1043,6 @@ <h3>Quick Tutorial</h3>
10411043

10421044
<pre>
10431045
<code class="language-python">
1044-
import typing as tp
10451046
import uuid
10461047

10471048
import bcrypt
@@ -1107,7 +1108,7 @@ <h3>Quick Tutorial</h3>
11071108
await session.execute(query)
11081109
await session.commit()
11091110

1110-
async def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str:
1111+
async def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str:
11111112
# save file to media directory or s3/filestorage, then return the file url
11121113
return f"/media/{file_name}"
11131114

@@ -1134,7 +1135,6 @@ <h3>Quick Tutorial</h3>
11341135

11351136
<pre>
11361137
<code class="language-python">
1137-
import typing as tp
11381138
import uuid
11391139

11401140
import bcrypt
@@ -1188,7 +1188,7 @@ <h3>Quick Tutorial</h3>
11881188
obj.hash_password = hash_password
11891189
commit()
11901190

1191-
def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
1191+
def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str: # type: ignore[override]
11921192
# save file to media directory or s3/filestorage, then return the file url
11931193
return f"/media/{file_name}"
11941194

@@ -1883,7 +1883,6 @@ <h3>Registering Models</h3>
18831883

18841884
<pre>
18851885
<code class="language-python">
1886-
import typing as tp
18871886
from uuid import UUID
18881887

18891888
import bcrypt
@@ -1962,11 +1961,9 @@ <h3>Registering Models</h3>
19621961
async def deactivate(self, ids: list[int]) -> None:
19631962
await self.model_cls.filter(id__in=ids).update(is_active=False)
19641963

1965-
async def upload_file(self, obj: tp.Any, field_name: str, file_name: str, file_content: bytes) -> str:
1964+
async def upload_file(self, field_name: str, file_name: str, file_content: bytes) -> str:
19661965
# save file to media directory or s3/filestorage, then return the file url
19671966
url = f"/media/{file_name}"
1968-
setattr(obj, field_name, url)
1969-
await obj.save(update_fields=(field_name,))
19701967
return url
19711968

19721969
</code>
@@ -2628,14 +2625,12 @@ <h3>Methods and Attributes</h3>
26282625

26292626
async def upload_file(
26302627
self,
2631-
obj: Any,
26322628
field_name: str,
26332629
file_name: str,
26342630
file_content: bytes,
26352631
) -> str:
26362632
"""This method is used to upload files.
26372633

2638-
:params obj: an object.
26392634
:params field_name: a name of field.
26402635
:params file_name: a name of file.
26412636
:params file_content: a content of file.
@@ -3402,6 +3397,31 @@ <h2>Changelog</h2>
34023397

34033398

34043399

3400+
<section id="v0_4_1">
3401+
<h3>v0.4.1</h3>
3402+
3403+
3404+
3405+
<p class="text-4">
3406+
Fix upload file functionality. Fix examples.
3407+
</p>
3408+
3409+
3410+
3411+
3412+
3413+
3414+
3415+
3416+
3417+
3418+
3419+
3420+
3421+
3422+
</section>
3423+
3424+
34053425
<section id="v0_4_0">
34063426
<h3>v0.4.0</h3>
34073427

examples/django_djangoorm/orm/models.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import typing as tp
21
import uuid
32

43
from django.db import models
@@ -110,7 +109,6 @@ def change_password(self, id: uuid.UUID | int, password: str) -> None:
110109

111110
def upload_file(
112111
self,
113-
obj: tp.Any,
114112
field_name: str,
115113
file_name: str,
116114
file_content: bytes,

examples/fastapi_ponyorm/example.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import typing as tp
32
import uuid
43
from collections.abc import AsyncGenerator
54
from contextlib import asynccontextmanager
@@ -61,7 +60,6 @@ def change_password(self, id: uuid.UUID | int, password: str) -> None:
6160

6261
def upload_file(
6362
self,
64-
obj: tp.Any,
6563
field_name: str,
6664
file_name: str,
6765
file_content: bytes,

0 commit comments

Comments
 (0)