79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
from flask import Blueprint, jsonify, render_template, abort, redirect, url_for, request
|
|
article_pages = Blueprint('articles', __name__)
|
|
from .model import Article
|
|
from .model import ArticleSchema
|
|
#import flask
|
|
from datetime import datetime
|
|
import json
|
|
|
|
from src import clogger
|
|
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
|
|
@article_pages.route("/")
|
|
@article_pages.route("")
|
|
@article_pages.route(".json")
|
|
def index():
|
|
v=request.values
|
|
pp,o=controller.pagination_params(v) # extract per_page and offset from params
|
|
if v.has_key("section_id"):
|
|
articles=controller.get_section_page(int(v["section_id"]),pp,o)
|
|
c=controller.section_count(int(v["section_id"]))
|
|
elif v.has_key("organization_id"):
|
|
articles=controller.get_organization_page(int(v["organization_id"]), pp,o)
|
|
c=controller.organization_count(int(v["organization_id"]))
|
|
else:
|
|
articles=controller.get_all_page(pp,o)
|
|
c=controller.count()
|
|
resp = jsonify(articles=articles)
|
|
resp.headers['Pagination-Limit']=pp
|
|
resp.headers['Pagination-Offset']=o
|
|
resp.headers['Pagination-TotalCount']=c
|
|
return resp
|
|
|
|
@article_pages.route("/<int:id>",methods=['PUT'])
|
|
@article_pages.route("/<int:id>.json",methods=['PUT'])
|
|
def update(id):
|
|
article=db_session.query(Article).get(id)
|
|
clogger.info(request.data)
|
|
a=request.get_json()
|
|
article.text=a["text"]
|
|
db_session.commit()
|
|
return jsonify(article=article)
|
|
|
|
|
|
@article_pages.route("/<int:id>",methods=['GET'])
|
|
@article_pages.route("/<int:id>.json",methods=['GET'])
|
|
def get(id):
|
|
article=db_session.query(Article).get(id)
|
|
# clogger.info(article)
|
|
# article=ArticleSchema().dump(article)[0]
|
|
return jsonify(article=article)
|
|
|
|
@article_pages.route("/<int:id>",methods=['DELETE'])
|
|
@article_pages.route("/<int:id>.json",methods=['DELETE'])
|
|
def delete(id):
|
|
article=db_session.query(Article).get(id)
|
|
# clogger.info(id)
|
|
if article != None:
|
|
db_session.delete(article)
|
|
db_session.commit()
|
|
return jsonify(article={})
|
|
|
|
|
|
@article_pages.route("/",methods=['POST'])
|
|
@article_pages.route("",methods=['POST'])
|
|
@article_pages.route(".json",methods=['POST'])
|
|
def create():
|
|
article=Article()
|
|
a=read_json(request)
|
|
article.text=a["article"]["text"]
|
|
db_session.add(article)
|
|
db_session.commit()
|
|
return jsonify(article=article)
|