sort laboratory list by name and hide numeric id.

This commit is contained in:
Yoshihiro OKUMURA 2023-06-04 18:53:21 +09:00
parent 593caa82ec
commit 1ffafd25ba
Signed by: orrisroot
GPG Key ID: 470AA444C92904B2
3 changed files with 8 additions and 2 deletions

View File

@ -19,4 +19,5 @@ class LaboratoryApi(BaseApi):
ret = Laboratories() ret = Laboratories()
for data in response.json(): for data in response.json():
ret.append(parse_obj_as(Laboratory, data)) ret.append(parse_obj_as(Laboratory, data))
ret.sort()
return ret return ret

View File

@ -28,13 +28,15 @@ class LabsCommand(BaseCommand):
length["pi_name"] = max(length["pi_name"], len(laboratory.pi_name)) length["pi_name"] = max(length["pi_name"], len(laboratory.pi_name))
length["full_name"] = max(length["full_name"], len(laboratory.full_name)) length["full_name"] = max(length["full_name"], len(laboratory.full_name))
header = ( header = (
f"{label['id']:{length['id']}}\t{label['name']:{length['name']}}\t" # f"{label['id']:{length['id']}}\t{label['name']:{length['name']}}\t"
f"{label['name']:{length['name']}}\t"
f"{label['pi_name']:{length['pi_name']}}\t{label['full_name']:{length['full_name']}}" f"{label['pi_name']:{length['pi_name']}}\t{label['full_name']:{length['full_name']}}"
) )
print(header) print(header)
print("-" * len(header.expandtabs())) print("-" * len(header.expandtabs()))
for laboratory in laboratories: for laboratory in laboratories:
print( print(
f"{laboratory.id:{length['id']}}\t{laboratory.name:{length['name']}}\t" # f"{laboratory.id:{length['id']}}\t{laboratory.name:{length['name']}}\t"
f"{laboratory.name:{length['name']}}\t"
f"{laboratory.pi_name:{length['pi_name']}}\t{laboratory.full_name:{length['full_name']}}" f"{laboratory.pi_name:{length['pi_name']}}\t{laboratory.full_name:{length['full_name']}}"
) )

View File

@ -28,6 +28,9 @@ class Laboratories:
def append(self, item: Laboratory) -> None: def append(self, item: Laboratory) -> None:
self.items.append(item) self.items.append(item)
def sort(self) -> None:
self.items.sort(key=lambda x: x.name)
def find_by_id(self, id: int) -> Laboratory | None: def find_by_id(self, id: int) -> Laboratory | None:
return next((x for x in self.items if x.id == id), None) return next((x for x in self.items if x.id == id), None)