git ssb

0+

Daan Patchwork / py-http-ssb



Tree: 2eae3f13696f12df75b1b2e93e8a83521ba60ca4

Files: 2eae3f13696f12df75b1b2e93e8a83521ba60ca4 / resources.py

2778 bytesRaw
1#!/usr/bin/env python3
2
3from db import session
4from datetime import datetime
5from flask.ext.restful import reqparse
6from flask.ext.restful import abort
7from flask.ext.restful import Resource
8from flask.ext.restful import fields
9from flask.ext.restful import marshal_with
10
11from models import Message
12
13message_fields = {
14 "id": fields.Integer,
15 "key": fields.String,
16 "feed_id": fields.String,
17 "timestamp": fields.DateTime,
18 "value": fields.String,
19}
20
21feed_fields = {
22 "id": fields.Integer,
23 "feed_id": fields.String,
24 "latest_sequence": fields.Integer,
25}
26
27parser = reqparse.RequestParser()
28parser.add_argument("key")
29parser.add_argument("author")
30parser.add_argument("timestamp")
31parser.add_argument("value")
32
33
34class FeedResource(Resource):
35 pass
36
37
38class FeedListResource(Resource):
39 @marshal_with(message_fields)
40 def get(self):
41 messages = session.query(Feed).all()
42 return messages
43
44
45class MessageResource(Resource):
46 @marshal_with(message_fields)
47 def get(self, key):
48 message = session.query(Message).filter(Message.key == key).first()
49 if not message:
50 abort(404, message=f"Message {key} doesn't exist")
51 return message
52
53 @marshal_with(message_fields)
54 def post(self):
55 parsed_args = parser.parse_args()
56 print(parsed_args)
57 abort(404, message="not implemented yet")
58 message = Feed(
59 title=parsed_args["title"],
60 description=parsed_args["description"],
61 create_at=parsed_args["create_at"],
62 create_by=parsed_args["create_by"],
63 priority=parsed_args["priority"],
64 )
65 session.add(message)
66 session.commit()
67 return message, 201
68
69 # FIXME: we do want to delete messages, but not as a public api... how to?
70 # def delete(self, key):
71 # message = session.query(Message).filter(Message.key == key).first()
72 # if not message:
73 # abort(404, message="Message {} doesn't exist".format(key))
74 # session.delete(message)
75 # session.commit()
76 # return {}, 204
77 #
78 # FIXME: put seems wrong... this updates messages, which we never want...
79 # @marshal_with(message_fields)
80 # def put(self, key):
81 # parsed_args = parser.parse_args()
82 # message = session.query(Message).filter(Message.key == key).first()
83 # message.title = parsed_args['title']
84 # message.description = parsed_args['description']
85 # message.create_at = parsed_args['create_at']
86 # message.create_by = parsed_args['create_by']
87 # message.priority = parsed_args['priority']
88 # session.add(message)
89 # session.commit()
90 # return message, 201
91

Built with git-ssb-web