From ab6b9bd2476b8c8e73212f6974214d074e5ac608 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 13 Aug 2015 10:29:18 -0400 Subject: [PATCH] change parsing to read type and flags this means that everything can be parsed --- templates/book.html | 4 +++- test.py | 57 ++++++++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/templates/book.html b/templates/book.html index 6bf9212..836cce2 100644 --- a/templates/book.html +++ b/templates/book.html @@ -4,5 +4,7 @@

{% endif %} {% for datum in (book|dictsort) %} -

{{names[datum[0]]}}: {{ datum[1] }}

+ {% if datum[1] is not equalto "" %} +

{{fields[datum[0]]["title"]}}: {{ datum[1] }}

+ {% endif %} {% endfor %} diff --git a/test.py b/test.py index 39defb8..3808cbd 100644 --- a/test.py +++ b/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/") 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')