posts.py 391 Bytes
Newer Older
Michal Pavlík's avatar
Michal Pavlík committed
1
from config import db
Michal Pavlík's avatar
Michal Pavlík committed
2
from models import Post
Michal Pavlík's avatar
Michal Pavlík committed
3 4
from errors import PostNotFound

Michal Pavlík's avatar
Michal Pavlík committed
5 6

def get(id):
Michal Pavlík's avatar
Michal Pavlík committed
7
    post = Post.query.get(id)
Michal Pavlík's avatar
Michal Pavlík committed
8

Michal Pavlík's avatar
Michal Pavlík committed
9 10 11 12
    if post:
        return post.json()
    else:
        raise PostNotFound(id)
Michal Pavlík's avatar
Michal Pavlík committed
13 14


Michal Pavlík's avatar
Michal Pavlík committed
15 16
def delete(id, user):
    post = Post.query.get(id)
Michal Pavlík's avatar
Michal Pavlík committed
17

Michal Pavlík's avatar
Michal Pavlík committed
18 19
    if not Post or post.author_id is not user:
        raise PostNotFound(id)
Michal Pavlík's avatar
Michal Pavlík committed
20

Michal Pavlík's avatar
Michal Pavlík committed
21 22
    db.session.delete(post)
    db.session.commit()