zulip/zproject/dev_urls.py
Alex Vandiver 536bd3188e middleware: Move locale-setting before domain checking.
Calling `render()` in a middleware before LocaleMiddleware has run
will pick up the most-recently-set locale.  This may be from the
_previous_ request, since the current language is thread-local.  This
results in the "Organization does not exist" page occasionally being
in not-English, depending on the preferences of the request which that
thread just finished serving.

Move HostDomainMiddleware below LocaleMiddleware; none of the earlier
middlewares call `render()`, so are safe.  This will also allow the
"Organization does not exist" page to be localized based on the user's
browser preferences.

Unfortunately, it also means that the default LocaleMiddleware catches
the 404 from the HostDomainMiddlware and helpfully tries to check if
the failure is because the URL lacks a language component (e.g.
`/en/`) by turning it into a 304 to that new URL.  We must subclass
the default LocaleMiddleware to remove this unwanted functionality.

Doing so exposes a two places in tests that relied (directly or
indirectly) upon the redirection: '/confirmation_key'
was redirected to '/en/confirmation_key', since the non-i18n version
did not exist; and requests to `/stats/realm/not_existing_realm/`
incorrectly were expecting a 302, not a 404.

This regression likely came in during f00ff1ef62, since prior to
that, the HostDomainMiddleware ran _after_ the rest of the request had
completed.
2020-09-14 22:16:09 -07:00

94 lines
3.8 KiB
Python

import os
from urllib.parse import urlsplit
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.views import serve as staticfiles_serve
from django.http import HttpRequest, HttpResponse
from django.urls import path
from django.views.generic import TemplateView
from django.views.static import serve
import zerver.views.auth
import zerver.views.development.email_log
import zerver.views.development.integrations
import zerver.views.development.registration
# These URLs are available only in the development environment
use_prod_static = not settings.DEBUG
urls = [
# Serve useful development environment resources (docs, coverage reports, etc.)
path('coverage/<path:path>',
serve, {'document_root':
os.path.join(settings.DEPLOY_ROOT, 'var/coverage'),
'show_indexes': True}),
path('node-coverage/<path:path>',
serve, {'document_root':
os.path.join(settings.DEPLOY_ROOT, 'var/node-coverage/lcov-report'),
'show_indexes': True}),
path('docs/<path:path>',
serve, {'document_root':
os.path.join(settings.DEPLOY_ROOT, 'docs/_build/html')}),
# The special no-password login endpoint for development
path('devlogin/', zerver.views.auth.login_page,
{'template_name': 'zerver/dev_login.html'}, name='zerver.views.auth.login_page'),
# Page for testing email templates
path('emails/', zerver.views.development.email_log.email_page),
path('emails/generate/', zerver.views.development.email_log.generate_all_emails),
path('emails/clear/', zerver.views.development.email_log.clear_emails),
# Listing of useful URLs and various tools for development
path('devtools/', TemplateView.as_view(template_name='zerver/dev_tools.html')),
# Register New User and Realm
path('devtools/register_user/',
zerver.views.development.registration.register_development_user,
name='zerver.views.development.registration.register_development_user'),
path('devtools/register_realm/',
zerver.views.development.registration.register_development_realm,
name='zerver.views.development.registration.register_development_realm'),
# Have easy access for error pages
path('errors/404/', TemplateView.as_view(template_name='404.html')),
path('errors/5xx/', TemplateView.as_view(template_name='500.html')),
# Add a convenient way to generate webhook messages from fixtures.
path('devtools/integrations/', zerver.views.development.integrations.dev_panel),
path('devtools/integrations/check_send_webhook_fixture_message',
zerver.views.development.integrations.check_send_webhook_fixture_message),
path('devtools/integrations/send_all_webhook_fixture_messages',
zerver.views.development.integrations.send_all_webhook_fixture_messages),
path('devtools/integrations/<integration_name>/fixtures',
zerver.views.development.integrations.get_fixtures),
]
# Serve static assets via the Django server
if use_prod_static:
urls += [
path('static/<path:path>', serve, {'document_root': settings.STATIC_ROOT}),
]
else:
def serve_static(request: HttpRequest, path: str) -> HttpResponse:
response = staticfiles_serve(request, path)
response["Access-Control-Allow-Origin"] = "*"
return response
urls += static(urlsplit(settings.STATIC_URL).path, view=serve_static)
i18n_urls = [
path('confirmation_key/', zerver.views.development.registration.confirmation_key),
]
urls += i18n_urls
# On a production instance, these files would be served by nginx.
if settings.LOCAL_UPLOADS_DIR is not None:
avatars_url = path(
'user_avatars/<path:path>',
serve,
{'document_root': os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars")},
)
urls += [avatars_url]