change parsing to read type and flags
this means that everything can be parsed
This commit is contained in:
parent
b4501d60bc
commit
ab6b9bd247
@ -4,5 +4,7 @@
|
||||
<p> <img src="{{ url_for('static', filename="library_files/" + book["cover"]) }}"> </p>
|
||||
{% endif %}
|
||||
{% for datum in (book|dictsort) %}
|
||||
<p> {{names[datum[0]]}}: {{ datum[1] }} </p>
|
||||
{% if datum[1] is not equalto "" %}
|
||||
<p> {{fields[datum[0]]["title"]}}: {{ datum[1] }} </p>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
57
test.py
57
test.py
@ -7,10 +7,10 @@ app = Flask(__name__)
|
||||
|
||||
data = []
|
||||
attributes= ["title",
|
||||
"author",
|
||||
"authors",
|
||||
"isbn"]
|
||||
|
||||
names = {}
|
||||
fields = {}
|
||||
|
||||
def updateData():
|
||||
out = []
|
||||
@ -21,22 +21,47 @@ def updateData():
|
||||
ns = "{http://periapsis.org/tellico/}"
|
||||
|
||||
for field in collection.find(ns + "fields"):
|
||||
print(field.attrib)
|
||||
if "title" in field.attrib:
|
||||
names[field.attrib["name"]] = field.attrib["title"]
|
||||
#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
|
||||
|
||||
for book in collection.iter(ns + "entry"):
|
||||
bookout = {}
|
||||
bookout["cover"] = book.find(ns + "cover").text if book.find(ns + "cover") != None else ""
|
||||
bookout["id"] = int(book.find(ns + "id").text)
|
||||
bookout["title"] = book.find(ns + "title").text if (book.find(ns + "title") != None) else ""
|
||||
bookout["isbn"] = book.find(ns + "isbn").text if (book.find(ns + "isbn") != None) else ""
|
||||
authors = []
|
||||
if book.find(ns + "authors") != None:
|
||||
for a in book.find(ns + "authors"):
|
||||
authors.append(a.text)
|
||||
bookout["author"] = "; ".join(authors)
|
||||
for datum in fields.keys():
|
||||
if book.find(ns + datum) != None:
|
||||
t = int(fields[datum]["type"])
|
||||
#simple text, paragraph, multiple choice, image
|
||||
if t == 1 or t == 2 or t == 3 or t == 10:
|
||||
#check if multiple entries are allowed
|
||||
#if so, put entries in a list
|
||||
if (int(fields[datum]["flags"]) & 0x01 & 0xff) != 0:
|
||||
bookout[datum] = []
|
||||
for sub in book.find(ns + datum):
|
||||
bookout[datum].append(sub.text)
|
||||
else:
|
||||
bookout[datum] = book.find(ns + datum).text
|
||||
|
||||
#date
|
||||
elif t == 12:
|
||||
bookout[datum] = ""
|
||||
if book.find(ns + "year") != None:
|
||||
bookout[datum] += book.find(ns + "year").text
|
||||
if book.find(ns + "month") != None:
|
||||
bookout[datum] += "-" + book.find(ns + "month").text
|
||||
if book.find(ns + "day") != None:
|
||||
bookout[datum] += "-" + book.find(ns + "day").text
|
||||
|
||||
elif t == 6: #Number
|
||||
bookout[datum] = book.find(ns + datum).text
|
||||
else:
|
||||
print(str(t) + ": " + datum)
|
||||
bookout[datum] = ""
|
||||
else:
|
||||
bookout[datum] = ""
|
||||
out.append(bookout.copy())
|
||||
return out.copy()
|
||||
|
||||
@ -48,8 +73,8 @@ def main():
|
||||
@app.route("/book/<int:id>")
|
||||
def book(id):
|
||||
data = updateData();
|
||||
book = next((item for item in data if item["id"] == id), {})
|
||||
return render_template("book.html", book = book, names = names)
|
||||
book = next((item for item in data if int(item["id"]) == id), {})
|
||||
return render_template("book.html", book = book, fields = fields)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True, host='0.0.0.0')
|
||||
|
Loading…
Reference in New Issue
Block a user