online fixes for ruby

This commit is contained in:
uwsgi
2017-02-11 12:48:30 +01:00
parent 621e1ca1ad
commit 49ac42b9a5
8 changed files with 65 additions and 45 deletions

View File

@@ -98,7 +98,7 @@ def home():
app.register_blueprint(article_pages, url_prefix='/articles') app.register_blueprint(article_pages, url_prefix='/articles')
app.register_blueprint(section_pages, url_prefix='/sections') app.register_blueprint(section_pages, url_prefix='/sections')
app.register_blueprint(compiler_pages, url_prefix='/compiler') 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(organization_pages, url_prefix='/organizations')
app.register_blueprint(organizationtype_pages, url_prefix='/organizationtypes') app.register_blueprint(organizationtype_pages, url_prefix='/organizationtypes')

View File

@@ -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() 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() 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() return Article.query.filter(Article.section_id==section_id).count()
def count(): def count(self):
return Article.query.count() return Article.query.count()

View File

@@ -41,6 +41,32 @@ def calc_fingerprint_h(a):
h.update(unicode(pp)) h.update(unicode(pp))
return h.hexdigest() 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): class Article(Base):
__tablename__ = 'articles' __tablename__ = 'articles'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
@@ -60,7 +86,10 @@ class Article(Base):
section_id=Column(Integer, ForeignKey('sections.id')) section_id=Column(Integer, ForeignKey('sections.id'))
sourcetype = Column(String(250)) sourcetype = Column(String(250))
image=Column(String(250)) image=Column(String(250))
__schema__=FullArticleSchema
__jsonid__='article'
__whiteattrs__= []
__jsonattrs__=None
def __init__(self, url=None,title=None, published_date=None): def __init__(self, url=None,title=None, published_date=None):
self.url=url self.url=url
self.title=title self.title=title
@@ -69,16 +98,16 @@ class Article(Base):
def __json__(self): def __json__(self):
return ArticleSchema().dump(self)[0] return ArticleSchema().dump(self)[0]
def dict(self): # 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} # 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 # @classmethod
def deserialize(cls,data): # def deserialize(cls,data):
a=Article() # a=Article()
for c in Article.__table__.columns: # for c in Article.__table__.columns:
if data.has_key(c.key): # if data.has_key(c.key):
setattr(a, c.key,data[c.key]) # setattr(a, c.key,data[c.key])
return a # return a
# @classmethod # @classmethod
# def sections(self): # def sections(self):
@@ -131,26 +160,6 @@ class Article(Base):
#json.JSONEncoder.default = lambda self,obj: ((ArticleSchema().dump(obj)[0]) if isinstance(obj, Article) else None) #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)

View File

@@ -34,7 +34,7 @@ def fbfeedelement(h):
return art return art
def fbfeed(url, raw): def fbfeed(url, raw, params={}):
js = json.loads(raw) js = json.loads(raw)
arts=[] arts=[]
u=urlparse.urlparse(url) u=urlparse.urlparse(url)

View File

@@ -11,9 +11,10 @@ class BaseController():
def create(self,request): def create(self,request):
d=read_json(request) d=read_json(request)
clogger.info(d) clogger.info(d)
clogger.info(d[self.__jsonid__]) if d.has_key(self.__jsonid__):
d= d[self.__jsonid__]
o=self.__myclass__() o=self.__myclass__()
success, errors=o.update(d[self.__jsonid__]) success, errors=o.update(d)
if success: if success:
db_session.add(o) db_session.add(o)
try: try:

View File

@@ -70,4 +70,8 @@ def delete(id):
@organization_pages.route(".json",methods=['POST']) @organization_pages.route(".json",methods=['POST'])
def create(): def create():
organization,errors=controller.create(request) 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

9
sections/controller.py Normal file
View File

@@ -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()

View File

@@ -7,6 +7,7 @@ from datetime import datetime
import json import json
from src import clogger from src import clogger
from src.articles import controller as article_controller from src.articles import controller as article_controller
from src.sections.controller import controller
from src.database import db_session, read_json from src.database import db_session, read_json
import flask import flask
@@ -21,12 +22,8 @@ def index():
@section_pages.route("/<int:id>",methods=['PUT']) @section_pages.route("/<int:id>",methods=['PUT'])
@section_pages.route("/<int:id>.json",methods=['PUT']) @section_pages.route("/<int:id>.json",methods=['PUT'])
def update(id): def update(id):
section=Section.query.get(id) section,errors=controller.update(id,request)
clogger.info(request.data) return jsonify(section=section, errors=errors)
a=read_json(request)
section.update(a["section"])
db_session.commit()
return jsonify(section=section)
@section_pages.route("/<int:id>",methods=['GET']) @section_pages.route("/<int:id>",methods=['GET'])