changes
This commit is contained in:
78
app/routers/cron_print.py
Normal file
78
app/routers/cron_print.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from fastapi import APIRouter, BackgroundTasks
|
||||
|
||||
from app.database import session
|
||||
from app.models.printer import Printer_jobs
|
||||
import subprocess
|
||||
import shutil
|
||||
import pdfkit
|
||||
|
||||
location_tickets = '/home/amnesia/gitbox/tickets/'
|
||||
printer_url_service = 'http://172.18.0.1:5173/ticket/'
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/cron",
|
||||
tags=["cron"],
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
|
||||
@router.get("/waiting")
|
||||
async def make_waiting():
|
||||
|
||||
"""
|
||||
Cron job - Gets waiting tickets in database and makes pdf files
|
||||
Updates values in database for next step
|
||||
"""
|
||||
get_tickets_to_print = (session.query(Printer_jobs)
|
||||
.filter(Printer_jobs.status == 0)
|
||||
.all())
|
||||
count = 0
|
||||
for f in get_tickets_to_print:
|
||||
# get the pdf name
|
||||
filename_out = str(f.id) + '.pdf'
|
||||
# read website for the html in
|
||||
web_url = printer_url_service + str(f.id)
|
||||
pdfkit.from_url(web_url, filename_out, verbose=True)
|
||||
#set location and name to move the ticket too
|
||||
renamed_file = location_tickets + filename_out
|
||||
# perform command to move command
|
||||
shutil.move(filename_out, renamed_file)
|
||||
|
||||
# update count
|
||||
newcount = count + 1
|
||||
count = newcount
|
||||
f.status = 1
|
||||
session.add(f)
|
||||
session.commit()
|
||||
|
||||
|
||||
return {'ok': True,
|
||||
'count': count,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/print/waiting")
|
||||
async def print_waiting(background_tasks: BackgroundTasks):
|
||||
"""
|
||||
Checks if pdfs are made by query
|
||||
Prints tickets using linux command on background tasks
|
||||
"""
|
||||
get_tickets_to_print = (session.query(Printer_jobs)
|
||||
.filter(Printer_jobs.status == 1)
|
||||
.all())
|
||||
if get_tickets_to_print is not None:
|
||||
for f in get_tickets_to_print:
|
||||
background_tasks.add_task(print_ticket, id=f.id)
|
||||
f.status = 3
|
||||
session.add(f)
|
||||
session.commit()
|
||||
|
||||
return {'ok': True }
|
||||
|
||||
def print_ticket(id: int):
|
||||
location = location_tickets + id + '.pdf'
|
||||
|
||||
p = (subprocess.run(["lp", location], capture_output=True))
|
||||
|
||||
for line in p.stdout.readlines():
|
||||
print(line)
|
||||
|
||||
Reference in New Issue
Block a user