def comment(request, id): item = Item.objects.get(pk=id) form_view = commentForm error = 0; if request.POST: form = commentForm(request.POST) if form.is_valid(): comment = Comment(name=form.cleaned_data['username']) comment.text = form.cleaned_data['text'] comment.date = datetime.today() comment.save() item.comments.add(comment) item.save() error = 0 return HttpResponseRedirect('/comments/' + id + '/') else: error = 1 form_view = commentForm( initial={'username': form.cleaned_data['username'], 'text': form.cleaned_data['text']}) return render( request, 'comments.html', { 'title': item.title, 'item': item, 'form': form_view, 'comments': item.comments.all(), 'error': error, } )
def post(post_id): '''view func for post page''' # From obj: 'Comment' form = CommentForm() # form.validate_on_submit() will be true and return the data obj # to form instance from user enter, when HTTP request is POST if form.validate_on_submit(): new_comment = Comment(id=str(uuid4()), name=form.name.data) new_comment.text = form.text.data new_comment.date = datetime.datetime.now() new_comment.post_id = post_id db.session.add(new_comment) db.session.commit() post = db.session.query(Post).get_or_404(post_id) tags = post.tags comments = post.comments.order_by(Comment.date.desc()).all() recent, top_tags = sidebar_data() return render_template('post.html', post=post, tags=tags, comments=comments, recent=recent, top_tags=top_tags, form=form)
def post(post_id): """View function for post page""" # Form object: `Comment` form = CommentForm() # form.validate_on_submit() will be true and return the # data object to form instance from user enter, # when the HTTP request is POST if form.validate_on_submit(): new_comment = Comment(id=str(uuid4()), name=form.name.data) new_comment.text = form.text.data new_comment.date = datetime.now() new_comment.post_id = post_id db.session.add(new_comment) db.session.commit() post = Post.query.get_or_404(post_id) tags = post.tags comments = post.comments.order_by(Comment.date.desc()).all() recent, top_tags = sidebar_data() return render_template('post.html', post=post, tags=tags, comments=comments, form=form, recent=recent, top_tags=top_tags)
def post(post_id): form = CommentForm() if form.validate_on_submit(): new_comment = Comment() new_comment.name = form.name.data new_comment.text = form.text.data new_comment.post_id = post_id new_comment.date = datetime.datetime.now() db.session.add(new_comment) db.session.commit() post = Post.query.get_or_404(post_id) tags = post.tags comments = post.comments.order_by(Comment.date.desc()).all() recent, top_tags = sidebar_data() return render_template('post.html', post=post, tags=tags, comments=comments, recent=recent, top_tags=top_tags, form=form)
def create_photo_collection_comment(): collection_id = int(request.form.get("comment_owner_id")) photo_collection = PhotoCollection.query.get(collection_id) collection_comment = PhotoCollectionComment() comment = Comment() comment.user_id = current_user.id comment.content = request.form.get("content") comment.date = datetime.now() collection_comment.comment = comment collection_comment.photo_collection = photo_collection db.session.add(comment) db.session.add(collection_comment) db.session.commit() return redirect(url_for("blog", photographer_id=photo_collection.photographer_id))
def post(self, param=None): data = request.get_json(force=True) comment = Comment() user = User.query.get(current_user_id()) comment.user = user comment.content = data.get('content', None) comment.date = datetime.datetime.now() post = Post.query.get(param) post.comments.append(comment) db.session.commit() return jsonify({ 'message': 'Comentario creado exitosamente', 'redirect': f'posts/view/{post.id}' })
def test_comment_cascade_reaction(self): post = Post(title='test2') post.timestamp = datetime.now() post.body = 'test message' post.user_id = 1 db.session.add(post) db.session.commit() comment = Comment(user_id=1, post_id=1) comment.date = datetime.now() comment.text = 'test' db.session.add(comment) db.session.commit() # delete user first user = User.query.filter_by(id=1).first() db.session.delete(user) db.session.commit() # post is also deleted post = Post.query.filter_by(title='test2').first() self.assertIsNone(post) # comment is also deleted comment = Comment.query.filter_by(id=1).first() self.assertIsNone(comment)
def add_comment(post_id): """View function for post page""" # Form object: `Comment` form = CommentForm() # form.validate_on_submit() will be true and return the # data object to form instance from user enter, # when the HTTP request is POST if form.validate_on_submit(): new_comment = Comment(form.name.data) new_comment.text = form.text.data new_comment.date = datetime.datetime.now() new_comment.post_id = post_id db.session.add(new_comment) db.session.commit() return redirect(url_for('post.list')) post = Post.query.get_or_404(post_id) # tags = post.tags # comments = post.comments.order_by(Comment.date.desc()).all() return render_template('post/add_comment.html', post=post, form=form)