Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infra improvements #692

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/frontend/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ def test_user_host_without_room(client, host_without_room):
)
assert len(resp.json["items"]) == 1
[it] = resp.json["items"]
assert it["switch"] is None
assert it["switch"] == []
assert it["port"] is None
17 changes: 11 additions & 6 deletions web/blueprints/host/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from web.blueprints.helpers.user import get_user_or_404
from web.blueprints.host.forms import InterfaceForm, HostForm
from web.blueprints.host.tables import InterfaceTable, HostTable, HostRow, InterfaceRow
from web.table.table import TableResponse, BtnColResponse
from web.table.table import TableResponse, BtnColResponse, LinkColResponse

bp = Blueprint('host', __name__)
access = BlueprintAccess(bp, required_properties=['user_show'])
Expand Down Expand Up @@ -330,17 +330,22 @@ def default_response() -> ResponseReturnValue:
def _host_row(host: Host, user_id: int) -> HostRow:
if host.room:
patch_ports = host.room.connected_patch_ports
switches = ", ".join(
p.switch_port.switch.host.name or "<unnamed switch>" for p in patch_ports
)
switches = [p.switch_port.switch.host for p in patch_ports]
switch_links = [
LinkColResponse(
href=url_for("infrastructure.switch_show", switch_id=s.id),
title=s.name or f"<unnamed switch #{s.id}>",
)
for s in switches
]
ports = ", ".join(p.switch_port.name for p in patch_ports)
else:
switches = None
switch_links = []
ports = None
return HostRow(
id=host.id,
name=host.name,
switch=switches,
switch=switch_links,
port=ports,
actions=[
BtnColResponse(
Expand Down
6 changes: 4 additions & 2 deletions web/blueprints/host/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
button_toolbar,
MultiBtnColumn,
BtnColResponse,
MultiLinkColumn,
LinkColResponse,
)


class HostTable(BootstrapTable):
"""A table for displaying hosts
"""
name = Column("Name")
switch = Column("Switch")
switch = MultiLinkColumn("Switch")
port = Column("SwitchPort")
actions = MultiBtnColumn("Aktionen", hide_if=no_hosts_change, width=3)
interfaces_table_link = Column("", hide_if=lambda: True)
Expand Down Expand Up @@ -49,7 +51,7 @@ def toolbar(self) -> HasDunderStr | None:

class HostRow(BaseModel):
name: str | None = None
switch: str | None = None
switch: list[LinkColResponse] | None = None
port: str | None = None
actions: list[BtnColResponse]
interfaces_table_link: str
Expand Down
5 changes: 4 additions & 1 deletion web/blueprints/infrastructure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ def switch_show(switch_id: int) -> ResponseValue:
switch=switch,
port_table=PortTable(
data_url=url_for('.switch_show_json', switch_id=switch.host_id),
switch_id=switch_id))
switch_id=switch_id,
switch_room_id=switch.host.room_id,
),
)


@bp.route('/switch/show/<int:switch_id>/json')
Expand Down
19 changes: 14 additions & 5 deletions web/blueprints/infrastructure/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask_login import current_user
from pydantic import BaseModel, ConfigDict

from web.table.lazy_join import HasDunderStr
from web.table.lazy_join import HasDunderStr, lazy_join
from web.table.table import (
BootstrapTable,
Column,
Expand Down Expand Up @@ -85,9 +85,10 @@ class Meta:
'data-sort-name': 'switchport_name',
}

def __init__(self, *, switch_id: int | None = None, **kw: t.Any) -> None:
def __init__(self, *, switch_id: int, switch_room_id: int, **kw: t.Any) -> None:
super().__init__(**kw)
self.switch_id = switch_id
self.switch_room_id = switch_room_id

switchport_name = Column("Name", width=2, col_args={'data-sorter': 'table.sortPort'})
patchport_name = Column("→ Patchport", width=2, col_args={'data-sorter': 'table.sortPatchPort'})
Expand All @@ -96,11 +97,19 @@ def __init__(self, *, switch_id: int | None = None, **kw: t.Any) -> None:
delete_link = BtnColumn('Löschen', hide_if=no_inf_change)

@property
def toolbar(self) -> HasDunderStr | None:
@lazy_join
def toolbar(self) -> t.Iterator[HasDunderStr | None]:
if no_inf_change():
return None
href = url_for(".switch_port_create", switch_id=self.switch_id)
return button_toolbar("Switch-Port", href)

yield from button_toolbar(
"Switch-Port",
url_for(".switch_port_create", switch_id=self.switch_id),
)
yield from button_toolbar(
"Patch-Port",
url_for("facilities.patch_port_create", switch_room_id=self.switch_room_id),
)


class PortRow(BaseModel):
Expand Down
10 changes: 10 additions & 0 deletions web/resources/js/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ export function multiBtnFormatter(value, row, index) {
return btnFormatter(value, row, index);
}

export function multiLinkFormatter(value, row, index) {
if (!value) {
return;
}
if (Array.isArray(value)) {
return value.map(v => linkFormatter(v, row, index)).join('&nbsp;');
}
return linkFormatter(value, row, index);
}

export function listFormatter(value, row, index) {
if (!value) {
return;
Expand Down
7 changes: 7 additions & 0 deletions web/table/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ def value(
...


@custom_formatter_column("table.multiLinkFormatter")
class MultiLinkColumn(Column):
def __init__(self, *a: t.Any, **kw: t.Any) -> None:
kw.setdefault("sortable", False)
super().__init__(*a, **kw)



@custom_formatter_column('table.dateFormatter')
class DateColumn(Column):
Expand Down
Loading