65 lines
1.8 KiB
Python
65 lines
1.8 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'],results_per_page=5)
|
|
manager.create_api(Paragraph, methods=['GET'])
|
|
manager.create_api(Comment, methods=['GET','POST'])
|
|
|
|
restapi = Api(app)
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return app.send_static_file('index.html')
|
|
|
|
class CreatePost(Resource):
|
|
def post(self):
|
|
print('hello')
|
|
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:
|
|
print(e)
|
|
return restapi.abort(400,'Invalid data')
|
|
|
|
restapi.add_resource(CreatePost,'/api/create_post')
|
|
# @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'
|
|
|
|
# ====================
|