2022-03-12 04:29:51 +01:00
|
|
|
from flask import Flask, request
|
2022-03-17 10:39:22 +01:00
|
|
|
from werkzeug.utils import secure_filename
|
2022-03-12 04:29:51 +01:00
|
|
|
from printer import Printer
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
|
|
|
|
class Web(object):
|
2020-12-27 21:33:12 +01:00
|
|
|
"""docstring for web."""
|
|
|
|
|
2022-03-12 04:29:51 +01:00
|
|
|
def __init__(self, app, printer ):
|
|
|
|
super(Web).__init__()
|
|
|
|
self.printer = printer
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
def print_sms(self, texte, sign: str):
|
|
|
|
# TODO: verify the texte before printing it here ?
|
2022-03-15 14:49:58 +01:00
|
|
|
self.app.logger.debug("Printing : " + str(texte) + " from " + str(sign))
|
2022-03-12 04:29:51 +01:00
|
|
|
if not os.getenv('LIPY_DEBUG'):
|
2022-03-15 14:49:58 +01:00
|
|
|
time.sleep(1)
|
2022-03-12 04:29:51 +01:00
|
|
|
|
|
|
|
return self.printer.print_sms(texte, sign)
|
2020-12-27 21:33:12 +01:00
|
|
|
|
2022-03-17 10:39:22 +01:00
|
|
|
def print_image(self, image, sign: str) -> bool:
|
|
|
|
self.app.logger.debug("Uploading file")
|
|
|
|
try:
|
|
|
|
self.app.logger.debug("Uploading file from " + str(sign))
|
|
|
|
if self.upload_file(image):
|
|
|
|
self.app.logger.debug("File has been uploaded, printing...")
|
|
|
|
self.printer.print_img(os.path.join(self.app.config['UPLOAD_FOLDER'], secure_filename(image.filename)), sign)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except Exception as e:
|
|
|
|
self.app.logger.error(e)
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
else:
|
|
|
|
flash("Could not upload file.",'error')
|
|
|
|
return False
|
2022-03-15 14:49:58 +01:00
|
|
|
|
2022-03-12 04:29:51 +01:00
|
|
|
def login(username: str,password: str) -> bool:
|
|
|
|
pass
|
2020-12-27 21:33:12 +01:00
|
|
|
|
2022-03-12 04:29:51 +01:00
|
|
|
def logout(username: str, password: str) -> bool:
|
|
|
|
pass
|
2022-03-17 10:39:22 +01:00
|
|
|
|
|
|
|
def allowed_file(self, filename) -> bool:
|
|
|
|
self.app.logger.debug("Is the filename allowed ?")
|
|
|
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in self.app.config['ALLOWED_EXTENSIONS']
|
|
|
|
|
|
|
|
def upload_file(self, image)-> bool:
|
|
|
|
self.app.logger.debug("Validating file")
|
|
|
|
if image and self.allowed_file(image.filename):
|
|
|
|
filename = secure_filename(image.filename)
|
|
|
|
self.app.logger.debug("File valid")
|
|
|
|
try:
|
|
|
|
image.save(os.path.join(self.app.config['UPLOAD_FOLDER'], filename))
|
|
|
|
self.app.logger.debug("File saved")
|
|
|
|
except Exception as e:
|
|
|
|
self.app.logger.error("Could not save file")
|
|
|
|
flash(e,'error')
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.app.logger.debug("File saved to " + str(os.path.join(self.app.config['UPLOAD_FOLDER'], filename)))
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.app.logger.error("Could not save file " + str(filename))
|
|
|
|
return False
|