def add_comment(item_id): # Get the item item = session.query(Items).filter_by(id=item_id).one() # Left sidebar child_of_root, father, parent_list, parent, children, category = sidebar( item.category_id) # Right sidebar category_favorite_list, item_favorite_list = right_sidebar() form = Comment() if form.validate_on_submit(): # Create the new object, Comment new_comment = Comments(current_user.id, item.id, form.title.data, form.comment.data, datetime.now()) session.add(new_comment) session.commit() return redirect( url_for('.item_details', category_id=item.category_id, item_id=item_id)) return render_template('comment_add.html', child_of_root=child_of_root, father=father, parent_list=parent_list, parent=parent, children=children, category=category, category_favorite_list=category_favorite_list, item=item, form=form, item_favorite_list=item_favorite_list)
def post(self,request): form = Comment(request.POST) if form.is_valid(): name = request.session.get('name') user = models.UserModel.objects.get(username=name) comment=form.save(commit=False) #form will not save now,instead an object creates (comment)..this is to save the hidden comment.name = user comment.save() return HttpResponseRedirect('/userhome/') return render(request,'base.html',{'form':form,'title':'comments','head':'Add your Comments','value':'add comment'})
def show_post(post_id): form = Comment() requested_post = BlogPost.query.get(post_id) if form.validate_on_submit(): if current_user.get_id(): new_comment = CommentsTable(text=request.form['comment'], post_id=post_id, author_id=current_user.id) db.session.add(new_comment) db.session.commit() else: flash("You are not logged in!") return redirect(url_for('login')) return render_template("post.html", post=requested_post, current_user=current_user, form=form)
def type_id(request, id): blogtype = BlogType.objects.all() blog = Blog.objects.all().get(pk=id) comment = Comments.objects.all() if request.method == 'POST': form = Comment(request.POST) if form.is_valid(): instance = form.save(commit=False) instance.blog = blog instance.user = request.user instance.save() else: form = Comment() c = dict(comment=comment, blog=blog, blogtype=blogtype, form=form) c.update(csrf(request)) return render(request, 'type_id.html', c)
def edit_comment(item_id, comment_id): # Get item item = session.query(Items).filter_by(id=item_id).first() # Left sidebar child_of_root, father, parent_list, parent, children, category = sidebar( item.category_id) # Right sidebar category_favorite_list, item_favorite_list = right_sidebar() # Get comment comment = session.query(Comments).filter_by(id=comment_id).first() # Check that the user is the one that made the comment if comment.user == current_user.id: # Fill the form with the details of the comment comment_edit = Comment(obj=comment) if comment_edit.validate_on_submit(): # Upon valid submission populate the comment object with the new data comment_edit.populate_obj(comment) session.commit() flash('Comment has been updated') return redirect( url_for('.item_details', category_id=item.category_id, item_id=item_id)) else: # If not the author of the comment, redirect back to the previous page flash('Only the user that created this comment can edit it.') return redirect( url_for('.item_details', category_id=item.category_id, item_id=item_id)) return render_template('edit_comment.html', category_id=item.category_id, comment_edit=comment_edit, child_of_root=child_of_root, father=father, parent_list=parent_list, parent=parent, children=children, category=category, category_favorite_list=category_favorite_list, item_favorite_list=item_favorite_list, comment=comment, item=item)
def book(title): form = Comment() book = db.execute( f"SELECT * FROM books WHERE title = '{title}'").fetchone() # prevent searching in browser url field try: reviews = db.execute("SELECT * FROM reviews WHERE book_id = :book_id", { "book_id": book[0] }).fetchall() except BaseException: return redirect(url_for('search')) if form.validate_on_submit(): flash("Review submited", 'success') comment = form.comment.data rating = form.rate.data db.execute( """INSERT INTO reviews(user_id, book_id, rating, comment, created_on) VALUES(:user_id, :book_id, :rating, :comment, :created_on)""", { "user_id": session["user_id"], "book_id": book[0], "rating": rating, "comment": comment, "created_on": curr_time() }) db.commit() return redirect(url_for('book', title=title)) return render_template("book.html", title=book[2], book=book, form=form, can_post=can_post(db, session["user_id"], book[0]), reviews=reviews, goodreads=goodreads_info(book[1]))
def comment(request): form = Comment() return render(request,'base.html',{'form':form,'title':'comments','head':'Add your Comments','value':'add comment'})
def blog_display(blog_id, update): form = Comment() form2 = YoutubeUpload() form3 = SelectFlag() cursor = db.connection.cursor() blog_id = int(blog_id) if form.validate_on_submit(): time = datetime.datetime.now() insert_stmt = "INSERT INTO comments (Blog_ID, Auth_ID, Comment, Time_Posted, Author) VALUES (%s,%s,%s,%s,%s)" data = (blog_id, session['user_id'], form.comment.data, time, session['user']) cursor = db.connection.cursor() cursor.execute(insert_stmt, data) db.connection.commit() if form2.validate_on_submit(): link = str(form2.link.data) if link != "": split_link = link.split('watch?v=') split_left = split_link[0] split_right = split_link[1].split('&') split_right = split_right[0] final_link = split_left + 'embed/' + split_right update_stmt = "UPDATE blogs SET Youtube = %s WHERE Blog_ID = %s" cursor.execute(update_stmt, [final_link, blog_id]) db.connection.commit() if form3.validate_on_submit(): flag = form3.flag.data select_stmt = "SELECT Flag_ID FROM flags WHERE Flag=%s" cursor = db.connection.cursor() cursor.execute(select_stmt, [flag]) data = cursor.fetchall() flag_id = data[0][0] #"UPDATE blogs SET Youtube = %s WHERE Blog_ID = %s" update_flag = "UPDATE blogs SET Flag_ID = %s WHERE Blog_ID=%s" cursor = db.connection.cursor() cursor.execute(update_flag, [flag_id, blog_id]) db.connection.commit() select_stmt = "SELECT Heading, Time_Published, Theme, Flag_ID, Auth_ID, Content, Youtube FROM blogs WHERE Blog_ID=%s" cursor = db.connection.cursor() cursor.execute(select_stmt, [blog_id]) data = cursor.fetchall() heading = data[0][0] time_published = data[0][1].strftime("%d-%b-%Y (%H:%M:%S)") theme = data[0][2] flag_id = data[0][3] auth_id = data[0][4] content = data[0][5] youtube = data[0][6] if youtube == '-': print("yes") select_stmt = "SELECT Name FROM author WHERE Auth_ID=%s" auth_id = int(auth_id) cursor = db.connection.cursor() cursor.execute(select_stmt, [auth_id]) data = cursor.fetchall() auth_name = data[0][0] select_stmt = "SELECT Flag FROM flags WHERE Flag_ID=%s" flag_id = int(flag_id) cursor = db.connection.cursor() cursor.execute(select_stmt, [flag_id]) data = cursor.fetchall() flag_name = data[0][0] select_stmt = "SELECT Comment, Author, Time_Posted, Auth_ID FROM comments WHERE Blog_ID=%s" cursor = db.connection.cursor() cursor.execute(select_stmt, [blog_id]) data = cursor.fetchall() temp_comments = list(data) comments = [] for comment in temp_comments: temp_list = list(comment) temp_list[2] = temp_list[2].strftime("%d-%b-%Y (%H:%M:%S)") temp_tuple = tuple(temp_list) comments.append(temp_tuple) comments = tuple(comments) blog_id = int(blog_id) update = int(update) if (update == 1): update_views = "UPDATE interactions SET Views= Views + 1 WHERE Blog_ID=%s" cursor = db.connection.cursor() cursor.execute(update_views, [blog_id]) db.connection.commit() select_stmt = "SELECT Applauds,Views,Reports FROM interactions WHERE Blog_ID=%s" cursor = db.connection.cursor() cursor.execute(select_stmt, [blog_id]) data = cursor.fetchall() applauds = int(data[0][0]) views = int(data[0][1]) reports = int(data[0][2]) return render_template("blog.html", heading=heading, time_published=time_published, theme=theme, author=auth_name, flag=flag_name, content=content, form=form, form2=form2, form3=form3, comments=comments, blog_id=blog_id, views=views, applauds=applauds, auth_id=str(auth_id), youtube=youtube, reports=reports)