Skip to content

Commit 668adf9

Browse files
authored
Add obj parameter for upload method (#130)
Add obj parameter for upload method (#130)
1 parent e290c88 commit 668adf9

24 files changed

Lines changed: 633 additions & 45 deletions

File tree

.github/workflows/ci.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
name: FastAdmin CI
22

3-
on: [create, push]
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
workflow_dispatch:
49

510
permissions:
611
contents: read
712
id-token: write
13+
pull-requests: write
814

9-
concurrency:
15+
concurrency:
1016
group: ${{ github.workflow }}-${{ github.ref }}
11-
cancel-in-progress: true
17+
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
1218

1319
jobs:
1420
ci:
@@ -20,14 +26,14 @@ jobs:
2026
- name: Set up Python
2127
uses: actions/setup-python@v4
2228
with:
23-
python-version: '3.13'
29+
python-version: "3.13"
2430
cache: "poetry"
2531
- name: Setup Node.js
2632
uses: actions/setup-node@v3
2733
with:
2834
node-version: v24.3.0
29-
cache: 'yarn'
30-
cache-dependency-path: 'frontend/yarn.lock'
35+
cache: "yarn"
36+
cache-dependency-path: "frontend/yarn.lock"
3137
- name: Install Dependencies
3238
run: make install
3339
- name: Run Lint

docs/build.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ def read_cls_docstring(cls):
4444

4545
def get_versions():
4646
return [
47+
{
48+
"version": "0.4.9",
49+
"changes": [
50+
"Add obj parameter to upload_file method.",
51+
"Add get_file_url method to model admin.",
52+
],
53+
},
4754
{
4855
"version": "0.4.8",
4956
"changes": [
@@ -426,7 +433,6 @@ def read_example(rel_path: str) -> str:
426433

427434

428435
def get_page_context(page_url):
429-
430436
from fastadmin import InlineModelAdmin, ModelAdmin, WidgetType, widget_action
431437
from fastadmin.models.base import BaseModelAdmin
432438
from fastadmin.models.schemas import (
@@ -759,7 +765,27 @@ async def save_model(self, id, payload):
759765
},
760766
{
761767
"type": "text",
762-
"content": "For file and image fields use <code>UploadFile</code> and <code>UploadImage</code> widgets in <code>formfield_overrides</code>. Implement <code>upload_file(obj, field_name, file_name, file_content)</code> on the model admin to handle uploads; it must return the file URL (e.g. after saving to disk or S3).",
768+
"content": "For file and image fields use <code>UploadFile</code> and <code>UploadImage</code> widgets in <code>formfield_overrides</code>. Implement <code>upload_file(field_name, file_name, file_content, obj=None)</code> on the model admin to handle uploads; it must return the file URL (e.g. after saving to disk or S3).",
769+
},
770+
{
771+
"type": "text",
772+
"content": "To customise the URL shown in the upload widget (e.g. generate an S3 presigned URL instead of displaying the raw <code>s3://</code> key), override <code>get_file_url</code> on the model admin. The display URL is emitted as <code>{field_name}__url</code> in the serialised object and passed to the widget as <code>valueRepr</code>; the raw stored value is never changed, so form saves are unaffected.",
773+
},
774+
{
775+
"type": "code-python",
776+
"content": """async def get_file_url(self, field_name: str, value: str, obj=None) -> str:
777+
# value is the raw stored key, e.g. "s3://bucket/key"
778+
# Return a presigned URL so the file can be viewed in the admin.
779+
# Example using aiobotocore:
780+
#
781+
# bucket, key = value.replace("s3://", "").split("/", 1)
782+
# async with aiobotocore.session.get_session().create_client("s3") as s3:
783+
# return await s3.generate_presigned_url(
784+
# "get_object",
785+
# Params={"Bucket": bucket, "Key": key},
786+
# ExpiresIn=3600,
787+
# )
788+
return value""",
763789
},
764790
{
765791
"type": "text",

0 commit comments

Comments
 (0)