changes
This commit is contained in:
@@ -12,6 +12,8 @@ WORKDIR /app
|
||||
|
||||
RUN pip3 install -r requirements.txt
|
||||
|
||||
RUN apt-get update && apt-get install -y wkhtmltopdf
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
COPY . /app
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
0
app/bin/__init__.py
Normal file
0
app/bin/__init__.py
Normal file
1
app/bin/print_file.py
Normal file
1
app/bin/print_file.py
Normal file
@@ -0,0 +1 @@
|
||||
adsfasd
|
||||
@@ -1,13 +0,0 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Header, HTTPException
|
||||
|
||||
|
||||
async def get_token_header(x_token: Annotated[str, Header()]):
|
||||
if x_token != "fake-super-secret-token":
|
||||
raise HTTPException(status_code=400, detail="X-Token header invalid")
|
||||
|
||||
|
||||
async def get_query_token(token: str):
|
||||
if token != "jessica":
|
||||
raise HTTPException(status_code=400, detail="No Jessica token provided")
|
||||
@@ -1,6 +1,8 @@
|
||||
|
||||
from fastapi import FastAPI
|
||||
from app.routers import printstatus, command
|
||||
from app.routers import (cron_print, printstatus,
|
||||
command,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -11,11 +13,12 @@ app = FastAPI()
|
||||
|
||||
app.include_router(printstatus.router)
|
||||
app.include_router(command.router)
|
||||
app.include_router(cron_print.router)
|
||||
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"Hello": "World"}
|
||||
return {"Status": "Printer Service is Online"}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
BIN
app/models/__pycache__/printer.cpython-312.pyc
Normal file
BIN
app/models/__pycache__/printer.cpython-312.pyc
Normal file
Binary file not shown.
@@ -1,5 +1,5 @@
|
||||
from sqlalchemy import Column, Integer,\
|
||||
Integer, DECIMAL, Text,\
|
||||
DECIMAL, Text,\
|
||||
VARCHAR, TIMESTAMP, Date
|
||||
from datetime import datetime
|
||||
from app.database import Base
|
||||
@@ -48,5 +48,3 @@ class Delivery(Base):
|
||||
pre_charge_amount = Column(DECIMAL(50, 2))
|
||||
total_price = Column(DECIMAL(50, 2))
|
||||
final_price = Column(DECIMAL(50, 2))
|
||||
|
||||
|
||||
|
||||
29
app/models/printer.py
Normal file
29
app/models/printer.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from sqlalchemy import Column, Integer,\
|
||||
DECIMAL, Text,\
|
||||
VARCHAR, TIMESTAMP, Date
|
||||
from datetime import datetime
|
||||
from app.database import Base
|
||||
|
||||
|
||||
class Printer_jobs(Base):
|
||||
__tablename__ = "printer_jobs"
|
||||
|
||||
id = Column(Integer,
|
||||
primary_key=True,
|
||||
autoincrement=True,
|
||||
unique=False)
|
||||
|
||||
delivery_id = Column(Integer)
|
||||
date_added = Column(Date(), default=datetime.utcnow())
|
||||
date_completed = Column(Date(), default=datetime.utcnow())
|
||||
employee_id = Column(Integer)
|
||||
#0 = waiting
|
||||
#1 = file made..read to print
|
||||
#2 = printing
|
||||
#3 = printed
|
||||
#4 = error
|
||||
status = Column(Integer)
|
||||
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
BIN
app/routers/__pycache__/cron_make_files.cpython-312.pyc
Normal file
BIN
app/routers/__pycache__/cron_make_files.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/routers/__pycache__/cron_print.cpython-312.pyc
Normal file
BIN
app/routers/__pycache__/cron_print.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/routers/__pycache__/cronprint.cpython-312.pyc
Normal file
BIN
app/routers/__pycache__/cronprint.cpython-312.pyc
Normal file
Binary file not shown.
Binary file not shown.
@@ -1,7 +1,9 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter
|
||||
|
||||
from datetime import date, timedelta, datetime
|
||||
from app.database import session
|
||||
from app.models.delivery import Delivery
|
||||
|
||||
from app.models.printer import Printer_jobs
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/command",
|
||||
@@ -10,7 +12,103 @@ router = APIRouter(
|
||||
)
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def read_items():
|
||||
x = session.query(Delivery).first()
|
||||
return {"x": x.id}
|
||||
@router.get("/printticket/{delivery_id}")
|
||||
async def print_specific_ticket(delivery_id):
|
||||
|
||||
now = datetime.utcnow()
|
||||
|
||||
get_ticket = (session.query(Delivery)
|
||||
.filter(Delivery.id == delivery_id)
|
||||
.first())
|
||||
|
||||
new_job = Printer_jobs(
|
||||
delivery_id = delivery_id,
|
||||
date_added = now,
|
||||
date_completed = None,
|
||||
employee_id = get_ticket.driver_employee_id,
|
||||
status = 0,
|
||||
)
|
||||
session.add(new_job)
|
||||
session.commit()
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.get("/print_waiting")
|
||||
async def print_waiting_tickets():
|
||||
|
||||
tomm = date.today() + timedelta(days=1)
|
||||
now = datetime.utcnow()
|
||||
|
||||
get_deliveries = (session
|
||||
.query(Delivery)
|
||||
.filter(Delivery.delivery_status == 0)
|
||||
.filter(Delivery.expected_delivery_date != tomm)
|
||||
.filter(Delivery.expected_delivery_date != date.today())
|
||||
.all())
|
||||
|
||||
for f in get_deliveries:
|
||||
new_job = Printer_jobs(
|
||||
delivery_id = f.id,
|
||||
date_added = now,
|
||||
date_completed = None,
|
||||
employee_id = f.driver_employee_id,
|
||||
status = 0,
|
||||
)
|
||||
session.add(new_job)
|
||||
|
||||
session.commit()
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.get("/print_today")
|
||||
async def print_today_tickets():
|
||||
|
||||
now = datetime.utcnow()
|
||||
|
||||
get_deliveries = (session
|
||||
.query(Delivery)
|
||||
.filter(Delivery.expected_delivery_date == date.today())
|
||||
.all())
|
||||
|
||||
for f in get_deliveries:
|
||||
new_job = Printer_jobs(
|
||||
delivery_id = f.id,
|
||||
date_added = now,
|
||||
date_completed = None,
|
||||
employee_id = f.driver_employee_id,
|
||||
status = 0,
|
||||
)
|
||||
session.add(new_job)
|
||||
|
||||
session.commit()
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.get("/print_tommorrow")
|
||||
async def print_tommorrow_tickets():
|
||||
|
||||
now = datetime.utcnow()
|
||||
tomm = date.today() + timedelta(days=1)
|
||||
|
||||
get_deliveries = (session
|
||||
.query(Delivery)
|
||||
.filter(Delivery.expected_delivery_date == tomm)
|
||||
.all())
|
||||
|
||||
for f in get_deliveries:
|
||||
new_job = Printer_jobs(
|
||||
delivery_id = f.id,
|
||||
date_added = now,
|
||||
date_completed = None,
|
||||
employee_id = f.driver_employee_id,
|
||||
status = 0,
|
||||
)
|
||||
session.add(new_job)
|
||||
|
||||
session.commit()
|
||||
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
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)
|
||||
|
||||
@@ -1,10 +1,40 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
|
||||
from app.database import session
|
||||
from app.models.delivery import Delivery
|
||||
from app.models.printer import Printer_jobs
|
||||
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/status",
|
||||
tags=["status"],
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
@router.get("/")
|
||||
|
||||
|
||||
@router.get("/waiting")
|
||||
async def read_items():
|
||||
return {"Hello": "World"}
|
||||
all_jobs = session.query(Printer_jobs).filter(Printer_jobs.status == 0).all()
|
||||
return JSONResponse(content=jsonable_encoder(all_jobs), status_code=200)
|
||||
|
||||
@router.get("/ready")
|
||||
async def read_items():
|
||||
all_jobs = session.query(Printer_jobs).filter(Printer_jobs.status == 1).all()
|
||||
return JSONResponse(content=jsonable_encoder(all_jobs), status_code=200)
|
||||
|
||||
@router.get("/printing")
|
||||
async def read_items():
|
||||
all_jobs = session.query(Printer_jobs).filter(Printer_jobs.status == 2).all()
|
||||
return JSONResponse(content=jsonable_encoder(all_jobs), status_code=200)
|
||||
|
||||
@router.get("/printed")
|
||||
async def read_items():
|
||||
all_jobs = session.query(Printer_jobs).filter(Printer_jobs.status == 3).all()
|
||||
return JSONResponse(content=jsonable_encoder(all_jobs), status_code=200)
|
||||
|
||||
@router.get("/error")
|
||||
async def read_items():
|
||||
all_jobs = session.query(Printer_jobs).filter(Printer_jobs.status == 4).all()
|
||||
return JSONResponse(content=jsonable_encoder(all_jobs), status_code=200)
|
||||
0
app/schema/__init__.py
Normal file
0
app/schema/__init__.py
Normal file
39
app/schema/delivery.py
Normal file
39
app/schema/delivery.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from datetime import datetime, timezone
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
def datetime_now() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
class Delivery(BaseModel):
|
||||
customer_id: int
|
||||
customer_name: str
|
||||
customer_address: str
|
||||
customer_town: str
|
||||
customer_state: int
|
||||
customer_zip: int
|
||||
gallons_ordered: float
|
||||
customer_asked_for_fill: int
|
||||
gallons_delivered: float
|
||||
customer_filled: int
|
||||
delivery_status: int
|
||||
when_ordered: datetime = Field(default_factory=datetime_now)
|
||||
when_delivered: datetime = Field(default_factory=datetime_now)
|
||||
expected_delivery_date: datetime = Field(default_factory=datetime_now)
|
||||
automatic: int
|
||||
oil_id: int
|
||||
supplier_price: float
|
||||
customer_price: float
|
||||
customer_temperature: float
|
||||
dispatcher_notes: str
|
||||
prime: int
|
||||
same_day: int
|
||||
payment_type: int
|
||||
payment_card_id: int
|
||||
cash_recieved: float
|
||||
driver_employee_id: int
|
||||
driver_first_name: str
|
||||
driver_last_name: str
|
||||
pre_charge_amount: float
|
||||
total_price: float
|
||||
final_price: float
|
||||
@@ -1,4 +1,9 @@
|
||||
fastapi
|
||||
uvicorn[standard]
|
||||
psycopg2-binary
|
||||
sqlalchemy
|
||||
sqlalchemy
|
||||
python-multipart
|
||||
pdfkit
|
||||
requests
|
||||
beautifulsoup4
|
||||
lxml
|
||||
BIN
tickets/1.pdf
Normal file
BIN
tickets/1.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user