From 4a935ee4a99729df8cf82993e6c92f90dc191869 Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Tue, 21 Feb 2017 19:55:19 +0100 Subject: [PATCH] use db_session for articles and sections --- articles/controller.py | 13 ++++--------- articles/model.py | 2 +- articles/views.py | 7 ++++--- controller.py | 11 ++--------- database.py | 2 +- organizations/controller.py | 4 ++-- organizationtypes/controller.py | 5 ----- sections/model.py | 7 +------ sections/views.py | 8 +++----- 9 files changed, 18 insertions(+), 41 deletions(-) diff --git a/articles/controller.py b/articles/controller.py index 756dbed..55b91b0 100644 --- a/articles/controller.py +++ b/articles/controller.py @@ -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): diff --git a/articles/model.py b/articles/model.py index 9d6ff99..5f495f9 100644 --- a/articles/model.py +++ b/articles/model.py @@ -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: diff --git a/articles/views.py b/articles/views.py index 93aaba2..b53e846 100644 --- a/articles/views.py +++ b/articles/views.py @@ -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("/",methods=['PUT']) @article_pages.route("/.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("/",methods=['GET']) @article_pages.route("/.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("/",methods=['DELETE']) @article_pages.route("/.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) diff --git a/controller.py b/controller.py index 3506ae4..a3b9ad9 100644 --- a/controller.py +++ b/controller.py @@ -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 diff --git a/database.py b/database.py index 188bf0f..0758c7b 100644 --- a/database.py +++ b/database.py @@ -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() diff --git a/organizations/controller.py b/organizations/controller.py index 63d10f4..05b5699 100644 --- a/organizations/controller.py +++ b/organizations/controller.py @@ -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() diff --git a/organizationtypes/controller.py b/organizationtypes/controller.py index 94de989..1e098b8 100644 --- a/organizationtypes/controller.py +++ b/organizationtypes/controller.py @@ -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() diff --git a/sections/model.py b/sections/model.py index b37b435..2c49b91 100644 --- a/sections/model.py +++ b/sections/model.py @@ -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) diff --git a/sections/views.py b/sections/views.py index 21e14ca..37a58a8 100644 --- a/sections/views.py +++ b/sections/views.py @@ -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("/",methods=['GET']) @section_pages.route("/.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("//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)