Remove flash from the printer class, update web class

This commit is contained in:
nono
2025-10-16 18:36:16 +02:00
parent dfeb1be0f0
commit eac737b889
2 changed files with 9 additions and 13 deletions

View File

@@ -1,5 +1,4 @@
# Importing the module to mage the connection to the printer.
from flask import flash
from escpos.printer import Usb, USBNotFoundError
from time import sleep, gmtime, strftime
import os.path
@@ -46,12 +45,10 @@ class Printer(object):
match status:
case 0:
self.app.logger.error('Printer has no more paper, aborting...')
flash("No more paper on the printer. Sorry.",category='error')
self.printer.close()
raise Exception("No more paper in the printer")
case 1:
self.app.logger.warning('Printer needs paper to be changed very soon ! ')
flash('Printer needs paper to be changed very soon ! ', category='info')
self.printer.close()
case 2:
self.app.logger.debug('Printer has paper, good to go')
@@ -101,7 +98,7 @@ class Printer(object):
return True
def print_sms(self, msg, signature) -> None:
def print_sms(self, msg, signature="Guest") -> None:
clean_msg = str(msg)
clean_signature = str(signature)
@@ -109,7 +106,7 @@ class Printer(object):
self.app.logger.warning("Could not print message of this length: " + str(len(clean_msg)))
raise Exception("Could not print message of this length :" + str(len(clean_msg)) + ", needs to between 3 and 4096 caracters long.")
if len(signature) > 256 or len(signature) < 3:
if len(signature) > 256 or len(signature) < 1:
self.app.logger.warning("Could not print signature of this length: " + str(len(clean_signature)))
raise Exception("Could not print signature of this length :" + str(len(clean_signature)) + ", needs to between 3 and 256 caracters long.")
@@ -133,13 +130,12 @@ class Printer(object):
self.app.logger.error("Unable to print because : " + str(e))
raise e
flash("Message printed : " + clean_msg ,category='info')
return True
def print_img(self, path, sign) -> None:
clean_signature = str(sign)
if len(sign) > 256 or len(sign) < 3:
if len(sign) > 256 or len(sign) < 1:
self.app.logger.warning("Could not print signature of this length: " + str(len(clean_signature)))
raise Exception("Could not print signature of this length :" + str(len(clean_signature)) + ", needs to between 3 and 256 caracters long.")
@@ -160,10 +156,10 @@ class Printer(object):
try:
self.check_paper()
self.printer.open(self.usb_args)
self.printer.textln("Printed by LittlePrinter ")
self.printer.textln("n07070.xyz/articles/littleprynter")
self.printer.textln("> " + clean_signature + " @ " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
self.printer.image(path)
self.printer.textln("Printed by LittlePrynter ")
self.printer.textln("n07070.xyz/articles/littleprynter")
self.printer.textln(clean_signature + " @ " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
self.printer.cut()
self.printer.close()
self.app.logger.debug("Printed an image : " + str(path))
@@ -214,7 +210,7 @@ def process_image(self, path):
# Final resize check
if original_img.height > max_height:
flash("Image is too long, sorry! Keep it below 575×1000 pixels.", 'error')
raise ValueError("Image is too long, sorry! Keep it below 575×1000 pixels.")
self.app.logger.error("Image is too long, sorry! Keep it below 575×1000 pixels.")
return False