from flask import Flask, session
from flask import render_template
from flask_script import Manager
from flask import url_for
from flask import request, jsonify, json
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
import os
from flask_marshmallow import Marshmallow
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
app.config["SECRET_KEY"] = "a string"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + \
os.path.join(basedir, "data.sqlite")
-sharp
app.config["SQLALCHEMY_COMMIT_TEARDOWN"] = True
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
manager = Manager(app)
db = SQLAlchemy(app)
ma = Marshmallow(app)
class Article(db.Model):
"""docstring for Article"""
__tablename__ = "articles"
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(64))
content = db.Column(db.Text)
class ArticleSchema(ma.ModelSchema):
class Meta:
medel = Article
@app.route("/")
def index():
articles = Article.query.all()
articles_schema = ArticleSchema()
output = articles_schema.dump(articles).data
return jsonify({"articles": output})
@app.route("/create", methods=["POST"])
def create():
title = request.form["title"]
content = request.form["content"]
article = Article(title=title, content=content)
db.session.add(article)
session["known"] = False
return content
if __name__ == "__main__":
manager.run()