72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
from threading import Thread
|
|
|
|
from asciimatics.widgets import Layout, Text
|
|
|
|
from ...utils import defer, http_patch, http_get
|
|
from ..CeoFrame import CeoFrame
|
|
|
|
|
|
class ChangeLoginShellView(CeoFrame):
|
|
def __init__(self, screen, width, height, model):
|
|
super().__init__(
|
|
screen, height, width, model, 'ChangeLoginShell',
|
|
save_data=True,
|
|
)
|
|
self._username_changed = False
|
|
|
|
layout = Layout([100], fill_frame=True)
|
|
self.add_layout(layout)
|
|
self._username = Text(
|
|
"Username:", "uid",
|
|
on_change=self._on_username_change,
|
|
on_blur=self._on_username_blur,
|
|
)
|
|
layout.add_widget(self._username)
|
|
self._login_shell = Text('Login shell:', 'login_shell')
|
|
layout.add_widget(self._login_shell)
|
|
|
|
self.add_flash_message_layout()
|
|
self.add_buttons(
|
|
back_btn=True,
|
|
next_scene='Confirm', on_next=self._next)
|
|
self.fix()
|
|
|
|
# TODO: deduplicate this from AddUserView
|
|
def _on_username_change(self):
|
|
self._username_changed = True
|
|
|
|
def _on_username_blur(self):
|
|
if not self._username_changed:
|
|
return
|
|
self._username_changed = False
|
|
username = self._username.value
|
|
if username == '':
|
|
return
|
|
Thread(target=self._get_user_info, args=[username]).start()
|
|
|
|
def _get_user_info(self, username):
|
|
self.flash_message('Looking up user...')
|
|
try:
|
|
resp = http_get('/api/members/' + username)
|
|
if resp.status_code != 200:
|
|
return
|
|
data = resp.json()
|
|
self._login_shell.value = data['login_shell']
|
|
finally:
|
|
self.clear_flash_message()
|
|
|
|
def _next(self):
|
|
uid = self._username.value
|
|
login_shell = self._login_shell.value
|
|
body = {'login_shell': login_shell}
|
|
self._model.deferred_req = defer(
|
|
http_patch, f'/api/members/{uid}', json=body)
|
|
self._model.confirm_lines = [
|
|
f"{uid}'s login shell will be changed to:",
|
|
'',
|
|
login_shell,
|
|
'',
|
|
'Are you sure you want to continue?',
|
|
]
|
|
self._model.operations = ['replace_login_shell']
|