use json type in sqlite.

This commit is contained in:
Jason Schwarzenberger 2020-11-09 15:45:10 +13:00
parent 4e04595415
commit 0bd0d40a31
3 changed files with 17 additions and 21 deletions

View File

@ -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]

View File

@ -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'])

View File

@ -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 (
<div className='container'>
<Helmet>