Commit 8d59be9d authored by Michal Pavlík's avatar Michal Pavlík

last state

parent b047a0d7
__pycache__
venv
.idea/*
data.db
import connexion
cnx = connexion.App(__name__, specification_dir='./')
cnx.add_api('swagger.yml')
from config import cnx, db
from flask_sqlalchemy import SQLAlchemy
import models
with cnx.app.app_context():
db.create_all()
if __name__ == '__main__':
cnx.add_api('swagger.yml')
cnx.run(debug=True, port=8080)
from models import Post
from flask import request, jsonify
from config import db
def post():
pass
data = request.json
new_post = Post(message=data['message'], author='Anonymous')
db.session.add(new_post)
db.session.commit()
return new_post.json(), 201
def parse_page(data):
return {
'content': [p.json() for p in data.items],
'page': data.page,
'size': data.per_page,
'totalPages': data.pages
}
def all(page=1, size=10):
data = Post.query.order_by(Post.created.desc()).paginate(page, size, False)
return parse_page(data)
def all():
pass
import connexion
from flask_sqlalchemy import SQLAlchemy
from errors import OurException
cnx = connexion.App(__name__, specification_dir='./')
db = SQLAlchemy()
db.init_app(cnx.app)
cnx.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./data.db'
cnx.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
cnx.app.config['SQLALCHEMY_ECHO'] = True
@cnx.app.errorhandler(OurException)
def exception_handler(e):
return {
'msg': e.get_message(),
'code': e.get_code()
}, e.get_http_code()
class OurException(Exception):
def get_message(self):
return 'no message'
def get_code(self):
return -1
def get_http_code(self):
return 500
class PostNotFound(OurException):
def __init__(self, post_id):
self.post_id = post_id
def get_message(self):
return f'Post with id {self.post_id} not found.'
def get_code(self):
return 100
def get_http_code(self):
return 404
from config import db
from datetime import datetime
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
message = db.Column(db.Text, nullable=False)
author = db.Column(db.String, nullable=False)
created = db.Column(db.DateTime, default=datetime.utcnow)
def json(self):
return {
'id': self.id,
'message': self.message,
'author': self.author,
'created': self.created
}
from models import Post
from config import db
from errors import PostNotFound
def get(id):
pass
data = Post.query.get(id)
if data:
return data.json()
else:
raise PostNotFound(id)
def delete(id):
pass
data = Post.query.get(id)
if not data:
raise PostNotFound(id)
db.session.delete(data)
db.session.commit()
......@@ -9,11 +9,11 @@ servers:
- url: 'http://localhost:8080/'
- url: 'https://localhost:8080/'
security:
- BasicAuth: []
- OAuth2:
- read
- write
#security:
# - BasicAuth: []
# - OAuth2:
# - read
# - write
paths:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment