From d386cf78620d7139758aac403afc37a5ab24d328 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 4 Jun 2018 15:07:48 +0100 Subject: [PATCH] Tool for populating table of references. --- sourcing/cli.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/sourcing/cli.py b/sourcing/cli.py index 37f1cb2..8604652 100644 --- a/sourcing/cli.py +++ b/sourcing/cli.py @@ -34,3 +34,26 @@ def delete_item(hashid): item = model.Item.get_by_hashid(hashid) database.session.delete(item) database.session.commit() + +@app.cli.command() +def populate_references(): + home = 'http://localhost:5000/' + seen = set() + for link_obj in model.XanaLink.query: + link = link_obj.parse() + for items in link['facets']: + for i in items: + k, _, v = i.partition(': ') + if k == 'span' and ',' in v: + v = v.partition(',')[0] + item = model.Item.from_external(v, home=home) + if item: + print(link_obj.id, '->', item.id) + as_tuple = (link_obj.id, item.id) + if as_tuple in seen: + continue + ref = model.Reference(subject_id=link_obj.id, object_id=item.id) + database.session.add(ref) + seen.add(as_tuple) + + database.session.commit()