typeset-blog/app/views.py

72 lines
2.1 KiB
Python

# -*- encoding: utf-8 -*-
"""
Python Aplication Template
Licence: GPLv3
"""
from flask import request
from flask_restless import APIManager
from flask_restplus import Resource,Api
import json
from app import app,db
from models import Post,Paragraph,Comment
# Create the Flask-Restless API manager.
manager = APIManager(app, flask_sqlalchemy_db=db)
# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(Post, methods=['GET'],exclude_columns=['paragraphs'],results_per_page=5)
manager.create_api(Post, methods=['GET'],results_per_page=None,collection_name='post_para')
manager.create_api(Paragraph, methods=['GET'],results_per_page=None)
manager.create_api(Comment, methods=['GET','POST'])
restapi = Api(app)
@app.route('/')
def index():
return app.send_static_file('index.html')
@restapi.route('/api/post_misc')
class PostMisc(Resource):
def post(self):
try:
post_data = json.loads(request.data)
title = post_data['title']
content = post_data['content']
p_e = Post(title)
db.session.add(p_e)
pgs = content.split('\n\n')
for pg in pgs:
pg_e = Paragraph(body=pg,post_id=p_e.id)
db.session.add(pg_e)
db.session.commit()
return {'post_id':p_e.id}
except Exception as e:
return restapi.abort(400,'Invalid data')
# @app.route('/api/post_misc')
# def get_post():
# post_id = request.args.get('post_id')
# print(post_id)
# p_e = Post.query.get(post_id)
# return {'paragraphs':p_e.paragraphs}
# restapi.add_resource(PostMisc,'/api/post_misc')
# @app.route('/api/create_post',methods=['POST'])
# def create_post():
# title = request.form.get('title',None);
# content = request.form.get('content',None)
# print(title,content)
# return 'ok'
#
# @app.route('/api/addpost',methods=['POST'])
# def addpost():
# return 'Post Added'
#
# @app.route('/api/listposts')
# def listposts():
# return 'Post Added'
# ====================