typeset-blog/app/views.py

72 lines
2.1 KiB
Python
Raw Permalink Normal View History

2017-08-09 06:43:31 +00:00
# -*- encoding: utf-8 -*-
"""
Python Aplication Template
Licence: GPLv3
"""
from flask import request
from flask_restless import APIManager
2017-08-09 20:16:22 +00:00
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'])
2017-08-09 10:41:26 +00:00
restapi = Api(app)
2017-08-09 06:43:31 +00:00
@app.route('/')
def index():
return app.send_static_file('index.html')
2017-08-09 10:41:26 +00:00
@restapi.route('/api/post_misc')
class PostMisc(Resource):
2017-08-09 10:41:26 +00:00
def post(self):
try:
2017-08-09 20:16:22 +00:00
post_data = json.loads(request.data)
2017-08-09 10:41:26 +00:00
title = post_data['title']
content = post_data['content']
2017-08-09 20:16:22 +00:00
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}
2017-08-09 10:41:26 +00:00
except Exception as e:
2017-08-09 20:16:22 +00:00
return restapi.abort(400,'Invalid data')
2017-08-09 10:41:26 +00:00
# @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')
2017-08-09 10:41:26 +00:00
# @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'
2017-08-09 06:43:31 +00:00
# ====================