console_conf/models: identify the current system

When creating a recovery systems model, find out which of the systems is
current.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
This commit is contained in:
Maciej Borzecki 2020-04-01 15:58:09 +02:00
parent 93ac6b94a3
commit d7e03cd5a8
2 changed files with 84 additions and 34 deletions

View File

@ -85,6 +85,12 @@ class RecoverySystemsModel:
self.systems = systems_data
# current selection
self._selection = None
self._current = None
# find which system is current one, but be robust if none is marked as
# such
cs = [s for s in systems_data if s.current]
if cs:
self._current = cs[0]
def select(self, system, action):
self._selection = SelectedSystemAction(system=system, action=action)
@ -93,6 +99,41 @@ class RecoverySystemsModel:
def selection(self):
return self._selection
@property
def current(self):
return self._current
@staticmethod
def from_systems(recovery_systems):
systems = []
for syst in recovery_systems:
m = syst["model"]
b = syst["brand"]
model = SystemModel(
model=m["model"],
brand_id=m["brand-id"],
display_name=m["display-name"]
)
brand = Brand(
ID=b["id"],
username=b["username"],
display_name=b["display-name"],
validation=b.get("validation", "unproven"),
)
actions = []
for a in syst.get("actions", []):
actions.append(SystemAction(title=a["title"], mode=a["mode"]))
s = RecoverySystem(
current=syst.get("current", False),
label=syst["label"],
model=model,
brand=brand,
actions=actions,
)
systems.append(s)
return RecoverySystemsModel(systems)
@staticmethod
def from_systems_stream(chooser_input):
"""Deserialize recovery systems from input JSON stream."""
@ -107,34 +148,7 @@ class RecoverySystemsModel:
log.exception("cannot validate recovery systems data")
raise
systems = []
for sys in dec["systems"]:
m = sys["model"]
b = sys["brand"]
model = SystemModel(
model=m["model"],
brand_id=m["brand-id"],
display_name=m["display-name"]
)
brand = Brand(
ID=b["id"],
username=b["username"],
display_name=b["display-name"],
validation=b.get("validation", "unproven"),
)
actions = []
for a in sys.get("actions", []):
actions.append(SystemAction(title=a["title"], mode=a["mode"]))
s = RecoverySystem(
current=sys.get("current", False),
label=sys["label"],
model=model,
brand=brand,
actions=actions,
)
systems.append(s)
return RecoverySystemsModel(systems)
return RecoverySystemsModel.from_systems(systems)
@staticmethod
def to_response_stream(obj, chooser_output):
@ -146,7 +160,10 @@ class RecoverySystemsModel:
choice = {
"label": obj.system.label,
"action": {
"mode": obj.action.mode,
"title": obj.action.title,
},
}
return json.dump(choice, fp=chooser_output)

View File

@ -107,6 +107,7 @@ class RecoverySystemsModelTests(unittest.TestCase):
),
])
self.assertEqual(systems.systems, exp.systems)
self.assertEqual(systems.current, exp.systems[0])
def test_from_systems_stream_invalid_empty(self):
with self.assertRaises(jsonschema.ValidationError):
@ -204,7 +205,7 @@ class RecoverySystemsModelTests(unittest.TestCase):
raw = json.dumps(self.reference)
model = RecoverySystemsModel.from_systems_stream(StringIO(raw))
model.select(model.systems[1], model.systems[1].actions[0])
self.assertEquals(model.selection,
self.assertEqual(model.selection,
SelectedSystemAction(
system=model.systems[1],
action=model.systems[1].actions[0]))
@ -219,6 +220,38 @@ class RecoverySystemsModelTests(unittest.TestCase):
RecoverySystemsModel.to_response_stream(model.selection, stream)
fromjson = json.loads(stream.getvalue())
self.assertEqual(fromjson, {
"mode": "install",
"label": "other",
"action": {
"mode": "install",
"title": "reinstall",
},
})
def test_no_current(self):
reference = {
"systems": [
{
"current": False,
"label": "1234",
"brand": {
"id": "brand-id",
"username": "brand-username",
"display-name": "this is my brand",
"validation": "verified",
},
"model": {
"model": "core20-amd64",
"brand-id": "brand-id",
"display-name": "Core 20 AMD64 system",
},
"actions": [
{"title": "reinstall", "mode": "install"},
{"title": "recover", "mode": "recover"},
]
},
],
}
systems = RecoverySystemsModel.from_systems(reference["systems"])
self.assertEqual(len(systems.systems), 1)
self.assertEqual(systems.systems[0].label, "1234")
self.assertIsNone(systems.current)