-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathbuild.py
More file actions
760 lines (729 loc) · 27.2 KB
/
build.py
File metadata and controls
760 lines (729 loc) · 27.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
import inspect
import os
import sys
from datetime import datetime, timezone
from pathlib import Path
from django.apps import apps
from django.apps.registry import Apps
from jinja2 import Environment, FileSystemLoader, select_autoescape
from fastadmin.settings import ROOT_DIR, Settings
from tests.settings import ROOT_DIR as TESTS_ROOT_DIR
os.environ.setdefault("ADMIN_ENV_FILE", str(ROOT_DIR / "example.env"))
sys.path.append(str(TESTS_ROOT_DIR / "environment" / "django" / "dev"))
Apps.check_apps_ready = lambda x: None
def html_minify(html: str) -> str:
return html.strip()
class App:
label = "app"
apps.get_containing_app_config = lambda x: App()
GITHUB_URL = "https://github.com/vsdudakov/fastadmin"
PYPI_URL = "https://pypi.org/project/fastadmin/"
NAME = "FastAdmin"
AUTHOR_NAME = "Seva D."
AUTHOR_EMAIL = "vsdudakov@gmail.com"
ANTD_CHARTS_EXAMPLES = "https://ant-design-charts.antgroup.com/en/examples"
def read_cls_docstring(cls):
return cls.__doc__.strip()
def get_versions():
return [
{
"version": "0.3.4",
"changes": ["Fix sort by and search by relations. Fix examples."],
},
{
"version": "0.3.3",
"changes": [
"Fix DetachedInstanceError when session is closed after commit.",
"Fix list display widths.",
"Fix flask issues.",
],
},
{
"version": "0.3.2",
"changes": [
"Add formfield_overrides example. Add label and help props.",
],
},
{
"version": "0.3.1",
"changes": [
"Fix sqlalchemy required fields. Fix CI.",
],
},
{
"version": "0.3.0",
"changes": [
"Clean up documentation. Update dependencies. Fix linters and tests. Frontend refactoring.",
],
},
{
"version": "0.2.22",
"changes": [
"Fix upload base64 widget; add disableCropImage prop. Fix examples.",
],
},
{
"version": "0.2.21",
"changes": [
"Fix cleaning of async select fields on forms.",
],
},
{
"version": "0.2.20",
"changes": [
"Fix _id fields handling. Bump backend and frontend packages.",
],
},
{
"version": "0.2.19",
"changes": [
"Fix is_pk for Tortoise ORM.",
],
},
{
"version": "0.2.18",
"changes": [
"Fix M2M/FK handling for SQLAlchemy with PostgreSQL (convert str to int).",
],
},
{
"version": "0.2.17",
"changes": [
"Fix FK handling for SQLAlchemy with PostgreSQL (convert str to int).",
],
},
{
"version": "0.2.16",
"changes": [
"Add ADMIN_DISABLE_CROP_IMAGE setting to configure image cropping on upload.",
],
},
{
"version": "0.2.15",
"changes": [
"Fix password logic for user model.",
],
},
{
"version": "0.2.14",
"changes": [
"Make permission functions awaitable. Bump frontend and backend packages.",
],
},
{
"version": "0.2.13",
"changes": [
"Fix edit page frontend issue for Date field.",
],
},
{
"version": "0.2.12",
"changes": [
"Remove python-dotenv dependency. Bump Django. Add Django example.",
],
},
{
"version": "0.2.11",
"changes": [
"Fix examples. Fix Pony ORM (delete, update M2M). Allow sorting by custom columns. Fix list_display ordering.",
],
},
{
"version": "0.2.10",
"changes": [
"Fix empty M2M issue. Optimize unit tests. Fix Pony ORM. Optimize Tortoise ORM search.",
],
},
{
"version": "0.2.9",
"changes": [
"Fix modal inline dialogs. Fix M2M multiple select.",
],
},
{
"version": "0.2.8",
"changes": [
"Fix SQLAlchemy delete functionality. Add more examples.",
],
},
{
"version": "0.2.7",
"changes": [
"Fix helper functions. Add regex support.",
],
},
{
"version": "0.2.6",
"changes": [
"Add edit button for async select.",
],
},
{
"version": "0.2.5",
"changes": [
"Fix async select in inlines.",
],
},
{
"version": "0.2.4",
"changes": [
"Fix dashboard widgets and auto-register inlines.",
],
},
{
"version": "0.2.3",
"changes": [
"Fix filter issue on list views. Remove Jinja from dependencies.",
],
},
{
"version": "0.2.2",
"changes": [
"Fix datetime-related bugs.",
],
},
{
"version": "0.2.1",
"changes": [
"Update packages. Fix linters and tests in Vite frontend. Remove Pydantic from dependencies.",
],
},
{
"version": "0.2.0",
"changes": [
"Update packages. Use Vite instead of deprecated react-scripts.",
],
},
]
def get_sections():
versions = get_versions()
return [
{
"name": "Introduction",
"url": "#introduction",
},
{
"name": "Getting Started",
"url": "#getting_started",
"children": [
{
"name": "Installation",
"url": "#installation",
},
{
"name": "Quick Tutorial",
"url": "#quick_tutorial",
},
],
},
{
"name": "Settings",
"url": "#settings",
},
{
"name": "Dashboard Widget Admins",
"url": "#dashboard-widget-admins",
"children": [
{
"name": "Registering Widgets",
"url": "#registering-widgets",
},
{
"name": "Methods and Attributes",
"url": "#widget-methods-and-attributes",
},
{
"name": "Chart Types",
"url": "#widget-chart-types",
},
],
},
{
"name": "Model Admins",
"url": "#model-admins",
"children": [
{
"name": "Registering Models",
"url": "#registering-models",
},
{
"name": "Authentication",
"url": "#authentication",
},
{
"name": "Methods and Attributes",
"url": "#model-methods-and-attributes",
},
{
"name": "Form Field Types",
"url": "#model-form-field-types",
},
],
},
{
"name": "Inline Model Admins",
"url": "#inline-admins",
"children": [
{
"name": "Registering Inlines",
"url": "#registering-inlines",
},
{
"name": "Methods and Attributes",
"url": "#inline-methods-and-attributes",
},
],
},
{
"name": "Changelog",
"url": "#changelog",
"children": [
{
"name": f"v{version['version']}",
"url": f"#v{version['version'].replace('.', '_')}",
}
for version in versions
],
},
]
def get_page_context(page_url):
from docs.code.dashboard import djangoorm as dashboard_djangoorm
from docs.code.dashboard import tortoise as dashboard_tortoise
from docs.code.inlines import tortoise as inlines_tortoise
from docs.code.models import tortoise as models_tortoise
from docs.code.quick_tutorial import django as quick_tutorial_django
from docs.code.quick_tutorial import djangoorm as quick_tutorial_djangoorm
from docs.code.quick_tutorial import fastapi as quick_tutorial_fastapi
from docs.code.quick_tutorial import flask as quick_tutorial_flask
from docs.code.quick_tutorial import ponyorm as quick_tutorial_ponyorm
from docs.code.quick_tutorial import sqlalchemy as quick_tutorial_sqlalchemy
from docs.code.quick_tutorial import tortoise as quick_tutorial_tortoise
from fastadmin import DashboardWidgetAdmin, DashboardWidgetType, InlineModelAdmin, ModelAdmin, WidgetType
from fastadmin.models.base import BaseModelAdmin
match page_url:
case "#introduction":
return [
{
"type": "text",
"content": f"<a href='{GITHUB_URL}' target='_blank'>FastAdmin</a> is an easy-to-use admin dashboard for FastAPI, Django, and Flask, inspired by Django Admin.",
},
{
"type": "text",
"content": "FastAdmin is built with relationships in mind and admiration for Django Admin. Its design focuses on making it as easy as possible to configure your admin dashboard for FastAPI, Django, or Flask.",
},
{
"type": "text",
"content": "FastAdmin aims to be minimal, functional, and familiar.",
},
]
case "#getting_started":
return [
{
"type": "alert-info",
"content": f"If you have questions beyond this documentation, feel free to <a href='mailto:{AUTHOR_EMAIL}' target='_blank'>email us</a>.",
},
]
case "#installation":
return [
{
"type": "text-lead",
"content": f"Follow the steps below to set up {NAME}:",
},
{
"type": "text",
"content": "Install the package with pip:",
},
{
"type": "alert-info",
"content": "On zsh and macOS, use quotes: <code>pip install 'fastadmin[fastapi,django]'</code>",
},
{
"type": "code-bash",
"content": """
pip install fastadmin[fastapi,django] # FastAPI with Django ORM
pip install fastadmin[fastapi,tortoise-orm] # FastAPI with Tortoise ORM
pip install fastadmin[fastapi,pony] # FastAPI with Pony ORM
pip install fastadmin[fastapi,sqlalchemy] # FastAPI with SQLAlchemy (includes greenlet)
pip install fastadmin[django] # Django with Django ORM
pip install fastadmin[django,pony] # Django with Pony ORM
pip install fastadmin[flask,sqlalchemy] # Flask with SQLAlchemy (includes greenlet)
""",
},
{
"type": "text",
"content": "Or install with Poetry:",
},
{
"type": "code-bash",
"content": """
poetry add 'fastadmin[fastapi,django]'
poetry add 'fastadmin[fastapi,tortoise-orm]'
poetry add 'fastadmin[fastapi,pony]'
poetry add 'fastadmin[fastapi,sqlalchemy]'
poetry add 'fastadmin[django]'
poetry add 'fastadmin[django,pony]'
poetry add 'fastadmin[flask,sqlalchemy]'
""",
},
{
"type": "alert-info",
"content": "When using SQLAlchemy, the <code>greenlet</code> package is required (included in the <code>fastadmin[sqlalchemy]</code> extra).",
},
{
"type": "text",
"content": "Configure the required settings with environment variables:",
},
{
"type": "alert-info",
"content": "You can add these variables to a <code>.env</code> file and load them with python-dotenv. See <a href='https://vsdudakov.github.io/fastadmin#settings'>all settings</a> in the full documentation.",
},
{
"type": "code-bash",
"content": """
export ADMIN_USER_MODEL=User
export ADMIN_USER_MODEL_USERNAME_FIELD=username
export ADMIN_SECRET_KEY=secret_key
""",
},
]
case "#quick_tutorial":
return [
{
"type": "text-lead",
"content": f"Set up {NAME} for your framework",
},
{
"type": "tabs",
"id": "setup_framework",
"content": [
{
"name": "FastAPI",
"id": "fastapi",
"content": [{"type": "code-python", "content": inspect.getsource(quick_tutorial_fastapi)}],
},
{
"name": "Django",
"id": "django",
"content": [{"type": "code-python", "content": inspect.getsource(quick_tutorial_django)}],
},
{
"name": "Flask",
"id": "flask",
"content": [{"type": "code-python", "content": inspect.getsource(quick_tutorial_flask)}],
},
],
},
{
"type": "text-lead",
"content": "Register ORM models",
},
{
"type": "tabs",
"id": "register_orm_models",
"content": [
{
"name": "Tortoise ORM",
"id": "tortoise_orm",
"content": [{"type": "code-python", "content": inspect.getsource(quick_tutorial_tortoise)}],
},
{
"name": "Django ORM",
"id": "django_orm",
"content": [
{"type": "code-python", "content": inspect.getsource(quick_tutorial_djangoorm)}
],
},
{
"name": "SQLAlchemy",
"id": "sql_alchemy",
"content": [
{"type": "code-python", "content": inspect.getsource(quick_tutorial_sqlalchemy)}
],
},
{
"name": "Pony ORM",
"id": "pony_orm",
"content": [{"type": "code-python", "content": inspect.getsource(quick_tutorial_ponyorm)}],
},
],
},
]
# settings
case "#settings":
return [
{
"type": "text",
"content": "The following settings have default values:",
},
{
"type": "alert-info",
"content": "Set environment variables or create a <code>.env</code> file and load it with the <code>python-dotenv</code> package.",
},
{"type": "code-python", "content": inspect.getsource(Settings)},
{
"type": "alert-warning",
"content": "Settings without default values are required.",
},
]
# widgets
case "#registering-widgets":
return [
{
"type": "text-lead",
"content": "Register dashboard widgets",
},
{
"type": "tabs",
"id": "register_dashboard_widgets",
"content": [
{
"name": "Tortoise ORM",
"id": "dashboard_tortoise_orm",
"content": [{"type": "code-python", "content": inspect.getsource(dashboard_tortoise)}],
},
{
"name": "Django ORM",
"id": "dashboard_django_orm",
"content": [{"type": "code-python", "content": inspect.getsource(dashboard_djangoorm)}],
},
{
"name": "SQLAlchemy",
"id": "dashboard_sql_alchemy",
"content": [{"type": "alert-info", "content": "See the Tortoise ORM example above."}],
},
{
"name": "Pony ORM",
"id": "dashboard_pony_orm",
"content": [{"type": "alert-info", "content": "See the Tortoise ORM example above."}],
},
],
},
]
case "#widget-methods-and-attributes":
return [
{
"type": "text",
"content": "The following methods and attributes are available for dashboard widget admins:",
},
{"type": "code-python", "content": inspect.getsource(DashboardWidgetAdmin)},
{
"type": "alert-warning",
"content": f"See <a href='{ANTD_CHARTS_EXAMPLES}' target='_blank'>antd charts</a> for <code>x_field_filter_widget_props</code>.",
},
]
case "#widget-chart-types":
return [
{
"type": "text",
"content": "The FastAdmin dashboard supports the following widget types:",
},
{"type": "code-python", "content": inspect.getsource(DashboardWidgetType)},
{
"type": "alert-warning",
"content": f"See <a href='{ANTD_CHARTS_EXAMPLES}' target='_blank'>antd charts</a> for more details (e.g. how they look).",
},
]
# models
case "#registering-models":
return [
{
"type": "tabs",
"id": "register_models",
"content": [
{
"name": "Tortoise ORM",
"id": "models_tortoise_orm",
"content": [{"type": "code-python", "content": inspect.getsource(models_tortoise)}],
},
{
"name": "Django ORM",
"id": "models_django_orm",
"content": [{"type": "alert-info", "content": "See example for Tortoise ORM"}],
},
{
"name": "SQLAlchemy",
"id": "models_sql_alchemy",
"content": [{"type": "alert-info", "content": "See the Tortoise ORM example"}],
},
{
"name": "Pony ORM",
"id": "models_pony_orm",
"content": [{"type": "alert-info", "content": "See the Tortoise ORM example"}],
},
],
},
]
case "#authentication":
return [
{
"type": "alert-info",
"content": "You must implement <code>authenticate</code> and <code>change_password</code> in the model admin for the User model. See the example above.",
},
]
case "#model-methods-and-attributes":
return [
{
"type": "text",
"content": "The following methods and attributes are available for model admins:",
},
{"type": "code-python", "content": inspect.getsource(BaseModelAdmin)},
{
"type": "text",
"content": "Model-admin-specific methods and attributes:",
},
{"type": "code-python", "content": inspect.getsource(ModelAdmin)},
{
"type": "text",
"content": "You can customize relation loading and relation search by overriding <code>orm_get_list</code> and forwarding <code>prefetch_related_fields</code> and <code>additional_search_fields</code>:",
},
{
"type": "code-python",
"content": """class TaskAdmin(TortoiseModelAdmin):
search_fields = ("title",)
async def orm_get_list(
self,
offset=None,
limit=None,
search=None,
sort_by=None,
filters=None,
):
return await super().orm_get_list(
offset=offset,
limit=limit,
search=search,
sort_by=sort_by,
filters=filters,
prefetch_related_fields=["user"],
additional_search_fields=["user__email"],
)
""",
},
]
case "#model-form-field-types":
return [
{
"type": "text",
"content": "The following form field types are available for model admins:",
},
{"type": "code-python", "content": inspect.getsource(WidgetType)},
{
"type": "alert-warning",
"content": "See <a href='https://ant.design/components/overview' target='_blank'>antd components</a> for more details (e.g. how they look).",
},
{
"type": "text",
"content": "Use <code>formfield_overrides</code> to customize widget props per field. You can set <code>label</code> for a custom field label and <code>help</code> for description text below the field:",
},
{
"type": "code-python",
"content": """formfield_overrides = {
"username": (
WidgetType.SlugInput,
{
"required": True,
"label": "Custom label",
"help": "Detailed description of the field",
},
),
}""",
},
]
# inlines
case "#registering-inlines":
return [
{
"type": "tabs",
"id": "register_inlines",
"content": [
{
"name": "Tortoise ORM",
"id": "inlines_tortoise_orm",
"content": [{"type": "code-python", "content": inspect.getsource(inlines_tortoise)}],
},
{
"name": "Django ORM",
"id": "inlines_django_orm",
"content": [{"type": "alert-info", "content": "See example for Tortoise ORM"}],
},
{
"name": "SQLAlchemy",
"id": "inlines_sql_alchemy",
"content": [{"type": "alert-info", "content": "See the Tortoise ORM example"}],
},
{
"name": "Pony ORM",
"id": "inlines_pony_orm",
"content": [{"type": "alert-info", "content": "See the Tortoise ORM example"}],
},
],
},
]
case "#inline-methods-and-attributes":
return [
{
"type": "text",
"content": "The following methods and attributes are available for inline model admins:",
},
{
"type": "alert-info",
"content": "See the BaseModelAdmin methods and attributes in the model admin section.",
},
{
"type": "text",
"content": "Inline-model-specific methods and attributes:",
},
{"type": "code-python", "content": inspect.getsource(InlineModelAdmin)},
]
# changelog
case "#changelog":
return [
{
"type": "alert-info",
"content": "See what was added, changed, fixed, or improved in the latest versions.",
},
]
case _:
if page_url.startswith("#v"):
version = page_url.replace("#v", "").replace("_", ".")
for v in get_versions():
if v["version"] == version:
return [
{
"type": "text",
"content": change,
}
for change in v["changes"]
]
return []
def get_context():
return {
"title": f"{NAME} | Documentation",
"description": f"{NAME} is an easy-to-use admin dashboard for FastAPI, Django, and Flask, inspired by Django Admin.",
"author": {
"name": AUTHOR_NAME,
"email": AUTHOR_EMAIL,
},
"name": NAME,
"year": datetime.now(timezone.utc).year,
"created_date": "7 March 2023",
"updated_date": datetime.now(timezone.utc).strftime("%d %B %Y"),
"github_url": GITHUB_URL,
"pypi_url": PYPI_URL,
"versions": get_versions(),
"sections": get_sections(),
"get_page_context": get_page_context,
}
def build():
env = Environment(loader=FileSystemLoader("."), autoescape=select_autoescape())
context = get_context()
index_template = env.get_template("templates/index.html")
index_html = index_template.render(**context)
with Path.open(Path("index.html"), "w") as fh:
fh.write(html_minify(index_html))
readme_template = env.get_template("templates/readme.md")
readme_md = readme_template.render(**context)
with Path.open(Path(ROOT_DIR / ".." / "README.md"), "w") as fh:
fh.write(readme_md)
if __name__ == "__main__":
build()