def _generate_comments( _count: int, reviewed: bool = True, from_admin: bool = False, is_replied: bool = False ): """ 生成评论数据,内部调用 :param _count: 生成的评论数量 :param reviewed: default=True,默认是已审核评论 :param from_admin: default=False,默认不是管理员评论 :param is_replied: default=False,默认不是回复评论 :return: None """ comments_count = Comment.query.count() posts_count = Post.query.count() for i in range(_count): with db.auto_commit(): comment = Comment() if not from_admin: comment.author = cls.FAKER.name() comment.email = cls.FAKER.email() comment.site = cls.FAKER.url() else: comment.author = Admin.query.get(1).nickname comment.email = "*****@*****.**" comment.site = "localhost:5000" comment.content = cls.FAKER.text(random.randint(40, 200)) comment.from_admin = from_admin comment.reviewed = reviewed if is_replied: comment.replied = Comment.query.get(random.randint(1, comments_count)) comment.post = Post.query.get(random.randint(1, posts_count)) db.session.add(comment)
def addComment(): article_id = request.form['article'] name = request.form['name'] email = request.form['email'] text = request.form['text'] comment = Comment() comment.id_article = article_id comment.email = email comment.name = name comment.text = text db.session.add(comment) db.session.commit() return redirect('/article/{}'.format(article_id))
def detail_view(request, year, month, date, post): post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month) comments = post.comments.filter(active=True) csubmit = False if request.method == 'POST': list = Comment() list.name = request.user t = request.user list.email = t.email list.body = request.POST.get("comment") list.post = post list.save() csubmit = True else: pass return render(request, 'app\detail.html', { 'list': post, 'csubmit': csubmit, 'comments': comments })
def submit_comment(): form = AuthenticatedCommentForm( ) if current_user.is_authenticated else CommentForm() if form.validate_on_submit(): current_app.logger.debug(request.form) captcha_data = { 'secret': current_app.config['RECAPTCHA_SECRET'], 'response': request.form.get('token'), } resp = requests.post('https://www.google.com/recaptcha/api/siteverify', captcha_data) response_data = resp.json() current_app.logger.debug(response_data) if current_app.config.get('DEVELOPMENT'): response_data['success'] = True #REMOVE if response_data.get('success'): form.reply_id.data = form.reply_id.data if form.reply_id.data else None form.page_id.data = form.page_id.data if form.page_id.data else None form.product_id.data = form.product_id.data if form.product_id.data else None comment = Comment() form.populate_obj(comment) current_app.logger.debug(f'REPLY ID: {comment.reply_id}') comment.ip = request.remote_addr if current_user.is_authenticated: comment.user_id = current_user.id comment.name = current_user.display_name() comment.email = current_user.email db.session.add(comment) db.session.commit() log_new(comment, 'added a comment') flash('Comment added.', 'success') comment.notify() # notify admin if comment.email: subscriber = Subscriber.query.filter_by( email=comment.email).first() if not subscriber: subscriber = Subscriber(first_name=comment.name, email=comment.email, subscription=',Comment Replies,') if form.subscribe.data: subscriber.subscription += ','.join([ i[0] for i in current_app.config['SUBSCRIPTION_GROUPS'] ]) + ',' db.session.add(subscriber) db.session.commit() if form.subscribe.data: log_new(subscriber, 'subscribed') flash('Subscribed!', 'success') subscriber.welcome() else: log_orig = log_change(subscriber) subscriber.subscription = ',Comment Replies,' if form.subscribe.data: subscriber.subscription += ','.join([ i[0] for i in current_app.config['SUBSCRIPTION_GROUPS'] ]) + ',' log_change(log_orig, subscriber, 'updated subscriptions') db.session.commit() flash('You are already subscribed!', 'info') else: if form.subscribe.data: flash( 'You must provide an email address to subscribe. Please <a href="/subscribe">subscribe here</a> instead.', 'info') comment.notify_reply() # Notify commenter replied to else: flash( 'Unable to save comment. Recaptcha flagged you as a bot. If you are not a bot, please try submitting your comment again.', 'danger') return redirect(request.referrer)