diff --git a/__init__.py b/__init__.py index 9436948..0817fc4 100644 --- a/__init__.py +++ b/__init__.py @@ -98,7 +98,7 @@ def home(): app.register_blueprint(article_pages, url_prefix='/articles') app.register_blueprint(section_pages, url_prefix='/sections') app.register_blueprint(compiler_pages, url_prefix='/compiler') -app.register_blueprint(organization_pages, url_prefix='/organizations') +#pp.register_blueprint(organization_pages, url_prefix='/organizations') app.register_blueprint(organization_pages, url_prefix='/organizations') app.register_blueprint(organizationtype_pages, url_prefix='/organizationtypes') diff --git a/articles/controller.py b/articles/controller.py index 4c6db9e..2684ee1 100644 --- a/articles/controller.py +++ b/articles/controller.py @@ -44,13 +44,13 @@ class ArtController(BaseController): return Article.query.filter(Article.section_id==section_id).order_by(Article.published_date.desc()).limit(lim).offset(off).all() - def get_section_page(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() - def section_count(section_id): + def section_count(self,section_id): return Article.query.filter(Article.section_id==section_id).count() - def count(): + def count(self): return Article.query.count() diff --git a/articles/model.py b/articles/model.py index 6f7f21a..cb19507 100644 --- a/articles/model.py +++ b/articles/model.py @@ -41,6 +41,32 @@ def calc_fingerprint_h(a): h.update(unicode(pp)) return h.hexdigest() + + +class FullArticleSchema(Schema): + id=fields.Integer() + parent_id=fields.Integer(allow_none=True) + url =fields.String() + is_primary=fields.Boolean(allow_none=True) + fingerprint=fields.String() + hash=fields.String(allow_none=True) + last_fetched=fields.DateTime(allow_none=True) + first_fetched=fields.DateTime(allow_none=True) + published_date=fields.DateTime() + date=fields.DateTime(allow_none=True) + text=fields.String() + title=fields.String() + author=fields.String(allow_none=True) + section_id=fields.Integer() + sourcetype =fields.String() + image =fields.String(allow_none=True) +# @post_load +# def make_article(self, data): +# return Article.deserialize(data) + + + + class Article(Base): __tablename__ = 'articles' id = Column(Integer, primary_key=True) @@ -60,7 +86,10 @@ class Article(Base): section_id=Column(Integer, ForeignKey('sections.id')) sourcetype = Column(String(250)) image=Column(String(250)) - + __schema__=FullArticleSchema + __jsonid__='article' + __whiteattrs__= [] + __jsonattrs__=None def __init__(self, url=None,title=None, published_date=None): self.url=url self.title=title @@ -69,16 +98,16 @@ class Article(Base): def __json__(self): return ArticleSchema().dump(self)[0] - def dict(self): - return {"id": str(int(self.id)), "title": self.title, "text": self.text, "author": self.author, "section":self.section, "sourcetype": self.sourcetype, "last_fetched": self.last_fetched, "first_fetched": self.first_fetched, "published_date": self.published_date, "date": self.date,"image": self.image, "url": self.url} +# def dict(self): +# return {"id": str(int(self.id)), "title": self.title, "text": self.text, "author": self.author, "section":self.section, "sourcetype": self.sourcetype, "last_fetched": self.last_fetched, "first_fetched": self.first_fetched, "published_date": self.published_date, "date": self.date,"image": self.image, "url": self.url} - @classmethod - def deserialize(cls,data): - a=Article() - for c in Article.__table__.columns: - if data.has_key(c.key): - setattr(a, c.key,data[c.key]) - return a +# @classmethod +# def deserialize(cls,data): +# a=Article() +# for c in Article.__table__.columns: +# if data.has_key(c.key): +# setattr(a, c.key,data[c.key]) +# return a # @classmethod # def sections(self): @@ -131,27 +160,7 @@ class Article(Base): #json.JSONEncoder.default = lambda self,obj: ((ArticleSchema().dump(obj)[0]) if isinstance(obj, Article) else None) -class FullArticleSchema(Schema): - id=fields.Integer() - parent_id=fields.Integer(allow_none=True) - url =fields.String() - is_primary=fields.Boolean(allow_none=True) - fingerprint=fields.String() - hash=fields.String(allow_none=True) - last_fetched=fields.DateTime(allow_none=True) - first_fetched=fields.DateTime(allow_none=True) - published_date=fields.DateTime() - date=fields.DateTime(allow_none=True) - text=fields.String() - title=fields.String() - author=fields.String(allow_none=True) - section_id=fields.Integer() - sourcetype =fields.String() - image =fields.String(allow_none=True) - @post_load - def make_article(self, data): - return Article.deserialize(data) - + class ArticleSchema(Schema): diff --git a/compiler/comp/fb.py b/compiler/comp/fb.py index 2a0ac64..d884fab 100644 --- a/compiler/comp/fb.py +++ b/compiler/comp/fb.py @@ -34,7 +34,7 @@ def fbfeedelement(h): return art -def fbfeed(url, raw): +def fbfeed(url, raw, params={}): js = json.loads(raw) arts=[] u=urlparse.urlparse(url) diff --git a/controller.py b/controller.py index 00f0a20..de55d54 100644 --- a/controller.py +++ b/controller.py @@ -11,9 +11,10 @@ class BaseController(): def create(self,request): d=read_json(request) clogger.info(d) - clogger.info(d[self.__jsonid__]) + if d.has_key(self.__jsonid__): + d= d[self.__jsonid__] o=self.__myclass__() - success, errors=o.update(d[self.__jsonid__]) + success, errors=o.update(d) if success: db_session.add(o) try: diff --git a/organizations/views.py b/organizations/views.py index 0eec873..8e287cd 100644 --- a/organizations/views.py +++ b/organizations/views.py @@ -70,4 +70,8 @@ def delete(id): @organization_pages.route(".json",methods=['POST']) def create(): organization,errors=controller.create(request) - return jsonify(organization=organization,errors=errors) + if len(errors)>0: + http_code=422 + else: + http_code=200 + return jsonify(organization=organization,errors=errors), http_code diff --git a/sections/controller.py b/sections/controller.py new file mode 100644 index 0000000..ecb885b --- /dev/null +++ b/sections/controller.py @@ -0,0 +1,9 @@ +from src.controller import BaseController +from model import Section +from src.database import db_session +class SecController(BaseController): + __myclass__=Section + __jsonid__='section' + +controller=SecController() + diff --git a/sections/views.py b/sections/views.py index 477d382..21e14ca 100644 --- a/sections/views.py +++ b/sections/views.py @@ -7,6 +7,7 @@ from datetime import datetime import json from src import clogger from src.articles import controller as article_controller +from src.sections.controller import controller from src.database import db_session, read_json import flask @@ -21,12 +22,8 @@ def index(): @section_pages.route("/",methods=['PUT']) @section_pages.route("/.json",methods=['PUT']) def update(id): - section=Section.query.get(id) - clogger.info(request.data) - a=read_json(request) - section.update(a["section"]) - db_session.commit() - return jsonify(section=section) + section,errors=controller.update(id,request) + return jsonify(section=section, errors=errors) @section_pages.route("/",methods=['GET'])