implemented list view for posts

This commit is contained in:
Malar Kannan
2017-08-10 01:46:22 +05:30
parent 617f6b890e
commit d5d980a25d
8 changed files with 105 additions and 30 deletions

View File

@@ -24,7 +24,8 @@ class ProductionConfig(Config):
DATABASE_URI = 'mysql://user@localhost/foo'
class DevelopmentConfig(Config):
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
# SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/typeset.db'
DEBUG = True
class TestingConfig(Config):

View File

@@ -5,18 +5,20 @@ Licence: GPLv3
"""
from app import db
from uuid import uuid4
from datetime import datetime
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
id = db.Column(db.String(32), primary_key=True)
title = db.Column(db.String(80))
pub_date = db.Column(db.DateTime)
def __init__(self, title,paragraphs, pub_date=None):
def __init__(self, title, pub_date=None):
self.title = title
if pub_date is None:
pub_date = datetime.utcnow()
self.pub_date = pub_date
self.paragraphs = paragraphs
self.id = uuid4().hex
def __repr__(self):
return '<Post %r>' % self.title
@@ -24,8 +26,8 @@ class Post(db.Model):
class Paragraph(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
db.relationship('Post',backref=db.backref('paragraphs', lazy='dynamic'))
post_id = db.Column(db.String(32), db.ForeignKey('post.id'))
post = db.relationship('Post',backref=db.backref('paragraphs', lazy='dynamic'))
def __init__(self, body,post_id):
self.body = body
@@ -39,8 +41,8 @@ class Comment(db.Model):
name = db.Column(db.String(50))
message = db.Column(db.String(140))
paragraph_id = db.Column(db.Integer, db.ForeignKey('paragraph.id'))
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
db.relationship('Paragraph',backref=db.backref('comments', lazy='dynamic'))
post_id = db.Column(db.String(32), db.ForeignKey('post.id'))
paragraph = db.relationship('Paragraph',backref=db.backref('comments', lazy='dynamic'))
def __init__(self, name,message):
self.name = name

View File

@@ -6,7 +6,8 @@ Licence: GPLv3
from flask import request
from flask_restless import APIManager
from flask_restful import Resource,Api
from flask_restplus import Resource,Api
import json
from app import app,db
from models import Post,Paragraph,Comment
@@ -14,7 +15,7 @@ from models import Post,Paragraph,Comment
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'])
manager.create_api(Post, methods=['GET'],results_per_page=5)
manager.create_api(Paragraph, methods=['GET'])
manager.create_api(Comment, methods=['GET','POST'])
@@ -27,18 +28,22 @@ def index():
class CreatePost(Resource):
def post(self):
print('hello')
try:
post_data = json.parse(request.data)
post_data = json.loads(request.data)
title = post_data['title']
content = post_data['content']
p = Post(title)
pgs = content.split('\n')
db.session.add(p)
db.session.add(p)
return {'post_id':'title'}
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 {'error':'invalid_post'}
print(e)
return restapi.abort(400,'Invalid data')
restapi.add_resource(CreatePost,'/api/create_post')
# @app.route('/api/create_post',methods=['POST'])