Example #1
0
def post(post_id):
    """Post detail page."""

    form = CommentForm()

    if form.validate_on_submit():
        # If valid form is submitted, add comment and redirect back to page
        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()
        return redirect(request.url)

    else:
        # No form submitted or form is invalid, show post (with errors if appropriate)
        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,
        )
Example #2
0
 def mutate(root, info, input=None):
     Posts = []
     for post_input in input.posts:
         post = Post.objects.get(pk=post_input['id'])
         if post is None:
             return CreateComment(comment=None)
         Posts.append(post)
     comment_instance = Comment(text=input.text, author=input.author)
     comment_instance.save()
     comment_instance.posts.set(Posts)
     return CreateComment(comment=comment_instance)
Example #3
0
def comment(request):
    now = datetime.datetime.now()
    if request.method == 'POST': # If the form has been submitted...
        form = CommentForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            try:
                form_name = User.objects.get(username = request.user)
            except User.DoesNotExist:
                return HttpResponseRedirect('/login/')
            form_comment = form.cleaned_data['comment']
            form_hid_id = form.cleaned_data['hidden_id']
            form_hid_name = form.cleaned_data['hidden_name']
            c = Comment(post_fkey_id = form_hid_id, uname = form_name, comment = form_comment, com_date = now)
            c.save()
            return HttpResponseRedirect('/posts/%s/%d/' %(form_hid_name, form_hid_id)) # Redirect after POST
Example #4
0
def blogContent(blog_name):
    blog = Blog.query.filter_by(title=blog_name).first()
    blog.click = blog.click + 1

    db.session.add(blog)
    db.session.commit()

    original = Blog.query.filter_by().count()
    fans = User.query.filter_by().count()
    comments_total = Comment.query.filter_by().count()
    java_blog = Blog.query.filter_by(tag='Java').count()
    c_blog = Blog.query.filter_by(tag='C').count()
    flask_blog = Blog.query.filter_by(tag='Flask').count()
    opengl_blog = Blog.query.filter_by(tag='OpenGL').count()
    android_blog = Blog.query.filter_by(tag='Android').count()
    python_blog = Blog.query.filter_by(tag='Python').count()

    form = CommentForm()
    blog_path = 'blogs/' + blog_name + '.html'

    comments = []
    comments_in_db = Comment.query.filter(Comment.blog_id == blog.id).all()
    for comment in comments_in_db:
        icon_index = str(random.randint(1, 6))
        username = User.query.filter(User.id == comment.user_id).first()
        comments.append({
            'username': username.username,
            'information': comment.information,
            'icon': icon_index
        })

    log_in_username = session.get("USERNAME")
    if log_in_username:
        if form.validate_on_submit():
            user_in_db = User.query.filter(
                User.username == session.get('USERNAME')).first()
            comment = Comment(information=form.comment.data,
                              blog_id=blog.id,
                              user_id=user_in_db.id)
            db.session.add(comment)
            db.session.commit()
            form.comment.data = ""
            return redirect(url_for('blogContent', blog_name=blog_name))
    else:
        flash("You have not log in yet!")
    return render_template('blogContent.html',
                           name=blog_path,
                           form=form,
                           comments_in_db=comments,
                           original=original,
                           fans=fans,
                           comments_total=comments_total,
                           java_blog=java_blog,
                           c_blog=c_blog,
                           flask_blog=flask_blog,
                           opengl_blog=opengl_blog,
                           android_blog=android_blog,
                           python_blog=python_blog,
                           username=log_in_username)
Example #5
0
def detail(request,page_num):

    if request.method == "GET":
        form = CommentForm

    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            comment = form.cleaned_data['comment']
            a = Aritcle.objects.get(id=page_num)
            c = Comment(name = name,comment = comment,belong_to=a)
            c.save()
            return redirect(to=detail,page_num=page_num)
    context = {}
    #comment_list = Comment.objects.all()
    article = Aritcle.objects.get(id=page_num)
    context['article'] = article
    #context['comment_list'] = comment_list
    context['form'] = form
    return render(request, 'detail.html', context)
Example #6
0
def post(post_id):
    """Post detail page."""

    form = CommentForm()

    if form.validate_on_submit():
        # If valid form is submitted, add comment and redirect back to page
        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()
        return redirect(request.url)

    else:
        # No form submitted or form is invalid, show post (with errors if appropriate)
        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,
        )
Example #7
0
def publishcomment(request):
    ##使用一个form类
    content = request.POST['comment']
    blogid = request.POST['blogid']
    commentModel = Comment()
    commentModel.content = content
    commentModel.blogid_id = blogid
    commentModel.save()
    blogModelList = Blog.objects.all()
    return render(request, 'blogapp/publishblog.html',
                  {'blogModelList': blogModelList})
Example #8
0
def moderate_admin():
    if not current_user.is_admin:
        abort(403)
    comments = Comment.for_moderation().order_by(Comment.timestamp.asc())
    return render_template('blog/moderate.html', comments=comments)
Example #9
0
    #semi-perfect length :)
    if len(excerpt) > 80:
        mark_pos = excerpt.find(strip_tags(mark))
        mark_len = len(strip_tags(mark))

        #might need some improvement
        start_diff = mark_pos - 30
        end_diff = len(excerpt) - mark_pos - mark_len - 30

        #might need some improvement as well
        start = (start_diff > 0) and (start_diff) or 0
        if end_diff < 0:
            end = len(excerpt)
        else:
            end = len(excerpt) - end_diff - ((start_diff < 0) and start_diff or 0)

        excerpt = excerpt[start:end]
        excerpt = ' '.join(excerpt.split(' ')[1:-1])

    excerpt = "[...] " + excerpt + " [...]"

    c = Comment(
        author_name=escape(title), 
        author_website=escape(resource),
        content=escape(excerpt),
        date=datetime.now(),
        comment_type='linkback',
        post=post_id)
    c.save()

    return "Pingback registered. Thank you."