change parsing to read type and flags

this means that everything can be parsed
This commit is contained in:
Adam Goldsmith 2015-08-13 10:29:18 -04:00
parent b4501d60bc
commit ab6b9bd247
2 changed files with 44 additions and 17 deletions

View File

@ -4,5 +4,7 @@
<p> <img src="{{ url_for('static', filename="library_files/" + book["cover"]) }}"> </p> <p> <img src="{{ url_for('static', filename="library_files/" + book["cover"]) }}"> </p>
{% endif %} {% endif %}
{% for datum in (book|dictsort) %} {% 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 %} {% endfor %}

57
test.py
View File

@ -7,10 +7,10 @@ app = Flask(__name__)
data = [] data = []
attributes= ["title", attributes= ["title",
"author", "authors",
"isbn"] "isbn"]
names = {} fields = {}
def updateData(): def updateData():
out = [] out = []
@ -21,22 +21,47 @@ def updateData():
ns = "{http://periapsis.org/tellico/}" ns = "{http://periapsis.org/tellico/}"
for field in collection.find(ns + "fields"): for field in collection.find(ns + "fields"):
print(field.attrib) #check if multiple entries are allowed
if "title" in field.attrib: if (int(field.attrib["flags"]) & 0x01 & 0xff) != 0:
names[field.attrib["name"]] = field.attrib["title"] #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"): for book in collection.iter(ns + "entry"):
bookout = {} bookout = {}
bookout["cover"] = book.find(ns + "cover").text if book.find(ns + "cover") != None else "" for datum in fields.keys():
bookout["id"] = int(book.find(ns + "id").text) if book.find(ns + datum) != None:
bookout["title"] = book.find(ns + "title").text if (book.find(ns + "title") != None) else "" t = int(fields[datum]["type"])
bookout["isbn"] = book.find(ns + "isbn").text if (book.find(ns + "isbn") != None) else "" #simple text, paragraph, multiple choice, image
authors = [] if t == 1 or t == 2 or t == 3 or t == 10:
if book.find(ns + "authors") != None: #check if multiple entries are allowed
for a in book.find(ns + "authors"): #if so, put entries in a list
authors.append(a.text) if (int(fields[datum]["flags"]) & 0x01 & 0xff) != 0:
bookout["author"] = "; ".join(authors) 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()) out.append(bookout.copy())
return out.copy() return out.copy()
@ -48,8 +73,8 @@ def main():
@app.route("/book/<int:id>") @app.route("/book/<int:id>")
def book(id): def book(id):
data = updateData(); data = updateData();
book = next((item for item in data if item["id"] == id), {}) book = next((item for item in data if int(item["id"]) == id), {})
return render_template("book.html", book = book, names = names) return render_template("book.html", book = book, fields = fields)
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0') app.run(debug=True, host='0.0.0.0')