hyper-admin 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hyper_admin-1.0.0.dist-info/METADATA +366 -0
- hyper_admin-1.0.0.dist-info/RECORD +442 -0
- hyper_admin-1.0.0.dist-info/WHEEL +4 -0
- hyper_admin-1.0.0.dist-info/entry_points.txt +2 -0
- hyper_admin-1.0.0.dist-info/licenses/LICENSE +21 -0
- hyperadmin/__init__.py +52 -0
- hyperadmin/__main__.py +21 -0
- hyperadmin/adapters/__init__.py +28 -0
- hyperadmin/adapters/_estimate.py +33 -0
- hyperadmin/adapters/_filter_clause.py +9 -0
- hyperadmin/adapters/_occ.py +44 -0
- hyperadmin/adapters/_relation_loading.py +23 -0
- hyperadmin/adapters/_search.py +84 -0
- hyperadmin/adapters/filtering.py +86 -0
- hyperadmin/adapters/introspection.py +239 -0
- hyperadmin/adapters/registry.py +37 -0
- hyperadmin/adapters/schema.py +239 -0
- hyperadmin/adapters/sqlalchemy.py +423 -0
- hyperadmin/adapters/sqlmodel.py +650 -0
- hyperadmin/auth/__init__.py +156 -0
- hyperadmin/auth/_types.py +52 -0
- hyperadmin/auth/backend.py +37 -0
- hyperadmin/auth/bridge.py +351 -0
- hyperadmin/auth/exceptions.py +124 -0
- hyperadmin/auth/middleware.py +287 -0
- hyperadmin/auth/models.py +245 -0
- hyperadmin/auth/oauth/__init__.py +81 -0
- hyperadmin/auth/oauth/backend.py +611 -0
- hyperadmin/auth/oauth/providers.py +214 -0
- hyperadmin/auth/oauth/refresh.py +126 -0
- hyperadmin/auth/oauth/session.py +96 -0
- hyperadmin/auth/oauth/tokens.py +114 -0
- hyperadmin/auth/oauth/views.py +306 -0
- hyperadmin/auth/oauth/wiring.py +131 -0
- hyperadmin/auth/otp.py +189 -0
- hyperadmin/auth/permissions.py +232 -0
- hyperadmin/auth/session.py +81 -0
- hyperadmin/auth/session_middleware.py +164 -0
- hyperadmin/auth/token_handoff.py +452 -0
- hyperadmin/auth/user_cache.py +222 -0
- hyperadmin/auth/views.py +581 -0
- hyperadmin/core/__init__.py +76 -0
- hyperadmin/core/actions.py +151 -0
- hyperadmin/core/adapters.py +883 -0
- hyperadmin/core/app.py +1437 -0
- hyperadmin/core/auth.py +97 -0
- hyperadmin/core/bulk_results.py +28 -0
- hyperadmin/core/cache.py +68 -0
- hyperadmin/core/choices.py +38 -0
- hyperadmin/core/concurrency.py +107 -0
- hyperadmin/core/csrf.py +108 -0
- hyperadmin/core/discovery.py +161 -0
- hyperadmin/core/display.py +172 -0
- hyperadmin/core/fields.py +158 -0
- hyperadmin/core/fieldsets.py +22 -0
- hyperadmin/core/filtering.py +403 -0
- hyperadmin/core/filters_compat.py +78 -0
- hyperadmin/core/inlines.py +64 -0
- hyperadmin/core/introspection.py +280 -0
- hyperadmin/core/layouts.py +17 -0
- hyperadmin/core/lifecycle.py +278 -0
- hyperadmin/core/model.py +189 -0
- hyperadmin/core/navigation.py +354 -0
- hyperadmin/core/options.py +391 -0
- hyperadmin/core/palette.py +94 -0
- hyperadmin/core/panels.py +227 -0
- hyperadmin/core/plugin_wiring.py +205 -0
- hyperadmin/core/plugins.py +588 -0
- hyperadmin/core/primary_key.py +118 -0
- hyperadmin/core/rate_limit.py +90 -0
- hyperadmin/core/registry.py +129 -0
- hyperadmin/core/relation_loading.py +25 -0
- hyperadmin/core/schema.py +83 -0
- hyperadmin/core/sensitive.py +134 -0
- hyperadmin/core/settings.py +377 -0
- hyperadmin/core/storage_paths.py +54 -0
- hyperadmin/core/timezones.py +137 -0
- hyperadmin/core/uploads.py +16 -0
- hyperadmin/db.py +160 -0
- hyperadmin/discover.py +54 -0
- hyperadmin/filters/__init__.py +39 -0
- hyperadmin/filters/base.py +246 -0
- hyperadmin/filters/boolean.py +105 -0
- hyperadmin/filters/choice.py +74 -0
- hyperadmin/filters/date.py +265 -0
- hyperadmin/filters/relation.py +69 -0
- hyperadmin/filters/saved_views.py +203 -0
- hyperadmin/i18n/__init__.py +50 -0
- hyperadmin/i18n/loader.py +150 -0
- hyperadmin/i18n/middleware.py +159 -0
- hyperadmin/i18n/validation_messages.py +264 -0
- hyperadmin/loadtest/__init__.py +28 -0
- hyperadmin/loadtest/batch.py +263 -0
- hyperadmin/loadtest/checkpoint.py +168 -0
- hyperadmin/loadtest/generators/__init__.py +29 -0
- hyperadmin/loadtest/generators/auth.py +86 -0
- hyperadmin/loadtest/generators/erp.py +177 -0
- hyperadmin/loadtest/plan.py +189 -0
- hyperadmin/loadtest/pool.py +113 -0
- hyperadmin/loadtest/progress.py +150 -0
- hyperadmin/locale/ar/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/ar/LC_MESSAGES/messages.po +2139 -0
- hyperadmin/locale/de/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/de/LC_MESSAGES/messages.po +2114 -0
- hyperadmin/locale/en/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/en/LC_MESSAGES/messages.po +1780 -0
- hyperadmin/locale/es/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/es/LC_MESSAGES/messages.po +2094 -0
- hyperadmin/locale/fr/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/fr/LC_MESSAGES/messages.po +2100 -0
- hyperadmin/locale/he/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/he/LC_MESSAGES/messages.po +2076 -0
- hyperadmin/locale/hi/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/hi/LC_MESSAGES/messages.po +2084 -0
- hyperadmin/locale/id/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/id/LC_MESSAGES/messages.po +2071 -0
- hyperadmin/locale/it/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/it/LC_MESSAGES/messages.po +2098 -0
- hyperadmin/locale/ja/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/ja/LC_MESSAGES/messages.po +2049 -0
- hyperadmin/locale/ko/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/ko/LC_MESSAGES/messages.po +2049 -0
- hyperadmin/locale/nl/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/nl/LC_MESSAGES/messages.po +2100 -0
- hyperadmin/locale/pl/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/pl/LC_MESSAGES/messages.po +2112 -0
- hyperadmin/locale/pt_BR/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/pt_BR/LC_MESSAGES/messages.po +2098 -0
- hyperadmin/locale/ru/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/ru/LC_MESSAGES/messages.po +2112 -0
- hyperadmin/locale/th/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/th/LC_MESSAGES/messages.po +2065 -0
- hyperadmin/locale/tr/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/tr/LC_MESSAGES/messages.po +2084 -0
- hyperadmin/locale/uk/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/uk/LC_MESSAGES/messages.po +1889 -0
- hyperadmin/locale/vi/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/vi/LC_MESSAGES/messages.po +2063 -0
- hyperadmin/locale/zh_CN/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/zh_CN/LC_MESSAGES/messages.po +2049 -0
- hyperadmin/main.py +3 -0
- hyperadmin/management/__init__.py +0 -0
- hyperadmin/management/commands/__init__.py +0 -0
- hyperadmin/management/commands/createsuperuser.py +114 -0
- hyperadmin/management/commands/initdb.py +76 -0
- hyperadmin/management/commands/plugins.py +31 -0
- hyperadmin/management/commands/seed.py +125 -0
- hyperadmin/navigation/__init__.py +9 -0
- hyperadmin/navigation/pins.py +153 -0
- hyperadmin/panels/__init__.py +10 -0
- hyperadmin/panels/pdf.py +151 -0
- hyperadmin/permissions/__init__.py +59 -0
- hyperadmin/permissions/audit.py +109 -0
- hyperadmin/permissions/matrix.py +343 -0
- hyperadmin/permissions/object_rules.py +143 -0
- hyperadmin/realtime/__init__.py +51 -0
- hyperadmin/realtime/_debug.py +77 -0
- hyperadmin/realtime/config.py +124 -0
- hyperadmin/realtime/emitting.py +143 -0
- hyperadmin/realtime/events.py +107 -0
- hyperadmin/realtime/presence.py +193 -0
- hyperadmin/realtime/presence_redis.py +142 -0
- hyperadmin/realtime/pubsub.py +129 -0
- hyperadmin/realtime/pubsub_redis.py +159 -0
- hyperadmin/realtime/registry.py +102 -0
- hyperadmin/realtime/sse.py +82 -0
- hyperadmin/realtime/ws.py +477 -0
- hyperadmin/routing.py +680 -0
- hyperadmin/static/css/_accessibility.css +149 -0
- hyperadmin/static/css/_alerts.css +147 -0
- hyperadmin/static/css/_autocomplete.css +39 -0
- hyperadmin/static/css/_banner.css +118 -0
- hyperadmin/static/css/_buttons.css +241 -0
- hyperadmin/static/css/_card.css +86 -0
- hyperadmin/static/css/_dark-mode.css +232 -0
- hyperadmin/static/css/_fieldsets.css +71 -0
- hyperadmin/static/css/_filter.css +203 -0
- hyperadmin/static/css/_fonts.css +25 -0
- hyperadmin/static/css/_forms.css +862 -0
- hyperadmin/static/css/_inline-cell.css +222 -0
- hyperadmin/static/css/_inlines.css +160 -0
- hyperadmin/static/css/_layout.css +62 -0
- hyperadmin/static/css/_login.css +112 -0
- hyperadmin/static/css/_menu.css +156 -0
- hyperadmin/static/css/_navbar.css +299 -0
- hyperadmin/static/css/_page-header.css +139 -0
- hyperadmin/static/css/_pagination.css +125 -0
- hyperadmin/static/css/_palette.css +304 -0
- hyperadmin/static/css/_panels.css +174 -0
- hyperadmin/static/css/_permissions.css +547 -0
- hyperadmin/static/css/_pill.css +123 -0
- hyperadmin/static/css/_popover.css +170 -0
- hyperadmin/static/css/_popup.css +65 -0
- hyperadmin/static/css/_progress.css +161 -0
- hyperadmin/static/css/_realtime.css +78 -0
- hyperadmin/static/css/_reset.css +48 -0
- hyperadmin/static/css/_responsive.css +51 -0
- hyperadmin/static/css/_rtl.css +23 -0
- hyperadmin/static/css/_search.css +67 -0
- hyperadmin/static/css/_seg.css +95 -0
- hyperadmin/static/css/_sidebar.css +446 -0
- hyperadmin/static/css/_sticky.css +54 -0
- hyperadmin/static/css/_table.css +776 -0
- hyperadmin/static/css/_theme-terminal-type.css +92 -0
- hyperadmin/static/css/_theme-terminal.css +330 -0
- hyperadmin/static/css/_theme-toggle.css +46 -0
- hyperadmin/static/css/_tokens.css +232 -0
- hyperadmin/static/css/_utilities.css +133 -0
- hyperadmin/static/css/hyperadmin.css +58 -0
- hyperadmin/static/fonts/Inter-OFL.txt +93 -0
- hyperadmin/static/fonts/InterVariable.woff2 +0 -0
- hyperadmin/static/fonts/JetBrainsMono-OFL.txt +93 -0
- hyperadmin/static/fonts/JetBrainsMonoVariable.woff2 +0 -0
- hyperadmin/static/icons/arrow-down.svg +1 -0
- hyperadmin/static/icons/arrow-up.svg +1 -0
- hyperadmin/static/icons/bar-chart-3.svg +1 -0
- hyperadmin/static/icons/bell.svg +1 -0
- hyperadmin/static/icons/bookmark.svg +1 -0
- hyperadmin/static/icons/briefcase.svg +1 -0
- hyperadmin/static/icons/building-2.svg +1 -0
- hyperadmin/static/icons/calendar.svg +1 -0
- hyperadmin/static/icons/check.svg +1 -0
- hyperadmin/static/icons/chevron-down.svg +1 -0
- hyperadmin/static/icons/chevron-left.svg +1 -0
- hyperadmin/static/icons/chevron-right.svg +1 -0
- hyperadmin/static/icons/circle-check.svg +1 -0
- hyperadmin/static/icons/circle-help.svg +1 -0
- hyperadmin/static/icons/circle-x.svg +1 -0
- hyperadmin/static/icons/clock.svg +1 -0
- hyperadmin/static/icons/columns-3.svg +1 -0
- hyperadmin/static/icons/command.svg +1 -0
- hyperadmin/static/icons/copy.svg +1 -0
- hyperadmin/static/icons/credit-card.svg +1 -0
- hyperadmin/static/icons/database.svg +1 -0
- hyperadmin/static/icons/download.svg +1 -0
- hyperadmin/static/icons/ellipsis.svg +1 -0
- hyperadmin/static/icons/external-link.svg +1 -0
- hyperadmin/static/icons/eye.svg +1 -0
- hyperadmin/static/icons/file-text.svg +1 -0
- hyperadmin/static/icons/filter.svg +1 -0
- hyperadmin/static/icons/folder.svg +1 -0
- hyperadmin/static/icons/globe.svg +1 -0
- hyperadmin/static/icons/history.svg +1 -0
- hyperadmin/static/icons/home.svg +1 -0
- hyperadmin/static/icons/image.svg +1 -0
- hyperadmin/static/icons/info.svg +1 -0
- hyperadmin/static/icons/key-round.svg +1 -0
- hyperadmin/static/icons/layers.svg +1 -0
- hyperadmin/static/icons/layout-grid.svg +1 -0
- hyperadmin/static/icons/list-checks.svg +1 -0
- hyperadmin/static/icons/lock.svg +1 -0
- hyperadmin/static/icons/log-out.svg +1 -0
- hyperadmin/static/icons/mail.svg +1 -0
- hyperadmin/static/icons/map-pin.svg +1 -0
- hyperadmin/static/icons/menu.svg +1 -0
- hyperadmin/static/icons/moon.svg +1 -0
- hyperadmin/static/icons/package.svg +1 -0
- hyperadmin/static/icons/panel-left-close.svg +1 -0
- hyperadmin/static/icons/panel-left-open.svg +1 -0
- hyperadmin/static/icons/pencil.svg +1 -0
- hyperadmin/static/icons/pin-off.svg +1 -0
- hyperadmin/static/icons/pin.svg +1 -0
- hyperadmin/static/icons/plus.svg +1 -0
- hyperadmin/static/icons/receipt.svg +1 -0
- hyperadmin/static/icons/refresh-cw.svg +1 -0
- hyperadmin/static/icons/rotate-ccw.svg +1 -0
- hyperadmin/static/icons/search.svg +1 -0
- hyperadmin/static/icons/settings.svg +1 -0
- hyperadmin/static/icons/shield-check.svg +1 -0
- hyperadmin/static/icons/shopping-cart.svg +1 -0
- hyperadmin/static/icons/sliders-horizontal.svg +1 -0
- hyperadmin/static/icons/sparkles.svg +1 -0
- hyperadmin/static/icons/store.svg +1 -0
- hyperadmin/static/icons/sun.svg +1 -0
- hyperadmin/static/icons/tag.svg +1 -0
- hyperadmin/static/icons/trash-2.svg +1 -0
- hyperadmin/static/icons/triangle-alert.svg +1 -0
- hyperadmin/static/icons/truck.svg +1 -0
- hyperadmin/static/icons/upload.svg +1 -0
- hyperadmin/static/icons/user.svg +1 -0
- hyperadmin/static/icons/users.svg +1 -0
- hyperadmin/static/icons/wallet.svg +1 -0
- hyperadmin/static/icons/x.svg +1 -0
- hyperadmin/static/img/hyperadmin-favicon.svg +1 -0
- hyperadmin/static/img/hyperadmin-mark-mono.svg +1 -0
- hyperadmin/static/img/hyperadmin-mark.svg +1 -0
- hyperadmin/static/img/hyperadmin-wordmark-dark.svg +1 -0
- hyperadmin/static/img/hyperadmin-wordmark.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-favicon-16.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-favicon.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-mark.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-wordmark-dark.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-wordmark.svg +1 -0
- hyperadmin/static/js/autocomplete.js +475 -0
- hyperadmin/static/js/confirm_delete.js +113 -0
- hyperadmin/static/js/error_summary.js +37 -0
- hyperadmin/static/js/form_guard.js +77 -0
- hyperadmin/static/js/inline_edit.js +97 -0
- hyperadmin/static/js/palette.js +292 -0
- hyperadmin/static/js/primitives.js +205 -0
- hyperadmin/static/js/realtime-status.js +515 -0
- hyperadmin/static/js/request_errors.js +85 -0
- hyperadmin/static/js/shortcuts.js +175 -0
- hyperadmin/static/js/sidebar.js +83 -0
- hyperadmin/static/js/vendor/LICENSE-alpinejs.txt +21 -0
- hyperadmin/static/js/vendor/LICENSE-htmx.txt +25 -0
- hyperadmin/static/js/vendor/alpinejs-3.17.4.min.js +21 -0
- hyperadmin/static/js/vendor/alpinejs-focus-3.17.4.min.js +18 -0
- hyperadmin/static/js/vendor/htmx-1.9.10.min.js +1 -0
- hyperadmin/templates/_base.html +161 -0
- hyperadmin/templates/_messages.html +11 -0
- hyperadmin/templates/_navbar.html +126 -0
- hyperadmin/templates/_sidebar.html +80 -0
- hyperadmin/templates/auth/mfa_challenge.html +75 -0
- hyperadmin/templates/auth/mfa_settings.html +110 -0
- hyperadmin/templates/auth/oauth_link_required.html +24 -0
- hyperadmin/templates/auth/oauth_settings.html +55 -0
- hyperadmin/templates/base.html +16 -0
- hyperadmin/templates/bulk_form.html +18 -0
- hyperadmin/templates/bulk_result.html +20 -0
- hyperadmin/templates/components/_icons.html +86 -0
- hyperadmin/templates/components/_macros.html +27 -0
- hyperadmin/templates/components/_nav.html +55 -0
- hyperadmin/templates/components/_row_access.html +26 -0
- hyperadmin/templates/components/banner.html +77 -0
- hyperadmin/templates/components/bulk_form.html +81 -0
- hyperadmin/templates/components/bulk_result.html +133 -0
- hyperadmin/templates/components/bulk_toolbar.html +63 -0
- hyperadmin/templates/components/command_palette.html +59 -0
- hyperadmin/templates/components/confirm_delete_dialog.html +26 -0
- hyperadmin/templates/components/conflict_dialog.html +42 -0
- hyperadmin/templates/components/csrf_reload_banner.html +20 -0
- hyperadmin/templates/components/error_summary.html +39 -0
- hyperadmin/templates/components/error_toast.html +18 -0
- hyperadmin/templates/components/fieldset.html +38 -0
- hyperadmin/templates/components/filter_bar.html +50 -0
- hyperadmin/templates/components/filter_sidebar.html +31 -0
- hyperadmin/templates/components/filters/boolean.html +10 -0
- hyperadmin/templates/components/filters/current_period.html +8 -0
- hyperadmin/templates/components/filters/date_range.html +26 -0
- hyperadmin/templates/components/filters/is_owner.html +8 -0
- hyperadmin/templates/components/filters/multi_choice.html +10 -0
- hyperadmin/templates/components/filters/multi_relation.html +20 -0
- hyperadmin/templates/components/filters/select.html +11 -0
- hyperadmin/templates/components/inline_cell.html +23 -0
- hyperadmin/templates/components/inline_cell_error.html +51 -0
- hyperadmin/templates/components/inline_cell_stale.html +20 -0
- hyperadmin/templates/components/inline_editor.html +85 -0
- hyperadmin/templates/components/inline_formset.html +34 -0
- hyperadmin/templates/components/inline_row.html +49 -0
- hyperadmin/templates/components/list_error.html +12 -0
- hyperadmin/templates/components/menu.html +74 -0
- hyperadmin/templates/components/nav_pinned.html +24 -0
- hyperadmin/templates/components/pagination.html +87 -0
- hyperadmin/templates/components/panel_document.html +19 -0
- hyperadmin/templates/components/panel_states.html +35 -0
- hyperadmin/templates/components/panel_tab_strip.html +31 -0
- hyperadmin/templates/components/pill.html +15 -0
- hyperadmin/templates/components/popover.html +59 -0
- hyperadmin/templates/components/presence_banner.html +28 -0
- hyperadmin/templates/components/progress.html +72 -0
- hyperadmin/templates/components/saved_views.html +61 -0
- hyperadmin/templates/components/search_input.html +19 -0
- hyperadmin/templates/components/seg.html +38 -0
- hyperadmin/templates/components/shortcuts_overlay.html +45 -0
- hyperadmin/templates/components/sortable_header.html +23 -0
- hyperadmin/templates/components/table.html +45 -0
- hyperadmin/templates/components/table_body.html +86 -0
- hyperadmin/templates/components/thumbnail.html +8 -0
- hyperadmin/templates/components/toast.html +36 -0
- hyperadmin/templates/components/tooltip.html +20 -0
- hyperadmin/templates/create.html +43 -0
- hyperadmin/templates/dashboard.html +8 -0
- hyperadmin/templates/detail.html +120 -0
- hyperadmin/templates/detail_layout.html +15 -0
- hyperadmin/templates/error.html +24 -0
- hyperadmin/templates/form_layout.html +26 -0
- hyperadmin/templates/list.html +5 -0
- hyperadmin/templates/list_layout.html +105 -0
- hyperadmin/templates/login.html +79 -0
- hyperadmin/templates/permissions/_banner.html +55 -0
- hyperadmin/templates/permissions/_diff_row.html +6 -0
- hyperadmin/templates/permissions/_group_gone.html +16 -0
- hyperadmin/templates/permissions/_macros.html +22 -0
- hyperadmin/templates/permissions/matrix.html +408 -0
- hyperadmin/templates/permissions/object_popover.html +95 -0
- hyperadmin/templates/update.html +69 -0
- hyperadmin/templates/widgets/autocomplete.html +100 -0
- hyperadmin/templates/widgets/checkbox_input.html +20 -0
- hyperadmin/templates/widgets/choices_options.html +3 -0
- hyperadmin/templates/widgets/date_input.html +13 -0
- hyperadmin/templates/widgets/datetime_input.html +18 -0
- hyperadmin/templates/widgets/encoded_textarea.html +4 -0
- hyperadmin/templates/widgets/file_input.html +15 -0
- hyperadmin/templates/widgets/float_input.html +12 -0
- hyperadmin/templates/widgets/image_input.html +108 -0
- hyperadmin/templates/widgets/multiselect_input.html +13 -0
- hyperadmin/templates/widgets/number_input.html +11 -0
- hyperadmin/templates/widgets/popup_form.html +64 -0
- hyperadmin/templates/widgets/relation_multiselect_input.html +36 -0
- hyperadmin/templates/widgets/relation_select_input.html +38 -0
- hyperadmin/templates/widgets/select_input.html +17 -0
- hyperadmin/templates/widgets/text_input.html +13 -0
- hyperadmin/templates/widgets/textarea.html +4 -0
- hyperadmin/uploads/__init__.py +44 -0
- hyperadmin/uploads/images.py +272 -0
- hyperadmin/uploads/storage.py +265 -0
- hyperadmin/uploads/validation.py +37 -0
- hyperadmin/views/__init__.py +3 -0
- hyperadmin/views/csrf.py +316 -0
- hyperadmin/views/dynamic/__init__.py +35 -0
- hyperadmin/views/dynamic/actions.py +264 -0
- hyperadmin/views/dynamic/base.py +576 -0
- hyperadmin/views/dynamic/concurrency.py +138 -0
- hyperadmin/views/dynamic/core.py +33 -0
- hyperadmin/views/dynamic/create.py +189 -0
- hyperadmin/views/dynamic/detail.py +231 -0
- hyperadmin/views/dynamic/files.py +233 -0
- hyperadmin/views/dynamic/form_data.py +162 -0
- hyperadmin/views/dynamic/helpers.py +151 -0
- hyperadmin/views/dynamic/inline.py +348 -0
- hyperadmin/views/dynamic/list_view.py +612 -0
- hyperadmin/views/dynamic/model_view.py +17 -0
- hyperadmin/views/dynamic/palette_search.py +78 -0
- hyperadmin/views/dynamic/relations.py +544 -0
- hyperadmin/views/dynamic/saved_views.py +159 -0
- hyperadmin/views/dynamic/update.py +313 -0
- hyperadmin/views/error_pages.py +46 -0
- hyperadmin/views/forms.py +1294 -0
- hyperadmin/views/htmx.py +53 -0
- hyperadmin/views/locale.py +113 -0
- hyperadmin/views/nav_pins.py +149 -0
- hyperadmin/views/navigation.py +143 -0
- hyperadmin/views/palette_context.py +91 -0
- hyperadmin/views/palette_search.py +103 -0
- hyperadmin/views/permissions_matrix.py +490 -0
- hyperadmin/views/rate_limit.py +87 -0
- hyperadmin/views/route.py +316 -0
- hyperadmin/views/static.py +138 -0
- hyperadmin/views/template_filters.py +305 -0
- hyperadmin/views/uploads.py +92 -0
- hyperadmin/views/urls.py +180 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"""Image upload helpers: metadata, EXIF auto-rotation and cached thumbnails.
|
|
2
|
+
|
|
3
|
+
Everything here is a pure function over Pillow images or file-like objects.
|
|
4
|
+
The only side effects are the ones the caller asks for: rewriting an upload
|
|
5
|
+
in place (:func:`prepare_image_upload`) or writing a thumbnail file next to
|
|
6
|
+
its original (:func:`generate_thumbnail`).
|
|
7
|
+
|
|
8
|
+
Two guards apply to every image this module opens:
|
|
9
|
+
|
|
10
|
+
* **Non-image input** never raises. :func:`extract_metadata` and
|
|
11
|
+
:func:`prepare_image_upload` return ``None`` and leave the stream rewound;
|
|
12
|
+
:func:`generate_thumbnail` returns a placeholder.
|
|
13
|
+
* **Decompression bombs** are rejected before a single pixel is decoded.
|
|
14
|
+
Pillow opens images lazily, so the header-declared size is compared with
|
|
15
|
+
``max_pixels`` (``Pillow``'s own ``MAX_IMAGE_PIXELS`` by default) and an
|
|
16
|
+
:class:`ImageTooLargeError` is raised for anything larger.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import io
|
|
22
|
+
import logging
|
|
23
|
+
import warnings
|
|
24
|
+
from contextlib import contextmanager
|
|
25
|
+
from dataclasses import dataclass
|
|
26
|
+
from pathlib import Path, PurePosixPath
|
|
27
|
+
from typing import IO, TYPE_CHECKING, Any
|
|
28
|
+
|
|
29
|
+
from PIL import Image, ImageOps
|
|
30
|
+
|
|
31
|
+
from hyperadmin.core.storage_paths import resolve_storage_path
|
|
32
|
+
from hyperadmin.uploads.validation import FileValidationError
|
|
33
|
+
|
|
34
|
+
if TYPE_CHECKING:
|
|
35
|
+
from collections.abc import Iterator
|
|
36
|
+
|
|
37
|
+
from starlette.datastructures import UploadFile
|
|
38
|
+
|
|
39
|
+
logger = logging.getLogger(__name__)
|
|
40
|
+
|
|
41
|
+
#: Pixel budget (width x height) above which an image is treated as a bomb.
|
|
42
|
+
MAX_IMAGE_PIXELS: int = Image.MAX_IMAGE_PIXELS or 89_478_485
|
|
43
|
+
#: Directory, relative to the original, that holds its cached thumbnails.
|
|
44
|
+
THUMBNAIL_DIR = ".thumbs"
|
|
45
|
+
#: Static asset returned by :func:`generate_thumbnail` when no thumbnail can be made.
|
|
46
|
+
PLACEHOLDER_THUMBNAIL = "icons/file-text.svg"
|
|
47
|
+
#: Default bounding box for thumbnails.
|
|
48
|
+
DEFAULT_THUMBNAIL_SIZE: tuple[int, int] = (200, 200)
|
|
49
|
+
|
|
50
|
+
_ORIENTATION_TAG = 0x0112
|
|
51
|
+
_NORMAL_ORIENTATION = 1
|
|
52
|
+
# Pillow signals unreadable input with these (UnidentifiedImageError is an OSError).
|
|
53
|
+
_OPEN_ERRORS: tuple[type[Exception], ...] = (OSError, ValueError, SyntaxError, TypeError)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class ImageTooLargeError(FileValidationError):
|
|
57
|
+
"""Raised when an image declares more pixels than ``max_pixels`` allows."""
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
_THUMBNAIL_ERRORS: tuple[type[Exception], ...] = (ImageTooLargeError, *_OPEN_ERRORS)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True)
|
|
64
|
+
class ImageMetadata:
|
|
65
|
+
"""What an image says about itself, read from its header and EXIF block.
|
|
66
|
+
|
|
67
|
+
Attributes:
|
|
68
|
+
width: Pixel width as stored (before any EXIF rotation is applied).
|
|
69
|
+
height: Pixel height as stored.
|
|
70
|
+
format: Pillow format name, e.g. ``"JPEG"`` or ``"PNG"``.
|
|
71
|
+
orientation: EXIF orientation tag (1-8); ``1`` means "already upright".
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
width: int
|
|
75
|
+
height: int
|
|
76
|
+
format: str
|
|
77
|
+
orientation: int = _NORMAL_ORIENTATION
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def needs_rotation(self) -> bool:
|
|
81
|
+
"""``True`` when the EXIF orientation asks the viewer to transpose the pixels."""
|
|
82
|
+
return self.orientation != _NORMAL_ORIENTATION
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _as_stream(source: Any) -> Any:
|
|
86
|
+
"""Unwrap an ``UploadFile`` to its underlying file; pass paths and streams through."""
|
|
87
|
+
inner = getattr(source, "file", None)
|
|
88
|
+
return inner if inner is not None and hasattr(inner, "read") else source
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _rewind(stream: Any) -> None:
|
|
92
|
+
if hasattr(stream, "seek"):
|
|
93
|
+
stream.seek(0)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@contextmanager
|
|
97
|
+
def _open_image(source: Any, *, max_pixels: int) -> Iterator[Image.Image]:
|
|
98
|
+
"""Open ``source`` lazily and refuse it before decoding if it is too large.
|
|
99
|
+
|
|
100
|
+
A file Pillow opened itself (from a path) is closed on exit; a stream the
|
|
101
|
+
caller passed in is left open for them.
|
|
102
|
+
|
|
103
|
+
Raises:
|
|
104
|
+
ImageTooLargeError: The header declares more than ``max_pixels`` pixels.
|
|
105
|
+
OSError, ValueError, SyntaxError, TypeError: Pillow cannot read the input.
|
|
106
|
+
"""
|
|
107
|
+
with warnings.catch_warnings():
|
|
108
|
+
# The size is checked explicitly below; Pillow's own 1x warning is noise here.
|
|
109
|
+
warnings.simplefilter("ignore", Image.DecompressionBombWarning)
|
|
110
|
+
try:
|
|
111
|
+
image = Image.open(source)
|
|
112
|
+
except Image.DecompressionBombError as exc:
|
|
113
|
+
raise ImageTooLargeError(str(exc)) from exc
|
|
114
|
+
with image:
|
|
115
|
+
if image.width * image.height > max_pixels:
|
|
116
|
+
raise ImageTooLargeError(
|
|
117
|
+
f"Image of {image.width}x{image.height} pixels exceeds the limit of "
|
|
118
|
+
f"{max_pixels} pixels"
|
|
119
|
+
)
|
|
120
|
+
yield image
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def extract_metadata(
|
|
124
|
+
source: str | Path | IO[bytes] | UploadFile,
|
|
125
|
+
*,
|
|
126
|
+
max_pixels: int = MAX_IMAGE_PIXELS,
|
|
127
|
+
) -> ImageMetadata | None:
|
|
128
|
+
"""Read dimensions, format and EXIF orientation without decoding pixels.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
source: A path, a binary stream or a Starlette ``UploadFile``. Streams
|
|
132
|
+
are rewound to position 0 afterwards.
|
|
133
|
+
max_pixels: Decompression-bomb threshold (width x height).
|
|
134
|
+
|
|
135
|
+
Returns:
|
|
136
|
+
The metadata, or ``None`` when ``source`` is not an image Pillow can read.
|
|
137
|
+
|
|
138
|
+
Raises:
|
|
139
|
+
ImageTooLargeError: The image is larger than ``max_pixels``.
|
|
140
|
+
"""
|
|
141
|
+
stream = _as_stream(source)
|
|
142
|
+
try:
|
|
143
|
+
with _open_image(stream, max_pixels=max_pixels) as image:
|
|
144
|
+
orientation = int(image.getexif().get(_ORIENTATION_TAG, _NORMAL_ORIENTATION))
|
|
145
|
+
return ImageMetadata(image.width, image.height, image.format or "", orientation)
|
|
146
|
+
except ImageTooLargeError:
|
|
147
|
+
raise
|
|
148
|
+
except _OPEN_ERRORS:
|
|
149
|
+
return None
|
|
150
|
+
finally:
|
|
151
|
+
_rewind(stream)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def auto_rotate(image: Image.Image) -> Image.Image:
|
|
155
|
+
"""Return ``image`` transposed so its pixels match the EXIF orientation.
|
|
156
|
+
|
|
157
|
+
The returned image carries no orientation tag, so re-saving it will not
|
|
158
|
+
rotate it twice. An image without an orientation tag is returned as a copy.
|
|
159
|
+
"""
|
|
160
|
+
rotated = ImageOps.exif_transpose(image)
|
|
161
|
+
return image if rotated is None else rotated
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def prepare_image_upload(
|
|
165
|
+
upload: UploadFile,
|
|
166
|
+
*,
|
|
167
|
+
max_pixels: int = MAX_IMAGE_PIXELS,
|
|
168
|
+
) -> ImageMetadata | None:
|
|
169
|
+
"""Normalise an uploaded image in place before the storage column saves it.
|
|
170
|
+
|
|
171
|
+
This is the hook the form save path calls for ``ImageType`` fields. A
|
|
172
|
+
non-image upload is left untouched. An image whose EXIF orientation says
|
|
173
|
+
"rotate me" is re-encoded upright, in its original format and with the
|
|
174
|
+
rest of its EXIF block, straight back into ``upload.file``.
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
upload: The multipart upload; its ``file`` is read, possibly rewritten,
|
|
178
|
+
and always rewound.
|
|
179
|
+
max_pixels: Decompression-bomb threshold (width x height).
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
The metadata of the file as it will be stored, or ``None`` for a non-image.
|
|
183
|
+
|
|
184
|
+
Raises:
|
|
185
|
+
ImageTooLargeError: The image is larger than ``max_pixels``.
|
|
186
|
+
"""
|
|
187
|
+
metadata = extract_metadata(upload, max_pixels=max_pixels)
|
|
188
|
+
if metadata is None or not metadata.needs_rotation:
|
|
189
|
+
return metadata
|
|
190
|
+
stream = upload.file
|
|
191
|
+
encoded = io.BytesIO()
|
|
192
|
+
try:
|
|
193
|
+
with _open_image(stream, max_pixels=max_pixels) as image:
|
|
194
|
+
rotated = auto_rotate(image)
|
|
195
|
+
save_kwargs: dict[str, Any] = {}
|
|
196
|
+
if rotated.info.get("exif"):
|
|
197
|
+
save_kwargs["exif"] = rotated.info["exif"]
|
|
198
|
+
rotated.save(encoded, format=metadata.format, **save_kwargs)
|
|
199
|
+
except _OPEN_ERRORS as exc:
|
|
200
|
+
logger.warning("Upload %r could not be auto-rotated: %s", upload.filename, exc)
|
|
201
|
+
_rewind(stream)
|
|
202
|
+
return metadata
|
|
203
|
+
stream.seek(0)
|
|
204
|
+
stream.truncate()
|
|
205
|
+
stream.write(encoded.getvalue())
|
|
206
|
+
stream.seek(0)
|
|
207
|
+
if hasattr(upload, "size"):
|
|
208
|
+
upload.size = len(encoded.getvalue())
|
|
209
|
+
return ImageMetadata(rotated.width, rotated.height, metadata.format)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def thumbnail_name(filename: str, max_size: tuple[int, int] = DEFAULT_THUMBNAIL_SIZE) -> str:
|
|
213
|
+
"""Return the storage-relative name of the thumbnail for ``filename``.
|
|
214
|
+
|
|
215
|
+
``photo.jpg`` at ``(100, 100)`` becomes ``.thumbs/photo_100x100.jpg``; a
|
|
216
|
+
nested ``gallery/photo.jpg`` keeps its directory: ``gallery/.thumbs/...``.
|
|
217
|
+
"""
|
|
218
|
+
original = PurePosixPath(filename)
|
|
219
|
+
width, height = max_size
|
|
220
|
+
return str(
|
|
221
|
+
original.parent / THUMBNAIL_DIR / f"{original.stem}_{width}x{height}{original.suffix}"
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def generate_thumbnail(
|
|
226
|
+
storage: Any,
|
|
227
|
+
filename: str,
|
|
228
|
+
max_size: tuple[int, int] = DEFAULT_THUMBNAIL_SIZE,
|
|
229
|
+
*,
|
|
230
|
+
max_pixels: int = MAX_IMAGE_PIXELS,
|
|
231
|
+
placeholder: str = PLACEHOLDER_THUMBNAIL,
|
|
232
|
+
) -> str:
|
|
233
|
+
"""Return a thumbnail of ``filename`` that fits inside ``max_size``, making it once.
|
|
234
|
+
|
|
235
|
+
The thumbnail is written under :data:`THUMBNAIL_DIR` next to the original,
|
|
236
|
+
auto-rotated, with its aspect ratio preserved and LANCZOS resampling. A
|
|
237
|
+
cached thumbnail is reused as long as it is not older than the original.
|
|
238
|
+
|
|
239
|
+
Args:
|
|
240
|
+
storage: A ``FileSystemStorage`` (or compatible object exposing
|
|
241
|
+
``get_path`` and a ``_path``/``path`` root).
|
|
242
|
+
filename: The stored name of the original, as held by the file column.
|
|
243
|
+
max_size: Bounding box ``(width, height)`` in pixels.
|
|
244
|
+
max_pixels: Decompression-bomb threshold for the original.
|
|
245
|
+
placeholder: Value returned when no thumbnail can be produced. The
|
|
246
|
+
default is a static icon path, not a storage name.
|
|
247
|
+
|
|
248
|
+
Returns:
|
|
249
|
+
The storage-relative thumbnail name, or ``placeholder`` when the
|
|
250
|
+
original is missing, outside the storage root, not an image, or too large.
|
|
251
|
+
"""
|
|
252
|
+
source = resolve_storage_path(storage, filename)
|
|
253
|
+
if source is None or not source.is_file():
|
|
254
|
+
return placeholder
|
|
255
|
+
name = thumbnail_name(filename, max_size)
|
|
256
|
+
target = resolve_storage_path(storage, name)
|
|
257
|
+
if target is None:
|
|
258
|
+
return placeholder
|
|
259
|
+
if target.is_file() and target.stat().st_mtime >= source.stat().st_mtime:
|
|
260
|
+
return name
|
|
261
|
+
encoded = io.BytesIO()
|
|
262
|
+
try:
|
|
263
|
+
with _open_image(source, max_pixels=max_pixels) as image:
|
|
264
|
+
thumb = auto_rotate(image)
|
|
265
|
+
thumb.thumbnail(max_size, Image.Resampling.LANCZOS)
|
|
266
|
+
thumb.save(encoded, format=image.format)
|
|
267
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
268
|
+
target.write_bytes(encoded.getvalue())
|
|
269
|
+
except _THUMBNAIL_ERRORS as exc:
|
|
270
|
+
logger.warning("Thumbnail for %r could not be generated: %s", filename, exc)
|
|
271
|
+
return placeholder
|
|
272
|
+
return name
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""S3-compatible storage backend for upload fields (epic #410, story #402).
|
|
2
|
+
|
|
3
|
+
:class:`S3StorageBackend` is a ``fastapi_storages.S3Storage`` configured from
|
|
4
|
+
keyword arguments or :class:`~hyperadmin.core.settings.HyperAdminSettings`
|
|
5
|
+
instead of process-wide environment variables read at import time, and works
|
|
6
|
+
with any S3-compatible service: AWS S3, Cloudflare R2, MinIO, Hetzner Object
|
|
7
|
+
Storage, DigitalOcean Spaces.
|
|
8
|
+
|
|
9
|
+
It implements every storage operation itself over a boto3 S3 *client*, so it
|
|
10
|
+
behaves the same on every supported ``fastapi-storages`` release (0.1.0 still
|
|
11
|
+
builds a ``boto3.resource`` and has no ``delete``; later releases differ again).
|
|
12
|
+
|
|
13
|
+
Requires the optional extra: ``pip install 'hyper-admin[s3]'``. ``boto3`` is
|
|
14
|
+
imported lazily, so importing this module never fails; building a backend
|
|
15
|
+
without it raises an ``ImportError`` that names the extra to install.
|
|
16
|
+
|
|
17
|
+
Objects live behind a URL rather than on local disk, so the admin's file views
|
|
18
|
+
redirect downloads to :meth:`S3StorageBackend.get_path` (a presigned URL by
|
|
19
|
+
default) and delete objects through the client; see :func:`is_remote_storage`.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import logging
|
|
25
|
+
import mimetypes
|
|
26
|
+
from pathlib import PurePosixPath
|
|
27
|
+
from typing import TYPE_CHECKING, Any
|
|
28
|
+
from urllib.parse import urlsplit
|
|
29
|
+
|
|
30
|
+
from fastapi_storages.s3 import S3Storage
|
|
31
|
+
from fastapi_storages.utils import secure_filename
|
|
32
|
+
|
|
33
|
+
if TYPE_CHECKING:
|
|
34
|
+
from typing import BinaryIO
|
|
35
|
+
|
|
36
|
+
from hyperadmin.core.settings import HyperAdminSettings
|
|
37
|
+
|
|
38
|
+
logger = logging.getLogger(__name__)
|
|
39
|
+
|
|
40
|
+
_S3_EXTRA_HINT = (
|
|
41
|
+
"S3StorageBackend requires the 'boto3' package. Install it with: pip install 'hyper-admin[s3]'"
|
|
42
|
+
)
|
|
43
|
+
_DEFAULT_CONTENT_TYPE = "application/octet-stream"
|
|
44
|
+
|
|
45
|
+
#: Object-name segments that would leave the bucket prefix on a path-style URL.
|
|
46
|
+
_UNSAFE_SEGMENTS = frozenset({"", ".", ".."})
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _s3_client(**kwargs: Any) -> Any:
|
|
50
|
+
"""Build a ``boto3`` S3 client, naming the extra to install when it is missing."""
|
|
51
|
+
try:
|
|
52
|
+
import boto3 # noqa: PLC0415 - optional extra, imported lazily
|
|
53
|
+
except ImportError as exc:
|
|
54
|
+
raise ImportError(_S3_EXTRA_HINT) from exc
|
|
55
|
+
return boto3.client("s3", **kwargs)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _split_endpoint(endpoint_url: str | None) -> tuple[str, str | None]:
|
|
59
|
+
"""Return ``(scheme, host)`` for an endpoint URL; ``("https", None)`` when unset.
|
|
60
|
+
|
|
61
|
+
A bare ``host:port`` is taken as https. The host keeps any port and path
|
|
62
|
+
(``https://minio.local:9000``, ``http://localhost:9000``), without the
|
|
63
|
+
trailing slash.
|
|
64
|
+
"""
|
|
65
|
+
if not endpoint_url:
|
|
66
|
+
return "https", None
|
|
67
|
+
raw = endpoint_url if "://" in endpoint_url else f"https://{endpoint_url}"
|
|
68
|
+
parts = urlsplit(raw)
|
|
69
|
+
if parts.scheme not in {"http", "https"} or not parts.netloc:
|
|
70
|
+
msg = f"Invalid S3 endpoint URL {endpoint_url!r}: expected http(s)://host[:port]"
|
|
71
|
+
raise ValueError(msg)
|
|
72
|
+
return parts.scheme, parts.netloc + parts.path.rstrip("/")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class S3StorageBackend(S3Storage):
|
|
76
|
+
"""``S3Storage`` configured per instance, for AWS S3 and S3-compatible services.
|
|
77
|
+
|
|
78
|
+
``fastapi_storages.S3Storage`` reads its configuration from ``AWS_*``
|
|
79
|
+
environment variables when the module is imported. This subclass takes the
|
|
80
|
+
same configuration as keyword arguments (or from settings via
|
|
81
|
+
:meth:`from_settings`), so two backends can point at different buckets and
|
|
82
|
+
nothing is read at import time.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
bucket_name: The bucket objects are written to.
|
|
86
|
+
access_key_id: Static credentials. When unset, boto3's own credential
|
|
87
|
+
chain applies (profiles, instance and task roles, ``AWS_*`` env).
|
|
88
|
+
secret_access_key: See ``access_key_id``.
|
|
89
|
+
region: The region name (``"eu-central-1"``). Optional for services that
|
|
90
|
+
ignore it (MinIO) but required by AWS without a profile default.
|
|
91
|
+
endpoint_url: ``http(s)://host[:port]`` of an S3-compatible service
|
|
92
|
+
(MinIO, R2, Hetzner, Spaces). Unset for AWS S3. ``http://`` turns SSL
|
|
93
|
+
off for that endpoint.
|
|
94
|
+
custom_domain: Serve object URLs from this host instead of the endpoint
|
|
95
|
+
(a CDN or an R2 public bucket domain). URLs are then not signed.
|
|
96
|
+
querystring_auth: Sign object URLs (presigned, time-limited; default).
|
|
97
|
+
Set ``False`` only for a public-read bucket.
|
|
98
|
+
default_acl: ACL set on every written object (``"public-read"``).
|
|
99
|
+
Unset (default) keeps objects private and sends no ACL, which
|
|
100
|
+
services without ACL support (R2) require.
|
|
101
|
+
client: An existing boto3 S3 client to use instead of building one
|
|
102
|
+
(tests, custom sessions). The other connection arguments are then
|
|
103
|
+
only used to build URLs.
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
def __init__( # noqa: PLR0913 - one keyword per S3 connection setting
|
|
107
|
+
self,
|
|
108
|
+
*,
|
|
109
|
+
bucket_name: str,
|
|
110
|
+
access_key_id: str | None = None,
|
|
111
|
+
secret_access_key: str | None = None,
|
|
112
|
+
region: str | None = None,
|
|
113
|
+
endpoint_url: str | None = None,
|
|
114
|
+
custom_domain: str | None = None,
|
|
115
|
+
querystring_auth: bool = True,
|
|
116
|
+
default_acl: str | None = None,
|
|
117
|
+
client: Any = None,
|
|
118
|
+
) -> None:
|
|
119
|
+
if not bucket_name:
|
|
120
|
+
msg = "S3StorageBackend requires a bucket name"
|
|
121
|
+
raise ValueError(msg)
|
|
122
|
+
scheme, host = _split_endpoint(endpoint_url)
|
|
123
|
+
self.bucket_name = bucket_name
|
|
124
|
+
self.region = region
|
|
125
|
+
self.custom_domain = custom_domain
|
|
126
|
+
self.querystring_auth = querystring_auth
|
|
127
|
+
self.default_acl = default_acl
|
|
128
|
+
self._http_scheme = scheme
|
|
129
|
+
endpoint = f"{scheme}://{host}" if host else None
|
|
130
|
+
if client is None:
|
|
131
|
+
client = _s3_client(
|
|
132
|
+
**_client_kwargs(
|
|
133
|
+
use_ssl=scheme == "https",
|
|
134
|
+
endpoint=endpoint,
|
|
135
|
+
region=region,
|
|
136
|
+
access_key_id=access_key_id,
|
|
137
|
+
secret_access_key=secret_access_key,
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
self._s3 = client
|
|
141
|
+
self._url = endpoint or str(client.meta.endpoint_url)
|
|
142
|
+
|
|
143
|
+
@classmethod
|
|
144
|
+
def from_settings(cls, settings: HyperAdminSettings, *, client: Any = None) -> S3StorageBackend:
|
|
145
|
+
"""Build a backend from the ``aws_*`` settings (``HYPERADMIN_AWS_*`` / ``AWS_*`` env).
|
|
146
|
+
|
|
147
|
+
Raises:
|
|
148
|
+
ValueError: ``aws_s3_bucket_name`` is unset.
|
|
149
|
+
"""
|
|
150
|
+
if not settings.aws_s3_bucket_name:
|
|
151
|
+
msg = (
|
|
152
|
+
"S3 storage requires a bucket: set aws_s3_bucket_name "
|
|
153
|
+
"(HYPERADMIN_AWS_S3_BUCKET_NAME or AWS_S3_BUCKET_NAME)"
|
|
154
|
+
)
|
|
155
|
+
raise ValueError(msg)
|
|
156
|
+
secret = settings.aws_secret_access_key
|
|
157
|
+
return cls(
|
|
158
|
+
bucket_name=settings.aws_s3_bucket_name,
|
|
159
|
+
access_key_id=settings.aws_access_key_id,
|
|
160
|
+
secret_access_key=secret.get_secret_value() if secret is not None else None,
|
|
161
|
+
region=settings.aws_s3_region,
|
|
162
|
+
endpoint_url=settings.aws_s3_endpoint_url,
|
|
163
|
+
custom_domain=settings.aws_s3_custom_domain,
|
|
164
|
+
querystring_auth=settings.aws_s3_querystring_auth,
|
|
165
|
+
default_acl=settings.aws_s3_default_acl,
|
|
166
|
+
client=client,
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
def get_name(self, name: str) -> str:
|
|
170
|
+
"""Return the object key for ``name``: the basename sanitised, directories kept."""
|
|
171
|
+
path = PurePosixPath(name.replace("\\", "/"))
|
|
172
|
+
safe = secure_filename(path.name)
|
|
173
|
+
return str(path.parent / safe) if safe else name
|
|
174
|
+
|
|
175
|
+
def get_path(self, name: str) -> str:
|
|
176
|
+
"""Return the URL of the object ``name``.
|
|
177
|
+
|
|
178
|
+
Served from ``custom_domain`` when set, otherwise presigned through the
|
|
179
|
+
client when ``querystring_auth`` is on (default), otherwise the plain
|
|
180
|
+
path-style URL under the endpoint.
|
|
181
|
+
"""
|
|
182
|
+
key = self.get_name(name)
|
|
183
|
+
if self.custom_domain:
|
|
184
|
+
return f"{self._http_scheme}://{self.custom_domain}/{key}"
|
|
185
|
+
if self.querystring_auth:
|
|
186
|
+
params = {"Bucket": self.bucket_name, "Key": key}
|
|
187
|
+
return str(self._s3.generate_presigned_url("get_object", Params=params))
|
|
188
|
+
return f"{self._url}/{self.bucket_name}/{key}"
|
|
189
|
+
|
|
190
|
+
def get_size(self, name: str) -> int:
|
|
191
|
+
"""Return the object size in bytes (one ``HeadObject`` call)."""
|
|
192
|
+
response = self._s3.head_object(Bucket=self.bucket_name, Key=self.get_name(name))
|
|
193
|
+
return int(response["ContentLength"])
|
|
194
|
+
|
|
195
|
+
def write(self, file: BinaryIO, name: str) -> str:
|
|
196
|
+
"""Upload ``file`` as ``name`` and return the object key.
|
|
197
|
+
|
|
198
|
+
No ``ACL`` parameter is sent when ``default_acl`` is unset: boto3
|
|
199
|
+
rejects ``ACL=None``, and services such as R2 reject any ACL at all.
|
|
200
|
+
"""
|
|
201
|
+
file.seek(0, 0)
|
|
202
|
+
key = self.get_name(name)
|
|
203
|
+
content_type, _ = mimetypes.guess_type(key)
|
|
204
|
+
params: dict[str, Any] = {"ContentType": content_type or _DEFAULT_CONTENT_TYPE}
|
|
205
|
+
if self.default_acl:
|
|
206
|
+
params["ACL"] = self.default_acl
|
|
207
|
+
self._s3.upload_fileobj(file, self.bucket_name, key, ExtraArgs=params)
|
|
208
|
+
return key
|
|
209
|
+
|
|
210
|
+
def delete(self, name: str) -> None:
|
|
211
|
+
"""Delete the object ``name`` from the bucket."""
|
|
212
|
+
self._s3.delete_object(Bucket=self.bucket_name, Key=self.get_name(name))
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _client_kwargs(
|
|
216
|
+
*,
|
|
217
|
+
use_ssl: bool,
|
|
218
|
+
endpoint: str | None,
|
|
219
|
+
region: str | None,
|
|
220
|
+
access_key_id: str | None,
|
|
221
|
+
secret_access_key: str | None,
|
|
222
|
+
) -> dict[str, Any]:
|
|
223
|
+
"""Return the ``boto3.client("s3", ...)`` arguments; unset values are left to boto3."""
|
|
224
|
+
kwargs: dict[str, Any] = {"use_ssl": use_ssl}
|
|
225
|
+
if endpoint:
|
|
226
|
+
kwargs["endpoint_url"] = endpoint
|
|
227
|
+
if region:
|
|
228
|
+
kwargs["region_name"] = region
|
|
229
|
+
if access_key_id:
|
|
230
|
+
kwargs["aws_access_key_id"] = access_key_id
|
|
231
|
+
if secret_access_key:
|
|
232
|
+
kwargs["aws_secret_access_key"] = secret_access_key
|
|
233
|
+
return kwargs
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def is_remote_storage(storage: Any) -> bool:
|
|
237
|
+
"""Return ``True`` when ``storage`` keeps objects behind a URL rather than on disk.
|
|
238
|
+
|
|
239
|
+
True for any ``fastapi_storages.S3Storage`` (``S3StorageBackend`` included).
|
|
240
|
+
The admin then redirects downloads to the object URL and deletes through
|
|
241
|
+
the storage instead of resolving paths inside a local root.
|
|
242
|
+
"""
|
|
243
|
+
return isinstance(storage, S3Storage)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def remote_file_name(value: Any) -> str | None:
|
|
247
|
+
"""Return the object name a file column value holds, or ``None`` when unusable.
|
|
248
|
+
|
|
249
|
+
A value written by a user into the database must never address an object
|
|
250
|
+
outside the bucket: on a path-style URL a ``..`` segment would leave the
|
|
251
|
+
bucket prefix, so names with empty, ``.`` or ``..`` segments are refused.
|
|
252
|
+
"""
|
|
253
|
+
if not value:
|
|
254
|
+
return None
|
|
255
|
+
name = str(value.name if hasattr(value, "name") else value)
|
|
256
|
+
if any(part in _UNSAFE_SEGMENTS for part in name.replace("\\", "/").split("/")):
|
|
257
|
+
logger.warning("Refusing object name with empty or traversal segments: %r", name)
|
|
258
|
+
return None
|
|
259
|
+
return name
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def remote_file_url(storage: Any, value: Any) -> str | None:
|
|
263
|
+
"""Return the URL ``storage`` serves the object held by ``value`` at, or ``None``."""
|
|
264
|
+
name = remote_file_name(value)
|
|
265
|
+
return None if name is None else str(storage.get_path(name))
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Upload file validation utilities."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from starlette.datastructures import UploadFile
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FileValidationError(ValueError):
|
|
12
|
+
"""Raised when an uploaded file fails validation checks."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def validate_upload(
|
|
16
|
+
file: UploadFile,
|
|
17
|
+
*,
|
|
18
|
+
allowed_types: list[str] | None = None,
|
|
19
|
+
max_size: int | None = None,
|
|
20
|
+
) -> None:
|
|
21
|
+
"""Validate an uploaded file against size and MIME type constraints.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
file: The uploaded file to validate.
|
|
25
|
+
allowed_types: Allowed MIME types (e.g. ``["image/jpeg", "image/png"]``).
|
|
26
|
+
When ``None``, all types are accepted.
|
|
27
|
+
max_size: Maximum file size in bytes. When ``None``, no limit.
|
|
28
|
+
|
|
29
|
+
Raises:
|
|
30
|
+
FileValidationError: If validation fails.
|
|
31
|
+
"""
|
|
32
|
+
if max_size is not None and file.size is not None and file.size > max_size:
|
|
33
|
+
raise FileValidationError(f"File size {file.size} bytes exceeds limit of {max_size} bytes")
|
|
34
|
+
if allowed_types is not None and file.content_type not in allowed_types:
|
|
35
|
+
raise FileValidationError(
|
|
36
|
+
f"File type '{file.content_type}' is not allowed. Accepted: {', '.join(allowed_types)}"
|
|
37
|
+
)
|