mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
fix(i18n): refusals and checks users read are translated
English-only texts users read, now in their language: - the serial console's four refusals, which socketiod sends in the language LocaleMiddleware would choose (cookie, then browser) and logs in English; the texts live in console/refusals.py, as makemessages reads only .py files; - a submission key reused for another request, a malformed MAC address, the moved-VM refusal of a script's request, the API's "Templates cannot be started" (now the page's sentence), "Pool is not active" and the ISO upload's checks; - "Problem occurred with host" was a fragment with the host's name and error after it: one sentence with both.
This commit is contained in:
parent
bf6a0c0a29
commit
0dda091cd5
14 changed files with 187 additions and 41 deletions
9
console/refusals.py
Normal file
9
console/refusals.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
"""socketiod's refusals, which its page shows in the user's language. Here,
|
||||
as makemessages reads only .py files and socketiod has no extension."""
|
||||
|
||||
from django.utils.translation import gettext_noop
|
||||
|
||||
TURNED_OFF = gettext_noop("The serial console is turned off on this server")
|
||||
NOT_ALLOWED = gettext_noop("The console link has expired, or you may no longer open this console: reload the page")
|
||||
OPEN_ELSEWHERE = gettext_noop("The console of this VM is open in another window")
|
||||
TOO_MANY = gettext_noop("Too many consoles are open on this server: try again later")
|
||||
|
|
@ -30,7 +30,11 @@ import socketio # noqa: E402
|
|||
from gunicorn.app.base import BaseApplication # noqa: E402
|
||||
from django import db # noqa: E402
|
||||
from django.conf import settings # noqa: E402
|
||||
from django.http import HttpRequest # noqa: E402
|
||||
from django.http.cookie import parse_cookie # noqa: E402
|
||||
from django.utils import translation # noqa: E402
|
||||
|
||||
from console import refusals # noqa: E402
|
||||
from console.tokens import authorize, session_user_id # noqa: E402
|
||||
|
||||
CONSOLECALLBACK = os.path.join(ROOT_PATH, "conf", "daemon", "consolecallback")
|
||||
|
|
@ -49,30 +53,34 @@ sessions = {}
|
|||
sessions_lock = threading.Lock()
|
||||
|
||||
|
||||
def refused(reason):
|
||||
"""The refusal the page shows; it is logged too."""
|
||||
def refused(environ, reason):
|
||||
"""The refusal the page shows, in its language (LocaleMiddleware's choice:
|
||||
the language cookie, else the browser's); it is logged too, in English."""
|
||||
logger.info("serial console refused: %s", reason)
|
||||
return socketio.exceptions.ConnectionRefusedError(reason)
|
||||
request = HttpRequest()
|
||||
request.COOKIES, request.META = parse_cookie(environ.get("HTTP_COOKIE", "")), environ
|
||||
with translation.override(translation.get_language_from_request(request)):
|
||||
return socketio.exceptions.ConnectionRefusedError(translation.gettext(reason))
|
||||
|
||||
|
||||
@sio.event
|
||||
def connect(sid, environ, auth):
|
||||
# the page shows the reason
|
||||
if not settings.SERIAL_CONSOLE_ENABLED:
|
||||
raise refused("The serial console is turned off on this server")
|
||||
raise refused(environ, refusals.TURNED_OFF)
|
||||
try:
|
||||
instance, user = authorize((auth or {}).get("token"), "pty")
|
||||
except PermissionError:
|
||||
raise refused("The console link has expired, or you may no longer open this console: reload the page")
|
||||
raise refused(environ, refusals.NOT_ALLOWED)
|
||||
finally:
|
||||
db.close_old_connections()
|
||||
taken = None
|
||||
with sessions_lock:
|
||||
open_sid = next((s for s, session in sessions.items() if session["vm"] == instance.pk), None)
|
||||
if open_sid is not None and not user.is_superuser:
|
||||
raise refused("The console of this VM is open in another window")
|
||||
raise refused(environ, refusals.OPEN_ELSEWHERE)
|
||||
if open_sid is None and len(sessions) >= MAX_SESSIONS:
|
||||
raise refused("Too many consoles are open on this server: try again later")
|
||||
raise refused(environ, refusals.TOO_MANY)
|
||||
if open_sid is not None: # a superuser takes over a console left open
|
||||
taken = open_sid, sessions.pop(open_sid)
|
||||
# the page tells its user who took the console; first, as its
|
||||
|
|
|
|||
|
|
@ -116,6 +116,18 @@ class SocketiodTestCase(Base):
|
|||
self.assertIn("Too many", self.refused("over", {"token": self.token(vm=other)}))
|
||||
self.assertEqual(list(self.d.sessions), ["first"])
|
||||
|
||||
@override_settings(SERIAL_CONSOLE_ENABLED=False)
|
||||
def test_a_refusal_is_in_the_page_s_language_and_logged_in_english(self):
|
||||
# as LocaleMiddleware: the language cookie first, then the browser's
|
||||
for environ, shown in (({"HTTP_COOKIE": "django_language=tr", "HTTP_ACCEPT_LANGUAGE": "zh-hans"}, "Seri konsol bu sunucuda kapalı"),
|
||||
({"HTTP_ACCEPT_LANGUAGE": "zh-CN,zh;q=0.9"}, "此服务器已关闭串行控制台"),
|
||||
({}, "The serial console is turned off on this server")):
|
||||
with self.subTest(shown), self.assertRaises(self.d.socketio.exceptions.ConnectionRefusedError) as refusal, \
|
||||
self.assertLogs("socketiod", level="INFO") as log:
|
||||
self.d.connect("sid", environ, {"token": self.token()})
|
||||
self.assertEqual(refusal.exception.error_args["message"], shown)
|
||||
self.assertIn("refused: The serial console is turned off on this server", log.output[0])
|
||||
|
||||
@override_settings(SERIAL_CONSOLE_ENABLED=True)
|
||||
def test_input_and_resize_go_to_the_session_and_are_checked(self):
|
||||
self.d.connect("sid-1", {}, {"token": self.token()})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue