From 88d22166278d3828de82e6d48525f166de023464 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 3 Oct 2020 23:42:21 +0000 Subject: [PATCH] Add a script to delete a story --- apiserver/delete-story.py | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 apiserver/delete-story.py diff --git a/apiserver/delete-story.py b/apiserver/delete-story.py new file mode 100644 index 0000000..a8994fd --- /dev/null +++ b/apiserver/delete-story.py @@ -0,0 +1,50 @@ +import database +import search +import sys + +import json +import requests + +database.init() +search.init() + +def database_del_story(sid): + try: + session = database.Session() + session.query(database.Story).filter(database.Story.sid==sid).delete() + session.commit() + except: + session.rollback() + raise + finally: + session.close() + +def search_del_story(sid): + try: + r = requests.delete(search.MEILI_URL + 'indexes/qotnews/documents/'+sid, timeout=2) + if r.status_code != 202: + raise Exception('Bad response code ' + str(r.status_code)) + return r.json() + except KeyboardInterrupt: + raise + except BaseException as e: + logging.error('Problem deleting MeiliSearch story: {}'.format(str(e))) + return False + +if __name__ == '__main__': + if len(sys.argv) == 2: + sid = sys.argv[1] + else: + print('Usage: python delete-story.py [story id]') + exit(1) + + story = database.get_story(sid) + + if story: + print('Deleting story:') + print(story.title) + database_del_story(sid) + search_del_story(sid) + database.del_ref(story.ref) + else: + print('Story not found. Exiting.')