boards.py 613 Bytes
Newer Older
Michal Pavlík's avatar
Michal Pavlík committed
1
from models import Post
Michal Pavlík's avatar
Michal Pavlík committed
2
from flask import request, escape
Michal Pavlík's avatar
Michal Pavlík committed
3
from config import db
Michal Pavlík's avatar
Michal Pavlík committed
4 5


Michal Pavlík's avatar
Michal Pavlík committed
6 7 8 9 10 11 12
def parse_page(data):
    return {
        'content': [e.json() for e in data.items],
        'page': data.page,
        'size': data.per_page,
        'totalPages': data.pages
    }
Michal Pavlík's avatar
Michal Pavlík committed
13 14


Michal Pavlík's avatar
Michal Pavlík committed
15 16
def post(user, token_info):
    data = request.json
Michal Pavlík's avatar
Michal Pavlík committed
17

Michal Pavlík's avatar
Michal Pavlík committed
18 19 20
    new_post = Post(message=escape(data['message']), author_id=user)
    db.session.add(new_post)
    db.session.commit()
Michal Pavlík's avatar
Michal Pavlík committed
21

Michal Pavlík's avatar
Michal Pavlík committed
22
    return new_post.json(), 201
Michal Pavlík's avatar
Michal Pavlík committed
23

Michal Pavlík's avatar
Michal Pavlík committed
24

Michal Pavlík's avatar
Michal Pavlík committed
25
def all(page=1, size=10):
Michal Pavlík's avatar
Michal Pavlík committed
26

Michal Pavlík's avatar
Michal Pavlík committed
27 28
    data = Post.query.order_by(Post.created.desc()).paginate(page, size, False)
    return parse_page(data)
Michal Pavlík's avatar
Michal Pavlík committed
29