2021-09-06 12:40:05 -04:00
|
|
|
from asciimatics.exceptions import NextScene
|
2021-09-06 16:16:45 -04:00
|
|
|
from asciimatics.widgets import Layout, Label
|
2021-09-06 12:40:05 -04:00
|
|
|
import requests
|
|
|
|
|
|
|
|
from .CeoFrame import CeoFrame
|
|
|
|
|
|
|
|
|
|
|
|
class ResultView(CeoFrame):
|
|
|
|
def __init__(self, screen, width, height, model):
|
|
|
|
super().__init__(
|
|
|
|
screen, height, width, model, 'Result',
|
|
|
|
on_load=self._resultview_on_load, title='Result',
|
2021-09-06 22:29:53 -04:00
|
|
|
has_dynamic_layouts=True,
|
2021-09-08 00:10:21 -04:00
|
|
|
escape_on_q=True,
|
2021-09-06 12:40:05 -04:00
|
|
|
)
|
|
|
|
|
2021-09-06 22:29:53 -04:00
|
|
|
# TODO: deduplicate this from ConfirmView
|
|
|
|
def _add_text(self, text: str = '\n', center: bool = False):
|
|
|
|
if center:
|
|
|
|
layout = Layout([100])
|
|
|
|
align = '^'
|
|
|
|
col = 0
|
|
|
|
else:
|
|
|
|
layout = Layout([1, 10])
|
|
|
|
align = '<'
|
|
|
|
col = 1
|
|
|
|
self.add_layout(layout)
|
2021-09-06 12:40:05 -04:00
|
|
|
for line in text.splitlines():
|
2021-09-06 22:29:53 -04:00
|
|
|
layout.add_widget(Label(line, align=align), col)
|
|
|
|
|
|
|
|
def _add_pair(self, key: str, val: str):
|
|
|
|
layout = Layout([10, 1, 10])
|
|
|
|
self.add_layout(layout)
|
2021-09-07 01:22:20 -04:00
|
|
|
if key:
|
|
|
|
layout.add_widget(Label(key + ':', align='>'), 0)
|
|
|
|
else:
|
|
|
|
layout.add_widget(Label(''), 0)
|
2021-09-06 22:29:53 -04:00
|
|
|
layout.add_widget(Label(val, align='<'), 2)
|
2021-09-06 12:40:05 -04:00
|
|
|
|
|
|
|
# override this method in child classes if desired
|
|
|
|
def show_result(self, resp: requests.Response):
|
2021-09-06 22:29:53 -04:00
|
|
|
self._add_text('The operation was successfully performed.', center=True)
|
2021-09-06 12:40:05 -04:00
|
|
|
|
|
|
|
def _resultview_on_load(self):
|
2021-09-06 22:29:53 -04:00
|
|
|
self._add_text()
|
|
|
|
resp = self._model.resp
|
|
|
|
if resp.status_code != 200:
|
|
|
|
self._add_text('An error occurred:')
|
2021-09-07 22:59:56 -04:00
|
|
|
if resp.headers.get('content-type') == 'application/json':
|
|
|
|
err_msg = resp.json()['error']
|
|
|
|
else:
|
|
|
|
err_msg = resp.text.rstrip()
|
|
|
|
self._add_text(err_msg)
|
2021-09-06 22:29:53 -04:00
|
|
|
else:
|
2021-09-06 16:16:45 -04:00
|
|
|
self.show_result(resp)
|
2021-09-06 22:29:53 -04:00
|
|
|
# fill the rest of the space
|
|
|
|
self.add_layout(Layout([100], fill_frame=True))
|
|
|
|
self.add_buttons(on_next_excl=self._next)
|
|
|
|
self.fix()
|
|
|
|
|
|
|
|
def _next(self):
|
|
|
|
self._model.reset()
|
|
|
|
raise NextScene('Welcome')
|