parent
a08fca4c60
commit
ebaeeaaf13
@ -0,0 +1,71 @@ |
||||
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'] |
@ -0,0 +1,76 @@ |
||||
from threading import Thread |
||||
|
||||
from asciimatics.widgets import Layout, Label, Text, TextBox, Widget |
||||
|
||||
from ...utils import defer, http_patch, http_get |
||||
from ..CeoFrame import CeoFrame |
||||
|
||||
|
||||
class SetForwardingAddressesView(CeoFrame): |
||||
def __init__(self, screen, width, height, model): |
||||
super().__init__( |
||||
screen, height, width, model, 'SetForwardingAddresses', |
||||
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._forwarding_addresses = TextBox( |
||||
Widget.FILL_FRAME, 'Forwarding addresses:', 'forwarding_addresses', |
||||
line_wrap=True) |
||||
layout.add_widget(self._forwarding_addresses) |
||||
layout.add_widget(Label('Press <TAB> to switch widgets')) |
||||
|
||||
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() |
||||
if 'forwarding_addresses' not in data: |
||||
return |
||||
self._forwarding_addresses.value = data['forwarding_addresses'] |
||||
finally: |
||||
self.clear_flash_message() |
||||
|
||||
def _next(self): |
||||
uid = self._username.value |
||||
forwarding_addresses = self._forwarding_addresses.value |
||||
body = {'forwarding_addresses': forwarding_addresses} |
||||
self._model.deferred_req = defer( |
||||
http_patch, f'/api/members/{uid}', json=body) |
||||
self._model.confirm_lines = [ |
||||
f"{uid}'s forwarding addresses will be set to:", |
||||
'', |
||||
*forwarding_addresses, |
||||
'', |
||||
'Are you sure you want to continue?', |
||||
] |
||||
self._model.operations = ['replace_forwarding_addresses'] |
Loading…
Reference in new issue