login. Route handler or application helper. This docstring was expanded to make future maintenance easier. Returns: Varies.
()
| 10598 | return True |
| 10599 | try: |
| 10600 | ip_obj = ipaddress.ip_address(host) |
| 10601 | return bool(ip_obj.is_private or ip_obj.is_loopback or ip_obj.is_link_local) |
| 10602 | except Exception: |
| 10603 | pass |
| 10604 | # Allow normal domain names used by reverse proxies / custom domains. |
| 10605 | if re.fullmatch(r"[a-z0-9.-]{1,253}", host) and "." in host: |
| 10606 | return True |
| 10607 | return False |
| 10608 | |
| 10609 | # --------------------------- |
| 10610 | # Public-link QR generation |
| 10611 | # --------------------------- |
| 10612 | |
| 10613 | CURRENT_LOCAL_URL = None |
| 10614 | CURRENT_LOCALHOST_URL = None |
| 10615 | |
| 10616 | |
| 10617 | def _qr_placeholder_data_uri() -> str: |
| 10618 | """Return a non-crashing placeholder if the qrcode package is unavailable.""" |
| 10619 | svg = ( |
| 10620 | '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 240">' |
| 10621 | '<rect width="240" height="240" fill="white"/>' |
| 10622 | '<rect x="16" y="16" width="208" height="208" rx="16" fill="none" stroke="#111" stroke-width="6"/>' |
| 10623 | '<text x="120" y="112" text-anchor="middle" font-family="sans-serif" font-size="18" fill="#111">QR unavailable</text>' |
| 10624 | '<text x="120" y="140" text-anchor="middle" font-family="sans-serif" font-size="13" fill="#444">Use the text link below</text>' |
| 10625 | '</svg>' |
| 10626 | ) |
| 10627 | return "data:image/svg+xml;base64," + base64.b64encode(svg.encode("utf-8")).decode("ascii") |
| 10628 | |
| 10629 | |
| 10630 | def _link_barcode_data_uri(link: str) -> str: |
| 10631 | """Generate a fresh SVG QR image for one URL without writing temporary files.""" |
| 10632 | value = str(link or "").strip() |
| 10633 | if not value: |
| 10634 | return _qr_placeholder_data_uri() |
| 10635 | try: |
| 10636 | if qrcode is None: |
| 10637 | raise RuntimeError("Python package 'qrcode' is unavailable") |
| 10638 | qr = qrcode.QRCode( |
| 10639 | version=None, |
| 10640 | error_correction=qrcode.constants.ERROR_CORRECT_M, |
| 10641 | box_size=8, |
| 10642 | border=3, |
| 10643 | ) |
| 10644 | qr.add_data(value) |
| 10645 | qr.make(fit=True) |
| 10646 | image = qr.make_image(image_factory=qrcode.image.svg.SvgPathImage) |
| 10647 | output = io.BytesIO() |
| 10648 | image.save(output) |
| 10649 | return "data:image/svg+xml;base64," + base64.b64encode(output.getvalue()).decode("ascii") |
| 10650 | except Exception as exc: |
| 10651 | log.error( |
| 10652 | "QR generation failed for %s: %s", |
| 10653 | value, |
| 10654 | exc, |
| 10655 | exc_info=(type(exc), exc, exc.__traceback__), |
| 10656 | ) |
| 10657 | return _qr_placeholder_data_uri() |
nothing calls this directly
no test coverage detected