forked from tanner/qotnews
use json type in sqlite.
This commit is contained in:
parent
4e04595415
commit
0bd0d40a31
|
@ -4,6 +4,7 @@ from sqlalchemy import create_engine, Column, String, ForeignKey, Integer
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
from sqlalchemy.types import JSON
|
||||||
|
|
||||||
engine = create_engine('sqlite:///data/qotnews.sqlite')
|
engine = create_engine('sqlite:///data/qotnews.sqlite')
|
||||||
Session = sessionmaker(bind=engine)
|
Session = sessionmaker(bind=engine)
|
||||||
|
@ -15,8 +16,8 @@ class Story(Base):
|
||||||
|
|
||||||
sid = Column(String(16), primary_key=True)
|
sid = Column(String(16), primary_key=True)
|
||||||
ref = Column(String(16), unique=True)
|
ref = Column(String(16), unique=True)
|
||||||
meta_json = Column(String)
|
meta = Column(JSON)
|
||||||
full_json = Column(String)
|
data = Column(JSON)
|
||||||
title = Column(String)
|
title = Column(String)
|
||||||
|
|
||||||
class Reflist(Base):
|
class Reflist(Base):
|
||||||
|
@ -36,19 +37,21 @@ def get_story(sid):
|
||||||
|
|
||||||
def put_story(story):
|
def put_story(story):
|
||||||
story = story.copy()
|
story = story.copy()
|
||||||
full_json = json.dumps(story)
|
data = {}
|
||||||
|
data.update(story)
|
||||||
|
|
||||||
story.pop('text', None)
|
meta = {}
|
||||||
story.pop('comments', None)
|
meta.update(story)
|
||||||
meta_json = json.dumps(story)
|
meta.pop('text', None)
|
||||||
|
meta.pop('comments', None)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session = Session()
|
session = Session()
|
||||||
s = Story(
|
s = Story(
|
||||||
sid=story['id'],
|
sid=story['id'],
|
||||||
ref=story['ref'],
|
ref=story['ref'],
|
||||||
full_json=full_json,
|
data=data,
|
||||||
meta_json=meta_json,
|
meta=meta,
|
||||||
title=story.get('title', None),
|
title=story.get('title', None),
|
||||||
)
|
)
|
||||||
session.merge(s)
|
session.merge(s)
|
||||||
|
@ -70,10 +73,10 @@ def get_reflist(amount):
|
||||||
|
|
||||||
def get_stories(amount):
|
def get_stories(amount):
|
||||||
session = Session()
|
session = Session()
|
||||||
q = session.query(Reflist, Story.meta_json).\
|
q = session.query(Reflist, Story.meta).\
|
||||||
order_by(Reflist.rid.desc()).\
|
|
||||||
join(Story).\
|
join(Story).\
|
||||||
filter(Story.title != None).\
|
filter(Story.title != None).\
|
||||||
|
order_by(Story.meta['date'].desc()).\
|
||||||
limit(amount)
|
limit(amount)
|
||||||
return [x[1] for x in q]
|
return [x[1] for x in q]
|
||||||
|
|
||||||
|
|
|
@ -43,8 +43,7 @@ cors = CORS(flask_app)
|
||||||
@flask_app.route('/api')
|
@flask_app.route('/api')
|
||||||
def api():
|
def api():
|
||||||
stories = database.get_stories(FEED_LENGTH)
|
stories = database.get_stories(FEED_LENGTH)
|
||||||
# hacky nested json
|
res = Response(json.dumps({"stories": stories}))
|
||||||
res = Response('{"stories":[' + ','.join(stories) + ']}')
|
|
||||||
res.headers['content-type'] = 'application/json'
|
res.headers['content-type'] = 'application/json'
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
@ -102,8 +101,7 @@ def submit():
|
||||||
def story(sid):
|
def story(sid):
|
||||||
story = database.get_story(sid)
|
story = database.get_story(sid)
|
||||||
if story:
|
if story:
|
||||||
# hacky nested json
|
res = Response(json.dumps({"story": story.data}))
|
||||||
res = Response('{"story":' + story.full_json + '}')
|
|
||||||
res.headers['content-type'] = 'application/json'
|
res.headers['content-type'] = 'application/json'
|
||||||
return res
|
return res
|
||||||
else:
|
else:
|
||||||
|
@ -127,7 +125,7 @@ def static_story(sid):
|
||||||
|
|
||||||
story = database.get_story(sid)
|
story = database.get_story(sid)
|
||||||
if not story: return abort(404)
|
if not story: return abort(404)
|
||||||
story = json.loads(story.full_json)
|
story = story.data
|
||||||
|
|
||||||
score = story['score']
|
score = story['score']
|
||||||
num_comments = story['num_comments']
|
num_comments = story['num_comments']
|
||||||
|
@ -170,8 +168,7 @@ def feed_thread():
|
||||||
item = ref_list[news_index]
|
item = ref_list[news_index]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
story_json = database.get_story(item['sid']).full_json
|
story = database.get_story(item['sid']).data
|
||||||
story = json.loads(story_json)
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
story = dict(id=item['sid'], ref=item['ref'], source=item['source'])
|
story = dict(id=item['sid'], ref=item['ref'], source=item['source'])
|
||||||
|
|
||||||
|
|
|
@ -50,10 +50,6 @@ class Feed extends React.Component {
|
||||||
const stories = this.state.stories;
|
const stories = this.state.stories;
|
||||||
const error = this.state.error;
|
const error = this.state.error;
|
||||||
|
|
||||||
if (stories) {
|
|
||||||
stories.sort((a, b) => b.date - a.date);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='container'>
|
<div className='container'>
|
||||||
<Helmet>
|
<Helmet>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user