"""Pagina de printare a unei etichete de produs finit.""" from __future__ import annotations from flask import Blueprint, abort, current_app, render_template, request from ..db import all_settings, get_setting from ..labels import LabelTemplateError, print_payload from ..repositories import get_default_template, get_package, get_recipe, get_template bp = Blueprint("printing", __name__) @bp.get("/labels//print") def print_label(package_id: int): package = get_package(package_id) if package is None: abort(404) template_id = request.args.get("template_id") or package.get("template_id") configured = get_setting("default_template_id", "") template = None if template_id: try: template = get_template(int(template_id)) except (TypeError, ValueError): template = None if template is None and configured.isdigit(): template = get_template(int(configured)) template = template or get_default_template() if template is None: abort(409, "Nu exista niciun sablon de eticheta configurat.") recipe = get_recipe(package["recipe_id"]) if package["recipe_id"] else None try: payload = print_payload(package, template, all_settings(), recipe) except LabelTemplateError as exc: abort(500, str(exc)) settings = all_settings() auto = request.args.get("auto") if auto is None: auto = settings.get("auto_print", "1") == "1" and current_app.config["AUTO_PRINT"] else: auto = auto not in {"0", "false", "no"} return render_template("label_print.html", payload=payload, auto=bool(auto), preview=False)