implement AddMemberToGroupView

pull/20/head
Max Erenberg 2 years ago
parent beb16b1740
commit 21173d1b8c
  1. 4
      ceo/tui/Model.py
  2. 6
      ceo/tui/ResultView.py
  3. 11
      ceo/tui/TUIStreamResponseHandler.py
  4. 3
      ceo/tui/WelcomeView.py
  5. 43
      ceo/tui/groups/AddMemberToGroupView.py
  6. 2
      ceo/tui/groups/__init__.py
  7. 2
      ceo/tui/start.py
  8. 4
      ceod/transactions/groups/AddMemberToGroupTransaction.py

@ -51,6 +51,10 @@ class Model:
'GetGroup': {
'cn': '',
},
'AddMemberToGroup': {
'cn': '',
'uid': '',
},
}
self.viewdata = deepcopy(self._initial_viewdata)
# data which is shared between multiple views

@ -45,7 +45,11 @@ class ResultView(CeoFrame):
resp = self._model.resp
if resp.status_code != 200:
self._add_text('An error occurred:')
self._add_text(resp.text.rstrip())
if resp.headers.get('content-type') == 'application/json':
err_msg = resp.json()['error']
else:
err_msg = resp.text.rstrip()
self._add_text(err_msg)
else:
self.show_result(resp)
# fill the rest of the space

@ -31,7 +31,7 @@ class TUIStreamResponseHandler(StreamResponseHandler):
self.txn_view.fix()
self.screen.force_update()
def _show_msg(self, msg: str = ''):
def _show_msg(self, msg: str = '\n'):
for line in msg.splitlines():
self.msg_layout.add_widget(Label(line, align='^'))
@ -43,7 +43,11 @@ class TUIStreamResponseHandler(StreamResponseHandler):
def handle_non_200(self, resp: requests.Response):
self._abort()
self._show_msg('An error occurred:')
self._show_msg(resp.text)
if resp.headers.get('content-type') == 'application/json':
err_msg = resp.json()['error']
else:
err_msg = resp.text
self._show_msg(err_msg)
self._update()
def begin(self):
@ -52,8 +56,9 @@ class TUIStreamResponseHandler(StreamResponseHandler):
def handle_aborted(self, err_msg: str):
self._abort()
self._show_msg('The transaction was rolled back.')
self._show_msg('The error was:')
self._show_msg('The error was:\n')
self._show_msg(err_msg)
self._show_msg()
self._show_msg('Please check the ceod logs.')
self._update()

@ -1,6 +1,6 @@
import functools
from asciimatics.widgets import Frame, ListBox, Layout, Divider, Button
from asciimatics.widgets import Frame, ListBox, Layout, Divider, Button, Label
from asciimatics.exceptions import NextScene, StopApplication
@ -60,6 +60,7 @@ class WelcomeView(Frame):
layout = Layout([100])
self.add_layout(layout)
layout.add_widget(Label('Press <TAB> to switch widgets'))
layout.add_widget(Divider())
layout = Layout([1, 1, 1])

@ -0,0 +1,43 @@
from asciimatics.widgets import Layout, Text, CheckBox, Label
from ...utils import defer, http_post
from ..CeoFrame import CeoFrame
from ceod.transactions.groups import AddMemberToGroupTransaction
class AddMemberToGroupView(CeoFrame):
def __init__(self, screen, width, height, model):
super().__init__(
screen, height, width, model, 'AddMemberToGroup',
save_data=True,
)
layout = Layout([100], fill_frame=True)
self.add_layout(layout)
self._cn = Text('Group name:', 'cn')
layout.add_widget(self._cn)
self._username = Text('Username:', 'uid')
layout.add_widget(self._username)
layout.add_widget(Label(''))
self._checkbox = CheckBox('subscribe to auxiliary mailing lists')
self._checkbox.value = True
layout.add_widget(self._checkbox)
self.add_buttons(
back_btn=True,
next_scene='Confirm', on_next=self._next)
self.fix()
def _next(self):
cn = self._cn.value
uid = self._username.value
self._model.confirm_lines = [
f'Are you sure you want to add {uid} to {cn}?',
]
operations = AddMemberToGroupTransaction.operations
url = f'/api/groups/{cn}/members/{uid}'
# TODO: deduplicate this logic from the CLI
if not self._checkbox.value:
url += '?subscribe_to_lists=false'
operations.remove('subscribe_user_to_auxiliary_mailing_lists')
self._model.deferred_req = defer(http_post, url)
self._model.operations = operations

@ -1,2 +0,0 @@
# -*- coding: utf-8 -*-

@ -12,6 +12,7 @@ from .ResultView import ResultView
from .TransactionView import TransactionView
from .WelcomeView import WelcomeView
from .groups.AddGroupView import AddGroupView
from .groups.AddMemberToGroupView import AddMemberToGroupView
from .groups.GetGroupView import GetGroupView
from .groups.GetGroupResultView import GetGroupResultView
from .members.AddUserView import AddUserView
@ -61,6 +62,7 @@ def screen_wrapper(screen, last_scene, model):
('AddGroup', AddGroupView(screen, width, height, model)),
('GetGroup', GetGroupView(screen, width, height, model)),
('GetGroupResult', GetGroupResultView(screen, width, height, model)),
('AddMemberToGroup', AddMemberToGroupView(screen, width, height, model)),
]
scenes = [
Scene([view], -1, name=name) for name, view in views

@ -69,9 +69,9 @@ class AddMemberToGroupTransaction(AbstractTransaction):
yield 'subscribe_user_to_auxiliary_mailing_lists'
except KeyError:
pass
except Exception:
except Exception as err:
logger.error(traceback.format_exc())
yield 'failed_to_subscribe_user_to_auxiliary_mailing_lists'
yield 'failed_to_subscribe_user_to_auxiliary_mailing_lists: ' + str(err)
result = {
'added_to_groups': [self.group_name] + [

Loading…
Cancel
Save