143 lines
3.7 KiB
Python
143 lines
3.7 KiB
Python
from fastapi import FastAPI, Form
|
|
from isbnlib import meta
|
|
from sqlalchemy import create_engine, Column, Integer, String, TEXT
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fpdf import FPDF
|
|
|
|
# Declare Base and setup tables
|
|
Base = declarative_base()
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = 'User'
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
Username = Column(String(50))
|
|
FirstName = Column(String(50))
|
|
LastName = Column(String(50))
|
|
Email = Column(String(50))
|
|
|
|
|
|
class Book(Base):
|
|
__tablename__ = 'Book'
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
userid = Column(Integer)
|
|
locationid = Column(Integer)
|
|
Title = Column(TEXT)
|
|
Author = Column(TEXT)
|
|
|
|
|
|
class Location(Base):
|
|
__tablename__ = 'Location'
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
userid = Column(Integer)
|
|
Bookcase = Column(Integer)
|
|
Shelf = Column(Integer)
|
|
|
|
|
|
# Connect to database and establish a session
|
|
engine = create_engine('mysql+pymysql://library:jdSCAd6KEqfbpzh9NT4x@db.dandembinski.com:3306/Library',
|
|
pool_recycle=3600)
|
|
# engine.connect()
|
|
|
|
Session = sessionmaker(bind=engine)
|
|
session = Session()
|
|
|
|
# Initialize FastAPI and setup routes
|
|
app = FastAPI()
|
|
app.mount("/static", StaticFiles(directory='static'), name='static')
|
|
|
|
|
|
@app.get('/')
|
|
async def root():
|
|
return {"message": "Hello World"}
|
|
|
|
|
|
@app.post('/checkin/')
|
|
async def checkin(isbn: str = Form(...), locationid: int = Form(...), userid: int = Form(...)):
|
|
info = await getMeta(isbn)
|
|
session.add(Book(userid=userid, Title=info['Title'], Author=info['Authors'], locationid=locationid))
|
|
try:
|
|
session.commit()
|
|
except:
|
|
session.rollback()
|
|
raise
|
|
return {"Checked in": info['Title']}
|
|
|
|
|
|
@app.get('/checkout/{isbn}')
|
|
async def checkout(isbn):
|
|
return {"Checked Out": isbn}
|
|
|
|
|
|
@app.get('/getbooks/{userid}')
|
|
async def getbooks(userid):
|
|
books = []
|
|
result = session.query(Book).filter(Book.userid == userid)
|
|
|
|
for each in result:
|
|
books.append(each)
|
|
|
|
return books
|
|
|
|
|
|
@app.post('/lookup')
|
|
async def lookup(isbn: str = Form(...)):
|
|
info = await getMeta(isbn)
|
|
return {"Title": info['Title']}
|
|
|
|
|
|
@app.post('/AddLocation/')
|
|
async def checkin(BookCase: str = Form(...), Shelf: str = Form(...)):
|
|
location = []
|
|
session.add(Location(userid=1, Bookcase=BookCase, Shelf=Shelf))
|
|
session.commit()
|
|
location.append(BookCase)
|
|
location.append(Shelf)
|
|
return {"Location Added": location}
|
|
|
|
|
|
@app.post('/barcodes')
|
|
async def barcodes(userid: int = Form(...)):
|
|
barcode = {'Check In': 'CI', 'Check Out': 'CO'}
|
|
pdf = FPDF()
|
|
|
|
try:
|
|
pdf.add_font('barcode', '', './static/barcodeFont/LibreBarcode39-Regular.ttf', uni=True)
|
|
except:
|
|
pdf.add_font('barcode', '', '.\\static\\barcodeFont\\LibreBarcode39-Regular.ttf', uni=True)
|
|
|
|
pdf.add_page()
|
|
|
|
result = session.query(Location).filter(Location.userid == userid)
|
|
|
|
for each in result:
|
|
code = str(each.id)
|
|
display = each.Bookcase + ' - ' + each.Shelf
|
|
barcode[display] = 'Loc.' + code
|
|
|
|
for each in barcode:
|
|
pdf.set_font('barcode', '', 25)
|
|
pdf.cell(0, 5, '*'+barcode[each]+'$M*')
|
|
w = pdf.get_string_width('*'+barcode[each]+'$M*')
|
|
pdf.ln()
|
|
pdf.set_font('Arial', '', 5)
|
|
pdf.cell(w, 5, each, align='C')
|
|
pdf.ln()
|
|
pdf.ln()
|
|
|
|
try:
|
|
try:
|
|
pdf.output(name='./static/barcodes.pdf')
|
|
except:
|
|
pdf.output(name='.\\static\\barcodes.pdf')
|
|
return {'Barcodes': 'Ready'}
|
|
except:
|
|
return {'Barcodes': 'Failed'}
|
|
|
|
|
|
async def getMeta(isbn):
|
|
info = meta(isbn, service='wiki')
|
|
return info
|