Files
MrBlakeandClaude Opus 5.5 ab8ab7ed61 Add room size input in meters alongside centimeters
The floor plan form and the designer accept a room size like "4 x 3"
(meters), show dimensions in meters on the plan edges, in the table,
detail page and tooltips, and display the floor area.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 12:37:15 +01:00

31 lines
955 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import re
from django.utils import formats
__all__ = ('format_meters', 'format_room_size', 'parse_room_size')
# Accepts e.g. "4x3", "4 x 3 m", "4m x 3m", "12,5 × 8", "4.2*3"
ROOM_SIZE_RE = re.compile(
r'^\s*(\d+(?:[.,]\d+)?)\s*m?\s*[x×*]\s*(\d+(?:[.,]\d+)?)\s*m?\s*$',
re.IGNORECASE,
)
def parse_room_size(value):
"""Parse "<width> x <depth>" in meters; returns (width_cm, depth_cm) or None."""
match = ROOM_SIZE_RE.match(value or '')
if not match:
return None
width, depth = (round(float(v.replace(',', '.')) * 100) for v in match.groups())
return width, depth
def format_meters(cm):
"""Format a centimeter value as meters using the active locale, e.g. 450 -> "4,5"."""
text = f'{cm / 100:.2f}'.rstrip('0').rstrip('.')
return text.replace('.', formats.get_format('DECIMAL_SEPARATOR'))
def format_room_size(width_cm, depth_cm):
return f'{format_meters(width_cm)} x {format_meters(depth_cm)}'