From 0bd0d40a311734c483acba730d26dffb7665bbeb Mon Sep 17 00:00:00 2001 From: Jason Schwarzenberger Date: Mon, 9 Nov 2020 15:45:10 +1300 Subject: [PATCH] use json type in sqlite. --- apiserver/database.py | 23 +++++++++++++---------- apiserver/server.py | 11 ++++------- webclient/src/Feed.js | 4 ---- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/apiserver/database.py b/apiserver/database.py index 24582c6..cada255 100644 --- a/apiserver/database.py +++ b/apiserver/database.py @@ -4,6 +4,7 @@ from sqlalchemy import create_engine, Column, String, ForeignKey, Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from sqlalchemy.exc import IntegrityError +from sqlalchemy.types import JSON engine = create_engine('sqlite:///data/qotnews.sqlite') Session = sessionmaker(bind=engine) @@ -15,8 +16,8 @@ class Story(Base): sid = Column(String(16), primary_key=True) ref = Column(String(16), unique=True) - meta_json = Column(String) - full_json = Column(String) + meta = Column(JSON) + data = Column(JSON) title = Column(String) class Reflist(Base): @@ -36,19 +37,21 @@ def get_story(sid): def put_story(story): story = story.copy() - full_json = json.dumps(story) + data = {} + data.update(story) - story.pop('text', None) - story.pop('comments', None) - meta_json = json.dumps(story) + meta = {} + meta.update(story) + meta.pop('text', None) + meta.pop('comments', None) try: session = Session() s = Story( sid=story['id'], ref=story['ref'], - full_json=full_json, - meta_json=meta_json, + data=data, + meta=meta, title=story.get('title', None), ) session.merge(s) @@ -70,10 +73,10 @@ def get_reflist(amount): def get_stories(amount): session = Session() - q = session.query(Reflist, Story.meta_json).\ - order_by(Reflist.rid.desc()).\ + q = session.query(Reflist, Story.meta).\ join(Story).\ filter(Story.title != None).\ + order_by(Story.meta['date'].desc()).\ limit(amount) return [x[1] for x in q] diff --git a/apiserver/server.py b/apiserver/server.py index cdf740f..9a7a8a5 100644 --- a/apiserver/server.py +++ b/apiserver/server.py @@ -43,8 +43,7 @@ cors = CORS(flask_app) @flask_app.route('/api') def api(): stories = database.get_stories(FEED_LENGTH) - # hacky nested json - res = Response('{"stories":[' + ','.join(stories) + ']}') + res = Response(json.dumps({"stories": stories})) res.headers['content-type'] = 'application/json' return res @@ -102,8 +101,7 @@ def submit(): def story(sid): story = database.get_story(sid) if story: - # hacky nested json - res = Response('{"story":' + story.full_json + '}') + res = Response(json.dumps({"story": story.data})) res.headers['content-type'] = 'application/json' return res else: @@ -127,7 +125,7 @@ def static_story(sid): story = database.get_story(sid) if not story: return abort(404) - story = json.loads(story.full_json) + story = story.data score = story['score'] num_comments = story['num_comments'] @@ -170,8 +168,7 @@ def feed_thread(): item = ref_list[news_index] try: - story_json = database.get_story(item['sid']).full_json - story = json.loads(story_json) + story = database.get_story(item['sid']).data except AttributeError: story = dict(id=item['sid'], ref=item['ref'], source=item['source']) diff --git a/webclient/src/Feed.js b/webclient/src/Feed.js index ef7e09d..75ad76e 100644 --- a/webclient/src/Feed.js +++ b/webclient/src/Feed.js @@ -50,10 +50,6 @@ class Feed extends React.Component { const stories = this.state.stories; const error = this.state.error; - if (stories) { - stories.sort((a, b) => b.date - a.date); - } - return (