use db_session for articles and sections
This commit is contained in:
@@ -37,25 +37,20 @@ class ArtController(BaseController):
|
|||||||
return p1
|
return p1
|
||||||
|
|
||||||
def search(self,s):
|
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):
|
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):
|
def get_all_page(self,lim, off):
|
||||||
# try:
|
arts=db_session.query(Article).order_by(Article.published_date.desc()).limit(lim).offset(off).all()
|
||||||
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()
|
|
||||||
return arts
|
return arts
|
||||||
|
|
||||||
|
|
||||||
def get_section_page(self,section_id, lim, off):
|
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):
|
def get_organization_page(self,organization_id, lim, off):
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ class Article(Base):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def from_hash(cls, a):
|
def from_hash(cls, a):
|
||||||
fp = calc_fingerprint_h(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:
|
if aa is None:
|
||||||
clogger.debug( "new Article")
|
clogger.debug( "new Article")
|
||||||
if a["published"] is not None:
|
if a["published"] is not None:
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import json
|
|||||||
from src.database import db_session, read_json
|
from src.database import db_session, read_json
|
||||||
import flask
|
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: ((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)
|
flask.json.JSONEncoder.default = lambda self,obj: ((obj.__json__()) if isinstance(obj, (Base, Article,CrawlUrl)) else None)
|
||||||
from controller import controller
|
from controller import controller
|
||||||
@@ -38,7 +39,7 @@ def index():
|
|||||||
@article_pages.route("/<int:id>",methods=['PUT'])
|
@article_pages.route("/<int:id>",methods=['PUT'])
|
||||||
@article_pages.route("/<int:id>.json",methods=['PUT'])
|
@article_pages.route("/<int:id>.json",methods=['PUT'])
|
||||||
def update(id):
|
def update(id):
|
||||||
article=Article.query.get(id)
|
article=db_session.query(Article).get(id)
|
||||||
clogger.info(request.data)
|
clogger.info(request.data)
|
||||||
a=request.get_json()
|
a=request.get_json()
|
||||||
article.text=a["text"]
|
article.text=a["text"]
|
||||||
@@ -49,7 +50,7 @@ def update(id):
|
|||||||
@article_pages.route("/<int:id>",methods=['GET'])
|
@article_pages.route("/<int:id>",methods=['GET'])
|
||||||
@article_pages.route("/<int:id>.json",methods=['GET'])
|
@article_pages.route("/<int:id>.json",methods=['GET'])
|
||||||
def get(id):
|
def get(id):
|
||||||
article=Article.query.get(id)
|
article=db_session.query(Article).get(id)
|
||||||
# clogger.info(article)
|
# clogger.info(article)
|
||||||
# article=ArticleSchema().dump(article)[0]
|
# article=ArticleSchema().dump(article)[0]
|
||||||
return jsonify(article=article)
|
return jsonify(article=article)
|
||||||
@@ -57,7 +58,7 @@ def get(id):
|
|||||||
@article_pages.route("/<int:id>",methods=['DELETE'])
|
@article_pages.route("/<int:id>",methods=['DELETE'])
|
||||||
@article_pages.route("/<int:id>.json",methods=['DELETE'])
|
@article_pages.route("/<int:id>.json",methods=['DELETE'])
|
||||||
def delete(id):
|
def delete(id):
|
||||||
article=Article.query.get(id)
|
article=db_session.query(Article).get(id)
|
||||||
# clogger.info(id)
|
# clogger.info(id)
|
||||||
if article != None:
|
if article != None:
|
||||||
db_session.delete(article)
|
db_session.delete(article)
|
||||||
|
|||||||
@@ -6,11 +6,10 @@ from src import clogger
|
|||||||
|
|
||||||
class BaseController():
|
class BaseController():
|
||||||
def get(self,id):
|
def get(self,id):
|
||||||
return self.__myclass__.query.get(id)
|
return db_session.query(self.__myclass__).get(id)
|
||||||
|
|
||||||
def create(self,request):
|
def create(self,request):
|
||||||
d=read_json(request)
|
d=read_json(request)
|
||||||
clogger.info(d)
|
|
||||||
if d.has_key(self.__jsonid__):
|
if d.has_key(self.__jsonid__):
|
||||||
d= d[self.__jsonid__]
|
d= d[self.__jsonid__]
|
||||||
o=self.__myclass__()
|
o=self.__myclass__()
|
||||||
@@ -25,12 +24,7 @@ class BaseController():
|
|||||||
return o, errors
|
return o, errors
|
||||||
|
|
||||||
def get_all(self):
|
def get_all(self):
|
||||||
# try:
|
ar = db_session.query(self.__myclass__).all()
|
||||||
ar = self.__myclass__.query.all()
|
|
||||||
# except Error,e:
|
|
||||||
# db_session.rollback()
|
|
||||||
# clogger.error(e)
|
|
||||||
# ar=[]
|
|
||||||
return ar
|
return ar
|
||||||
|
|
||||||
def delete(self,id):
|
def delete(self,id):
|
||||||
@@ -51,6 +45,5 @@ class BaseController():
|
|||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
db_session.rollback()
|
db_session.rollback()
|
||||||
errors.append(e.message)
|
errors.append(e.message)
|
||||||
|
|
||||||
return o,errors
|
return o,errors
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ from database_mbase import MyBase,MyBase2
|
|||||||
#Base = declarative_base()
|
#Base = declarative_base()
|
||||||
#Base.query = db_session.query_property()
|
#Base.query = db_session.query_property()
|
||||||
Base=declarative_base(cls=MyBase)
|
Base=declarative_base(cls=MyBase)
|
||||||
Base.query = db_session.query_property()
|
#Base.query = db_session.query_property()
|
||||||
|
|
||||||
Base2 = declarative_base(cls=MyBase2)
|
Base2 = declarative_base(cls=MyBase2)
|
||||||
Base2.query = db_session2.query_property()
|
Base2.query = db_session2.query_property()
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ class OrgController(BaseController):
|
|||||||
def get_articles(self,id):
|
def get_articles(self,id):
|
||||||
sid=db_session.query(Section.id).filter(Section.organization_id==id).all()
|
sid=db_session.query(Section.id).filter(Section.organization_id==id).all()
|
||||||
sid=map(lambda a:a[0], sid)
|
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
|
return articles
|
||||||
|
|
||||||
def get_by_key_articles(self,key):
|
def get_by_key_articles(self,key):
|
||||||
@@ -16,6 +16,6 @@ class OrgController(BaseController):
|
|||||||
return self.get_articles(org.id)
|
return self.get_articles(org.id)
|
||||||
|
|
||||||
def get_by_key(self,key):
|
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()
|
controller=OrgController()
|
||||||
|
|||||||
@@ -5,9 +5,4 @@ class OrgController(BaseController):
|
|||||||
__myclass__=Organizationtype
|
__myclass__=Organizationtype
|
||||||
__jsonid__='organizationtype'
|
__jsonid__='organizationtype'
|
||||||
|
|
||||||
#def get_all():
|
|
||||||
# Organizationtype.query.all()
|
|
||||||
|
|
||||||
#def get(id):
|
|
||||||
# Organizationtype.query.get(id)
|
|
||||||
controller=OrgController()
|
controller=OrgController()
|
||||||
|
|||||||
@@ -34,11 +34,6 @@ class FullSectionSchema(Schema):
|
|||||||
updated_at = fields.DateTime()
|
updated_at = fields.DateTime()
|
||||||
created_at = fields.DateTime()
|
created_at = fields.DateTime()
|
||||||
|
|
||||||
# @post_load
|
|
||||||
# def make_section(self, data):
|
|
||||||
# return Section.deserialize(data)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Section(Base):
|
class Section(Base):
|
||||||
__tablename__ = 'sections'
|
__tablename__ = 'sections'
|
||||||
@@ -78,7 +73,7 @@ class Section(Base):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def find_or_create(cls, fname):
|
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:
|
if s is None:
|
||||||
s=Section(fname)
|
s=Section(fname)
|
||||||
db_session.add(s)
|
db_session.add(s)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import flask
|
|||||||
@section_pages.route("")
|
@section_pages.route("")
|
||||||
@section_pages.route(".json")
|
@section_pages.route(".json")
|
||||||
def index():
|
def index():
|
||||||
sections=Section.query.all()
|
sections=controller.get_all()
|
||||||
return jsonify(sections=sections)
|
return jsonify(sections=sections)
|
||||||
|
|
||||||
|
|
||||||
@@ -29,9 +29,7 @@ def update(id):
|
|||||||
@section_pages.route("/<int:id>",methods=['GET'])
|
@section_pages.route("/<int:id>",methods=['GET'])
|
||||||
@section_pages.route("/<int:id>.json",methods=['GET'])
|
@section_pages.route("/<int:id>.json",methods=['GET'])
|
||||||
def get(id):
|
def get(id):
|
||||||
section=Section.query.get(id)
|
section=controller.get(id)
|
||||||
clogger.info(section)
|
|
||||||
# section=SectionSchema().dump(section)[0]
|
|
||||||
return jsonify(section=section)
|
return jsonify(section=section)
|
||||||
|
|
||||||
|
|
||||||
@@ -39,7 +37,7 @@ def get(id):
|
|||||||
@section_pages.route("/<int:id>/articles.json",methods=['GET'])
|
@section_pages.route("/<int:id>/articles.json",methods=['GET'])
|
||||||
def get_articles(id):
|
def get_articles(id):
|
||||||
v=request.values
|
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)
|
articles=article_controller.get_section_page(id,pp,o)
|
||||||
|
|
||||||
resp = jsonify(articles=articles)
|
resp = jsonify(articles=articles)
|
||||||
|
|||||||
Reference in New Issue
Block a user