예제 #1
0
 def post(self):
     """
     Adds a new post
     """
     data = api.payload
     tags = data.get('tags')
     if not tags:
         abort(400, {"Tags": ["Les tags sont necessaire!"]})
     form = PostForm.from_json(data)
     if form.validate():
         title = data.get('title')
         content = data.get('content')
         post = Post(id=uuid.uuid4().hex,title=title.lower(), content=content, user=g.user,\
                     posted_at=datetime.utcnow(), refugee_post=g.user.refugee)
         post.add_tags(tags)
         db.session.add(post)
         db.session.commit()
         return {'element': post.to_json()}, 201
     return {"message": form.errors}, 400
예제 #2
0
 def put(self, post_id):
     """
     Updates a post
     """
     post = Post.query.get(post_id)
     if not post:
         abort(404)
     data = api.payload
     tags = data.get('tags',post.tags)
     if not tags:
         abort(400)
     form = PostForm.from_json(data)
     if form.validate():
         post.title = data.get('title')
         post.content = data.get('content')
         post.add_tags(tags)
         post.posted_at = datetime.utcnow()
         db.session.add(post)
         db.session.commit()
         return {'element': post.to_json()}, 201
     return {"form_errors": form.errors}, 400