2015-08-07 11:55:21 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
import zipfile
|
|
|
|
from flask import Flask, render_template
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2015-08-08 19:26:24 -04:00
|
|
|
data = []
|
2015-08-17 12:47:54 -04:00
|
|
|
attributes = ["title",
|
|
|
|
"authors",
|
|
|
|
"isbn"]
|
2015-08-08 19:26:24 -04:00
|
|
|
|
2015-08-13 10:29:18 -04:00
|
|
|
fields = {}
|
2015-08-09 20:13:44 -04:00
|
|
|
|
2015-08-13 11:18:51 -04:00
|
|
|
@app.route("/r")
|
2015-08-17 12:47:54 -04:00
|
|
|
def update_data():
|
2015-08-13 11:18:51 -04:00
|
|
|
global data
|
2015-08-08 19:26:24 -04:00
|
|
|
out = []
|
2015-08-07 11:55:21 -04:00
|
|
|
|
|
|
|
f = zipfile.ZipFile("library.tc")
|
|
|
|
tree = ElementTree.parse(f.open("tellico.xml"))
|
|
|
|
collection = tree.getroot()[0]
|
|
|
|
ns = "{http://periapsis.org/tellico/}"
|
|
|
|
|
2015-08-09 20:13:44 -04:00
|
|
|
for field in collection.find(ns + "fields"):
|
2015-08-13 10:29:18 -04:00
|
|
|
#check if multiple entries are allowed
|
|
|
|
if (int(field.attrib["flags"]) & 0x01 & 0xff) != 0:
|
|
|
|
#change name to match
|
|
|
|
name = field.attrib["name"] + "s"
|
|
|
|
else:
|
|
|
|
name = field.attrib["name"]
|
|
|
|
fields[name] = field.attrib
|
2015-08-09 20:13:44 -04:00
|
|
|
|
2015-08-17 12:47:54 -04:00
|
|
|
for entry in collection.iter(ns + "entry"):
|
2015-08-07 11:55:21 -04:00
|
|
|
bookout = {}
|
2015-08-17 12:47:54 -04:00
|
|
|
for datum_name in fields.keys():
|
|
|
|
datum = entry.find(ns + datum_name)
|
2015-08-13 11:21:53 -04:00
|
|
|
if datum != None:
|
2015-08-17 12:47:54 -04:00
|
|
|
datum_type = int(fields[datum_name]["type"])
|
2015-08-13 10:29:18 -04:00
|
|
|
#simple text, paragraph, multiple choice, image
|
2015-08-17 12:47:54 -04:00
|
|
|
if datum_type == 1 or datum_type == 2 or datum_type == 3 or datum_type == 10:
|
2015-08-13 10:29:18 -04:00
|
|
|
#check if multiple entries are allowed
|
|
|
|
#if so, put entries in a list
|
2015-08-17 12:47:54 -04:00
|
|
|
if (int(fields[datum_name]["flags"]) & 0x01 & 0xff) != 0:
|
|
|
|
bookout[datum_name] = []
|
2015-08-13 11:21:53 -04:00
|
|
|
for sub in datum:
|
2015-08-17 12:47:54 -04:00
|
|
|
bookout[datum_name].append(sub.text)
|
2015-08-13 10:29:18 -04:00
|
|
|
else:
|
2015-08-17 12:47:54 -04:00
|
|
|
bookout[datum_name] = datum.text
|
2015-08-08 19:24:14 -04:00
|
|
|
|
2015-08-13 10:29:18 -04:00
|
|
|
#date
|
2015-08-17 12:47:54 -04:00
|
|
|
elif datum_type == 12:
|
|
|
|
bookout[datum_name] = ""
|
2015-08-13 11:21:53 -04:00
|
|
|
if datum.find(ns + "year") != None:
|
2015-08-17 12:47:54 -04:00
|
|
|
bookout[datum_name] += datum.find(ns + "year").text
|
2015-08-13 11:21:53 -04:00
|
|
|
if datum.find(ns + "month") != None:
|
2015-08-17 12:47:54 -04:00
|
|
|
bookout[datum_name] += "-" + datum.find(ns + "month").text
|
2015-08-13 11:21:53 -04:00
|
|
|
if datum.find(ns + "day") != None:
|
2015-08-17 12:47:54 -04:00
|
|
|
bookout[datum_name] += "-" + datum.find(ns + "day").text
|
2015-08-13 10:29:18 -04:00
|
|
|
|
2015-08-17 12:47:54 -04:00
|
|
|
elif datum_type == 6: #Number
|
|
|
|
bookout[datum_name] = datum.text
|
2015-08-13 10:29:18 -04:00
|
|
|
else:
|
2015-08-17 12:47:54 -04:00
|
|
|
print("Did not parse:" + datum_name + ", type:" + str(datum_type))
|
|
|
|
bookout[datum_name] = ""
|
2015-08-13 10:29:18 -04:00
|
|
|
else:
|
2015-08-17 12:47:54 -04:00
|
|
|
bookout[datum_name] = ""
|
2015-08-07 11:55:21 -04:00
|
|
|
out.append(bookout.copy())
|
2015-08-13 11:18:51 -04:00
|
|
|
data = out.copy()
|
|
|
|
return "success"
|
2015-08-08 19:26:24 -04:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def main():
|
2015-08-13 11:18:51 -04:00
|
|
|
if data == []:
|
2015-08-17 12:47:54 -04:00
|
|
|
update_data()
|
|
|
|
return render_template("main.html", books=data, attributes=attributes)
|
2015-08-08 19:26:24 -04:00
|
|
|
|
2015-08-17 12:47:54 -04:00
|
|
|
@app.route("/book/<int:id_num>")
|
|
|
|
def book(id_num):
|
2015-08-13 11:18:51 -04:00
|
|
|
if data == []:
|
2015-08-17 12:47:54 -04:00
|
|
|
update_data()
|
|
|
|
entry = next((item for item in data if int(item["id"]) == id_num), {})
|
|
|
|
return render_template("book.html", book=entry, fields=fields)
|
2015-08-07 11:55:21 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=True, host='0.0.0.0')
|