From c3f7ddff745d849e03200ddf49b696dd6f3eb462 Mon Sep 17 00:00:00 2001
From: clonejo <clonejo@shakik.de>
Date: Fri, 9 Feb 2024 00:35:12 +0100
Subject: [PATCH] contrib: bulk_location_update.py

---
 contrib/bulk_location_update.py | 59 +++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100755 contrib/bulk_location_update.py

diff --git a/contrib/bulk_location_update.py b/contrib/bulk_location_update.py
new file mode 100755
index 0000000..fd2752c
--- /dev/null
+++ b/contrib/bulk_location_update.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+
+from datetime import date
+
+import requests
+
+BASE = "https://in.ccc.ac"
+
+
+session = requests.Session()
+# session.auth = (":)", ":)")
+
+res = session.get(f"{BASE}/api/items")
+res.raise_for_status()
+items = res.json()
+
+is_in = input("is in (eg. R7): ")
+print(items[is_in])
+
+
+def format_last_updated(item) -> str:
+    last_updated = item["last_updated"]
+    if last_updated is None:
+        return ""
+    return date.fromtimestamp(item["last_updated"]).isoformat()[:-3]
+
+
+print(f"Items that should be in {is_in}:")
+print(
+    "\n".join(
+        (
+            f"{item['id']:<5} last_updated={format_last_updated(item)}"
+            for item in items.values()
+            if item["is_in"] == is_in
+        )
+    )
+)
+
+while True:
+    try:
+        item_id = input(f"item that is in {is_in}: ")
+    except KeyboardInterrupt:
+        break
+    if item_id == "":
+        break
+
+    if item_id not in items:
+        item_id = "K" + str(int(item_id))
+        if item_id not in items:
+            print(f"{item_id} and K{item_id} do not exist.")
+            continue
+    item = items[item_id]
+
+    print(f'location of {item_id}: {item["is_in"]} ->', end="")
+    item["is_in"] = is_in
+    print(f' {item["is_in"]}')
+
+    res = session.put(f"{BASE}/api/items/{item_id}", json=item)
+    res.raise_for_status()