article sections
This commit is contained in:
@@ -6,6 +6,26 @@ import json
|
|||||||
from src.database import db_session, read_json
|
from src.database import db_session, read_json
|
||||||
|
|
||||||
|
|
||||||
|
def pagination_params(v):
|
||||||
|
try:
|
||||||
|
if v.has_key("per_page"):
|
||||||
|
pp=int(v["per_page"])
|
||||||
|
else:
|
||||||
|
pp=20
|
||||||
|
if v.has_key("page"):
|
||||||
|
o=(int(v["page"])-1) *pp
|
||||||
|
else:
|
||||||
|
o=0
|
||||||
|
except ValueError:
|
||||||
|
pp=20
|
||||||
|
o=0
|
||||||
|
if not (isinstance(pp,int) and pp>0 and pp<10000):
|
||||||
|
pp=20
|
||||||
|
if not (isinstance(o,int) and o>=0 and o<100000):
|
||||||
|
o=0
|
||||||
|
return (pp, o)
|
||||||
|
|
||||||
|
|
||||||
def get_all():
|
def get_all():
|
||||||
return Article.query.order_by(Article.published_date.desc()).all()
|
return Article.query.order_by(Article.published_date.desc()).all()
|
||||||
|
|
||||||
@@ -15,5 +35,9 @@ def search(s):
|
|||||||
def get_all_page(lim, off):
|
def get_all_page(lim, off):
|
||||||
return Article.query.order_by(Article.published_date.desc()).limit(lim).offset(off).all()
|
return Article.query.order_by(Article.published_date.desc()).limit(lim).offset(off).all()
|
||||||
|
|
||||||
|
|
||||||
|
def get_section_page(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 count():
|
def count():
|
||||||
return Article.query.count()
|
return Article.query.count()
|
||||||
|
|||||||
@@ -21,22 +21,10 @@ import controller
|
|||||||
@article_pages.route(".json")
|
@article_pages.route(".json")
|
||||||
def index():
|
def index():
|
||||||
v=request.values
|
v=request.values
|
||||||
try:
|
pp,o=controller.pagination_params(v) # extract per_page and offset from params
|
||||||
if v.has_key("per_page"):
|
if v.has_key("section_id"):
|
||||||
pp=int(v["per_page"])
|
articles=controller.get_section_page(int(v["section_id"]),pp,o)
|
||||||
else:
|
else:
|
||||||
pp=20
|
|
||||||
if v.has_key("page"):
|
|
||||||
o=(int(v["page"])-1) *pp
|
|
||||||
else:
|
|
||||||
o=0
|
|
||||||
except ValueError:
|
|
||||||
pp=20
|
|
||||||
o=0
|
|
||||||
if not (isinstance(pp,int) and pp>0 and pp<10000):
|
|
||||||
pp=20
|
|
||||||
if not (isinstance(o,int) and o>=0 and o<100000):
|
|
||||||
o=0
|
|
||||||
articles=controller.get_all_page(pp,o)
|
articles=controller.get_all_page(pp,o)
|
||||||
resp = jsonify(articles=articles)
|
resp = jsonify(articles=articles)
|
||||||
resp.headers['Pagination-Limit']=pp
|
resp.headers['Pagination-Limit']=pp
|
||||||
|
|||||||
@@ -63,4 +63,4 @@ class SectionSchema(Schema):
|
|||||||
id=fields.Integer()
|
id=fields.Integer()
|
||||||
foreign_name=fields.String()
|
foreign_name=fields.String()
|
||||||
name=fields.String()
|
name=fields.String()
|
||||||
articles=fields.Nested(ArticleCompSchema,many=True)
|
# articles=fields.Nested(ArticleCompSchema,many=True)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from .model import SectionSchema
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
from src import clogger
|
from src import clogger
|
||||||
|
import src.articles.controller as article_controller
|
||||||
from src.database import db_session, read_json
|
from src.database import db_session, read_json
|
||||||
import flask
|
import flask
|
||||||
|
|
||||||
@@ -38,3 +38,18 @@ def get(id):
|
|||||||
return jsonify(section=section)
|
return jsonify(section=section)
|
||||||
|
|
||||||
|
|
||||||
|
@section_pages.route("/<int:id>/articles",methods=['GET'])
|
||||||
|
@section_pages.route("/<int:id>/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
|
||||||
|
articles=controller.get_section_page(id,pp,o)
|
||||||
|
|
||||||
|
resp = jsonify(articles=articles)
|
||||||
|
resp.headers['Pagination-Limit']=pp
|
||||||
|
resp.headers['Pagination-Offset']=o
|
||||||
|
resp.headers['Pagination-TotalCount']=controller.count()
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user