forked from tanner/qotnews
Integrate sqlite database with server
This commit is contained in:
@@ -3,6 +3,7 @@ import json
|
||||
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
|
||||
|
||||
engine = create_engine('sqlite:///data/qotnews.sqlite')
|
||||
Session = sessionmaker(bind=engine)
|
||||
@@ -15,6 +16,8 @@ class Story(Base):
|
||||
sid = Column(String(16), primary_key=True)
|
||||
meta_json = Column(String)
|
||||
full_json = Column(String)
|
||||
title = Column(String)
|
||||
date = Column(Integer)
|
||||
|
||||
class Reflist(Base):
|
||||
__tablename__ = 'reflist'
|
||||
@@ -26,24 +29,26 @@ class Reflist(Base):
|
||||
def init():
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
def get_meta(sid):
|
||||
def get_story(sid):
|
||||
session = Session()
|
||||
return session.query(Story).get(sid).meta_json
|
||||
|
||||
def get_full(sid):
|
||||
session = Session()
|
||||
return session.query(Story).get(sid).full_json
|
||||
return session.query(Story).get(sid)
|
||||
|
||||
def put_story(story):
|
||||
full_json = json.dumps(story)
|
||||
|
||||
story.pop('text')
|
||||
story.pop('comments')
|
||||
story.pop('text', None)
|
||||
story.pop('comments', None)
|
||||
meta_json = json.dumps(story)
|
||||
|
||||
try:
|
||||
session = Session()
|
||||
s = Story(sid=story['id'], full_json=full_json, meta_json=meta_json)
|
||||
s = Story(
|
||||
sid=story['id'],
|
||||
full_json=full_json,
|
||||
meta_json=meta_json,
|
||||
title=story.get('title', None),
|
||||
date=story.get('date', None),
|
||||
)
|
||||
session.merge(s)
|
||||
session.commit()
|
||||
except:
|
||||
@@ -52,11 +57,24 @@ def put_story(story):
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
def search(q):
|
||||
session = Session()
|
||||
return session.query(Story).filter(Story.title.contains(q))
|
||||
|
||||
def get_reflist(amount):
|
||||
session = Session()
|
||||
q = session.query(Reflist).order_by(Reflist.rid.desc()).limit(amount)
|
||||
return [dict(ref=x.ref, sid=x.sid) for x in q.all()]
|
||||
|
||||
def get_stories(amount):
|
||||
session = Session()
|
||||
q = session.query(Reflist, Story.meta_json).\
|
||||
order_by(Reflist.rid.desc()).\
|
||||
join(Story).\
|
||||
filter(Story.title != None).\
|
||||
limit(amount)
|
||||
return [x[1] for x in q]
|
||||
|
||||
def put_ref(ref, sid):
|
||||
try:
|
||||
session = Session()
|
||||
@@ -69,5 +87,18 @@ def put_ref(ref, sid):
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
def del_ref(ref):
|
||||
try:
|
||||
session = Session()
|
||||
session.query(Reflist).filter(Reflist.ref==ref).delete()
|
||||
session.commit()
|
||||
except:
|
||||
session.rollback()
|
||||
raise
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
init()
|
||||
|
||||
print(get_stories(5))
|
||||
|
Reference in New Issue
Block a user