fix: restore dev root file helpers

This commit is contained in:
2026-04-14 13:33:10 +03:00
parent 75302a563a
commit 19bf4fb293

View File

@@ -62,7 +62,43 @@ handler403 = 'web.views.handler403'
handler500 = 'web.views.handler500'
if settings.DEBUG:
import mimetypes
import debug_toolbar
from django.views.static import serve
def _serve_public_root_file(request, path):
"""Отдаёт файлы из корня `public` в dev-режиме в utf-8."""
response = serve(request, path, document_root=settings.PUBLIC_DIR)
content_type, _ = mimetypes.guess_type(path)
if content_type:
if content_type.startswith('text/'):
response['Content-Type'] = f'{content_type}; charset=utf-8'
else:
response['Content-Type'] = content_type
elif path.endswith('.txt'):
response['Content-Type'] = 'text/plain; charset=utf-8'
elif path.endswith('.html'):
response['Content-Type'] = 'text/html; charset=utf-8'
return response
def _iter_public_root_files():
"""Находит все обычные файлы в корне `public`, кроме служебных артефактов."""
for file_path in sorted(settings.PUBLIC_DIR.iterdir()):
if not file_path.is_file():
continue
if file_path.name.startswith('.'):
continue
if file_path.name == 'README.md':
continue
yield file_path.name
PUBLIC_ROOT_URLPATTERNS = [
path(filename, _serve_public_root_file, {'path': filename})
for filename in _iter_public_root_files()
]
urlpatterns = [path('__debug__/', include(debug_toolbar.urls)), ] + urlpatterns
urlpatterns = [*PUBLIC_ROOT_URLPATTERNS, *urlpatterns]
urlpatterns += static(settings.STATIC_URL, document_root=settings.PUBLIC_DIR.joinpath('static'))
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)