forked from SazhnevWIS/noodles_blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
article_controller.py
59 lines (45 loc) · 1.64 KB
/
article_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
from json import dumps
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm.session import Session
from webob.exc import HTTPMethodNotAllowed
from core import serialize
from models import Article, engine
from noodles.http import Response
session = Session(bind=engine)
def add_article(request):
if not request.method != 'POST':
raise HTTPMethodNotAllowed
try:
article = Article(user_id=request.params['user_id'],
title=request.params['title'],
text=request.params['text'])
session.add(article)
session.commit()
response = dumps(serialize(article))
except KeyError:
response = 'query parameters was not provided'
return Response(response)
def read(request, id):
article = session.query(Article).filter(Article.id == id.path).one()
json = dumps(serialize(article))
return Response(json)
def list_articles(request):
try:
articles = session.query(Article).filter(
Article.user_id == request.params['user_id']).all()
except KeyError:
articles = session.query(Article).all()
json = dumps([serialize(article) for article in articles])
return Response(json)
def delete_article(request, id):
if not request.method != 'DELETE':
raise HTTPMethodNotAllowed
try:
article = session.query(Article).filter(Article.id == id.path).one()
session.delete(article)
session.commit()
message = 'article {} deleted'.format(article.title)
except NoResultFound:
message = 'article does not exist'
return Response(message)