def get(self, id): try: query = models.Post.get(models.Post.id == id) post_schema = models.PostSchema(only=('id', 'content', 'title', 'author.name', 'author.id', 'is_url', 'created_at', 'last_modified')) post = post_schema.dump(query).data print(post) query = (models.Tag.select(models.Tag).join( models.PostTags, JOIN.RIGHT_OUTER).where(models.PostTags.post == id)) tag_schema = models.TagSchema(many=True) tags = tag_schema.dump(query).data post['tags'] = tags return jsonify({'post': post}) except models.DoesNotExist: abort(404, message="Record does not exist.")
def post(self): print('it got here 1') if (request.is_json): print('it got here 2') data = request.get_json(force=True) if is_valid(data): title = data['title'].strip() is_url = data['is_url'] name = data['author'].strip() content = data['content'].strip() tags = data['tags'] author = models.User.get(models.User.name == name) print(g.user) print(author) print(name) if g.user != author: print("user is different") abort(401) print("user is NOT different") query = models.Post.select().where( models.Post.title == title, models.Post.content == content, models.Post.author == author.id) if query.exists(): print('duplicate') abort(400, message="Duplicate entry.") else: print('log 2') post_id = models.Post.insert(title=title, is_url=is_url, author=author, content=content).execute() print('log 3') print("*post id =", post_id) insert_tags(tags, post_id) print('log 4') postid = int(post_id) query = models.Post.get(models.Post.id == postid) post_schema = models.PostSchema( only=('id', 'content', 'title', 'author.name', 'author.id', 'is_url', 'created_at', 'last_modified')) print('log 6') output = post_schema.dump(query).data print('log 7') return jsonify({'post': output}) else: abort(400, message="Missing or invalid fields.") else: abort(400, message='Not JSON data')
def get(self): try: query = models.Post.select().order_by(models.Post.id) post_schema = models.PostSchema( many=True, exclude=('author.password', 'author.email', 'author.is_moderator', 'author.member_since')) #only=('id', 'content', 'title', 'author.name', 'author.id', 'is_url', 'created_at', 'last_modified') output = post_schema.dump(query).data return jsonify({'posts': output}) except: abort(500, message="Oh, no! The Community is in turmoil!")
def get(self, name): try: query = (models.Post.select(models.Post).join( models.PostTags).join( models.Tag).where(models.Tag.name == name)) except: abort(404, message="Record does not exist.") schema = models.PostSchema(many=True, exclude=('author.password', 'author.email', 'author.is_moderator', 'author.member_since')) output = schema.dump(query).data return jsonify({'posts': output})
def get(self, name): try: user = models.User.get(models.User.name == name) except: abort(404, message="user not found") query = models.Post.select().where( models.Post.author == user.id).order_by(models.Post.id) post_schema = models.PostSchema( many=True, only=('id', 'content', 'title', 'author.name', 'author.id', 'is_url', 'created_at', 'last_modified')) output = post_schema.dump(query).data count = len(output) return jsonify({'posts': output, 'count': count})
def put(self, id): if (request.is_json): data = request.get_json(force=True) try: post = models.Post.select().where(models.Post.id == id).get() except: abort(404, message="Post doesn't exist") if g.user != post.author: # unauthorized abort(401) if ('title' in data and 'content' in data and 'is_url' in data): title = data['title'].strip() content = data['content'].strip() is_url = data['is_url'] query = models.Post.update( title=title, content=content, is_url=is_url).where(models.Post.id == id) query.execute() query_2 = models.Post.get(models.Post.id == id) post_schema = models.PostSchema( only=('id', 'content', 'title', 'author.name', 'author.id', 'is_url', 'created_at', 'last_modified')) post = post_schema.dump(query_2).data return jsonify({'post': post}) else: abort(400, message="Missing or invalid fields.") else: abort(400, message='Not JSON data')