use db_session for articles and sections

This commit is contained in:
Andreas Stephanides
2017-02-21 19:55:19 +01:00
parent ae2f61485e
commit 4a935ee4a9
9 changed files with 18 additions and 41 deletions

View File

@@ -37,25 +37,20 @@ class ArtController(BaseController):
return p1
def search(self,s):
return Article.query.filter(Article.title.like("%"+s+"%")).order_by(Article.published_date.desc()).limit(20).all()
return db_session.query(Article).query.filter(Article.title.like("%"+s+"%")).order_by(Article.published_date.desc()).limit(20).all()
def get_all(self):
return Article.query.order_by(Article.published_date.desc()).all()
return db_session.query(Article).order_by(Article.published_date.desc()).all()
def get_all_page(self,lim, off):
# try:
arts=Article.query.order_by(Article.published_date.desc()).limit(lim).offset(off).all()
# except exc.InvalidRequestError, e:
# clogger.error(e)
# db_session.rollback()
# arts=Article.query.order_by(Article.published_date.desc()).limit(lim).offset(off).all()
arts=db_session.query(Article).order_by(Article.published_date.desc()).limit(lim).offset(off).all()
return arts
def get_section_page(self,section_id, lim, off):
return Article.query.filter(Article.section_id==section_id).order_by(Article.published_date.desc()).limit(lim).offset(off).all()
return db_session.query(Article).filter(Article.section_id==section_id).order_by(Article.published_date.desc()).limit(lim).offset(off).all()
def get_organization_page(self,organization_id, lim, off):

View File

@@ -128,7 +128,7 @@ class Article(Base):
@classmethod
def from_hash(cls, a):
fp = calc_fingerprint_h(a)
aa = Article.query.filter(Article.fingerprint==fp).first()
aa = db_session.query(Article).filter(Article.fingerprint==fp).first()
if aa is None:
clogger.debug( "new Article")
if a["published"] is not None:

View File

@@ -11,6 +11,7 @@ import json
from src.database import db_session, read_json
import flask
#flask.json.JSONEncoder.default = lambda self,obj: ((ArticleSchema().dump(obj)[0]) if isinstance(obj, Article) else None)
flask.json.JSONEncoder.default = lambda self,obj: ((obj.__json__()) if isinstance(obj, (Base, Article,CrawlUrl)) else None)
from controller import controller
@@ -38,7 +39,7 @@ def index():
@article_pages.route("/<int:id>",methods=['PUT'])
@article_pages.route("/<int:id>.json",methods=['PUT'])
def update(id):
article=Article.query.get(id)
article=db_session.query(Article).get(id)
clogger.info(request.data)
a=request.get_json()
article.text=a["text"]
@@ -49,7 +50,7 @@ def update(id):
@article_pages.route("/<int:id>",methods=['GET'])
@article_pages.route("/<int:id>.json",methods=['GET'])
def get(id):
article=Article.query.get(id)
article=db_session.query(Article).get(id)
# clogger.info(article)
# article=ArticleSchema().dump(article)[0]
return jsonify(article=article)
@@ -57,7 +58,7 @@ def get(id):
@article_pages.route("/<int:id>",methods=['DELETE'])
@article_pages.route("/<int:id>.json",methods=['DELETE'])
def delete(id):
article=Article.query.get(id)
article=db_session.query(Article).get(id)
# clogger.info(id)
if article != None:
db_session.delete(article)

View File

@@ -6,11 +6,10 @@ from src import clogger
class BaseController():
def get(self,id):
return self.__myclass__.query.get(id)
return db_session.query(self.__myclass__).get(id)
def create(self,request):
d=read_json(request)
clogger.info(d)
if d.has_key(self.__jsonid__):
d= d[self.__jsonid__]
o=self.__myclass__()
@@ -25,12 +24,7 @@ class BaseController():
return o, errors
def get_all(self):
# try:
ar = self.__myclass__.query.all()
# except Error,e:
# db_session.rollback()
# clogger.error(e)
# ar=[]
ar = db_session.query(self.__myclass__).all()
return ar
def delete(self,id):
@@ -51,6 +45,5 @@ class BaseController():
except IntegrityError as e:
db_session.rollback()
errors.append(e.message)
return o,errors

View File

@@ -51,7 +51,7 @@ from database_mbase import MyBase,MyBase2
#Base = declarative_base()
#Base.query = db_session.query_property()
Base=declarative_base(cls=MyBase)
Base.query = db_session.query_property()
#Base.query = db_session.query_property()
Base2 = declarative_base(cls=MyBase2)
Base2.query = db_session2.query_property()

View File

@@ -8,7 +8,7 @@ class OrgController(BaseController):
def get_articles(self,id):
sid=db_session.query(Section.id).filter(Section.organization_id==id).all()
sid=map(lambda a:a[0], sid)
articles=Article.query.filter(Article.section_id.in_(sid)).all()
articles=db_session.query(Article).filter(Article.section_id.in_(sid)).all()
return articles
def get_by_key_articles(self,key):
@@ -16,6 +16,6 @@ class OrgController(BaseController):
return self.get_articles(org.id)
def get_by_key(self,key):
return Organization.query.filter(Organization.key==key).one()
return db_session.query(Organization).filter(Organization.key==key).one()
controller=OrgController()

View File

@@ -5,9 +5,4 @@ class OrgController(BaseController):
__myclass__=Organizationtype
__jsonid__='organizationtype'
#def get_all():
# Organizationtype.query.all()
#def get(id):
# Organizationtype.query.get(id)
controller=OrgController()

View File

@@ -34,11 +34,6 @@ class FullSectionSchema(Schema):
updated_at = fields.DateTime()
created_at = fields.DateTime()
# @post_load
# def make_section(self, data):
# return Section.deserialize(data)
class Section(Base):
__tablename__ = 'sections'
@@ -78,7 +73,7 @@ class Section(Base):
@classmethod
def find_or_create(cls, fname):
s=Section.query.filter(Section.foreign_name==fname).first()
s=db_session.query(Section).filter(Section.foreign_name==fname).first()
if s is None:
s=Section(fname)
db_session.add(s)

View File

@@ -15,7 +15,7 @@ import flask
@section_pages.route("")
@section_pages.route(".json")
def index():
sections=Section.query.all()
sections=controller.get_all()
return jsonify(sections=sections)
@@ -29,9 +29,7 @@ def update(id):
@section_pages.route("/<int:id>",methods=['GET'])
@section_pages.route("/<int:id>.json",methods=['GET'])
def get(id):
section=Section.query.get(id)
clogger.info(section)
# section=SectionSchema().dump(section)[0]
section=controller.get(id)
return jsonify(section=section)
@@ -39,7 +37,7 @@ def get(id):
@section_pages.route("/<int:id>/articles.json",methods=['GET'])
def get_articles(id):
v=request.values
pp,o=article_controller.pagination_params(v) # extract per_page and offset from params
pp,o=article_controller.pagination_params(v)
articles=article_controller.get_section_page(id,pp,o)
resp = jsonify(articles=articles)