From d8c279001e58da51331a2762fb4c0272229b0f5a Mon Sep 17 00:00:00 2001
From: Edward Betts <edward@4angle.com>
Date: Fri, 4 Oct 2019 12:02:45 +0100
Subject: [PATCH] Create depicts object during save, if needed.

---
 app.py | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/app.py b/app.py
index b36aaef..b543819 100755
--- a/app.py
+++ b/app.py
@@ -153,6 +153,29 @@ def current_url():
     args.update(request.args)
     return url_for(request.endpoint, **args)
 
+def create_depicts_item(item_id):
+    qid = f'Q{item_id}'
+    entity = mediawiki.get_entity(qid)
+    if 'en' in entity['labels']:
+        label = entity['labels']['en']['value']
+    else:
+        label = None
+
+    if 'en' in entity['descriptions']:
+        description = entity['descriptions']['en']['value']
+    else:
+        description = None
+
+    if 'en' in entity['aliases']:
+        alt_labels = {alt['value'] for alt in entity['aliases']['en']}
+    else:
+        alt_labels = set()
+
+    return DepictsItem(label=label,
+                       description=description,
+                       alt_labels=alt_labels,
+                       count=0)
+
 @app.before_request
 def init_profile():
     g.profiling = []
@@ -175,6 +198,13 @@ def save(item_id):
 
     for depicts_qid in depicts:
         depicts_id = int(depicts_qid[1:])
+
+        depicts_item = DepictsItem.query.get(depicts_id)
+        if depicts_item is None:
+            depicts_item = create_depicts_item(depicts_id)
+            database.session.add(depicts_item)
+            database.session.commit()
+
         r = create_claim(item_id, depicts_id, token)
         reply = r.json()
         if 'error' in reply: