Files
fachschaften/articles/controller.py
2017-02-15 10:32:45 +01:00

73 lines
2.5 KiB
Python

from .model import Article
from .model import ArticleSchema
#import flask
from datetime import datetime
import json
from src.sections.model import Section
from src.organizations.model import Organization
from src.database import db_session, read_json
from src.controller import BaseController
#from src.organizations.controller import controller as org_controller
class ArtController(BaseController):
__myclass__=Article
__jsonid__='article'
def pagination_params(self,v):
try:
if v.has_key("per_page"):
pp=int(v["per_page"])
else:
pp=20
except ValueError:
pp=20
try:
if v.has_key("page"):
o=(int(v["page"])-1) *pp
else:
o=0
except ValueError:
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
p1=(pp,o)
return p1
def search(self,s):
return 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()
def get_all_page(self,lim, off):
return Article.query.order_by(Article.published_date.desc()).limit(lim).offset(off).all()
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 get_organization_page(self,organization_id, lim, off):
sid=db_session.query(Section.id).filter(Section.organization_id==int(organization_id)).all()
sid=map(lambda a:a[0], sid)
articles=Article.query.filter(Article.section_id.in_(sid)).order_by(Article.published_date.desc()).limit(lim).offset(off).all()
return articles
#
# return Article.query.filter(Article.section_id==section_id).order_by(Article.published_date.desc()).limit(lim).offset(off).all()
def section_count(self,section_id):
return Article.query.filter(Article.section_id==section_id).count()
def organization_count(self,organization_id):
sid=db_session.query(Section.id).filter(Section.organization_id==int(organization_id)).all()
sid=map(lambda a:a[0], sid)
return Article.query.filter(Article.section_id.in_(sid)).count()
def count(self):
return Article.query.count()
controller=ArtController()