-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53a9849
commit 7fa0075
Showing
7 changed files
with
419 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,79 @@ | ||
"""Calculator Controller.""" | ||
from __future__ import annotations | ||
|
||
from litestar import Controller | ||
import ipaddress | ||
from typing import TYPE_CHECKING, Annotated | ||
|
||
from litestar import Controller, get | ||
|
||
__all__ = ["CalculatorController"] | ||
|
||
from litestar.params import Parameter # noqa | ||
from litestar.status_codes import HTTP_200_OK | ||
from litestar.contrib.htmx.response import HTMXTemplate | ||
|
||
from app.domain import urls | ||
from app.domain.calculator.schema import NetworkInfo | ||
|
||
if TYPE_CHECKING: | ||
from litestar.contrib.htmx.request import HTMXRequest | ||
|
||
|
||
class CalculatorController(Controller): | ||
"""Calculator Controller.""" | ||
|
||
opt = {"exclude_from_auth": True} | ||
|
||
@get( | ||
path=urls.IP, | ||
operation_id="CalculatorIP", | ||
name="calculator:ip", | ||
status_code=HTTP_200_OK, | ||
) | ||
async def ip( | ||
self, | ||
request: HTMXRequest, | ||
ip: Annotated[ | ||
str, | ||
Parameter( | ||
..., | ||
description="The IP address in standard IPv4 format.", | ||
pattern=r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", | ||
), | ||
], | ||
prefix: Annotated[ | ||
str, Parameter(..., description="The CIDR notation.", pattern=r"^(?:[0-9]|[1-2][0-9]|3[0-2])$") | ||
], | ||
) -> HTMXTemplate: | ||
"""Calculate IP.""" | ||
|
||
net = ipaddress.IPv4Network(f"{ip}/{prefix}", strict=False) | ||
|
||
subnet_mask = str(net.netmask) | ||
wildcard_subnet_mask = str(net.hostmask) | ||
total_ips = net.num_addresses | ||
usable_ips = total_ips - 2 # minus network and broadcast addresses | ||
network_ip = str(net.network_address) | ||
broadcast_ip = str(net.broadcast_address) | ||
|
||
# First and last usable IP addresses | ||
hosts = list(net.hosts()) | ||
first_ip = str(hosts[0]) if hosts else None | ||
last_ip = str(hosts[-1]) if hosts else None | ||
|
||
network_info = NetworkInfo( | ||
subnet_mask=subnet_mask, | ||
wildcard_subnet_mask=wildcard_subnet_mask, | ||
total_ips=total_ips, | ||
usable_ips=usable_ips, | ||
network_ip=network_ip, | ||
broadcast_ip=broadcast_ip, | ||
first_ip=first_ip, | ||
last_ip=last_ip, | ||
) | ||
|
||
return HTMXTemplate( | ||
template_name="partial.html", | ||
context=network_info.dict(), # Convert the pydantic model to a dict | ||
push_url="/calculate", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.