Exemple #1
0
def edit_post(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)

    if form.validate_on_submit():
        post.title = form.title.data
        post.subtitle = form.subtitle.data
        post.body = form.body.data
        post.update_time = datetime.utcnow()
        post.category = Category.query.get(form.category.data)
        post.topic = Topic.query.get(form.topic.data)
        db.session.commit()

        img_path = current_app.root_path + '/static/img/' + str(
            post.topic.name)
        for f in request.files.getlist('image'):
            filename = f.filename
            f.save(os.path.join(img_path, filename))

        flash('Post updated.', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))

    form.title.data = post.title
    form.subtitle.data = post.subtitle
    form.body.data = post.body
    form.category.data = post.category_id
    form.topic.data = post.topic_id
    return render_template('admin/edit_post.html', form=form)
Exemple #2
0
def new_post():
    form = PostForm()

    if form.validate_on_submit():
        title = form.title.data
        subtitle = form.subtitle.data
        theme = request.files.getlist('image')[0].filename
        body = form.body.data
        category = Category.query.get(form.category.data)
        topic = Topic.query.get(form.topic.data)
        post = Post(title=title,
                    subtitle=subtitle,
                    theme=theme,
                    body=body,
                    category=category,
                    topic=topic)
        # same with:
        # category_id = form.category.data
        # post = Post(title=title, body=body, category_id=category_id)
        db.session.add(post)
        db.session.commit()

        img_list = request.files.getlist('image')
        img_path = current_app.root_path + '/static/img/' + str(topic.name)
        if img_list[0].filename:

            for f in img_list:
                filename = f.filename
                f.save(os.path.join(img_path, filename))

        flash('Post created.', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))

    return render_template('admin/new_post.html', form=form)
Exemple #3
0
def post_form_test(request):
    if request.method == 'GET':
        form = PostForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation
    else:
        form = PostForm()
    return render(request, 'myblog/post_list.html', {'form': form})
Exemple #4
0
def add_post(request):
    form = PostForm(request.POST or None)
    if form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.save()
        return redirect(post)
    return render_to_response('add_post.html', { 'form': form }, context_instance=RequestContext(request))
Exemple #5
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        category = Category.query.get(form.category.data)
        body = form.body.data
        post = Post(title=title, category=category, body=body)
        db.session.add(post)
        db.session.commit()
        flash('文章已创建', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
Exemple #6
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.category.data)
        post = Post(title=title, body=body, category=category)
        db.session.add(post)
        db.session.commit()
        flash("post created", "success")
        return redirect(url_for("blog.show_post", post_id=post.id))
    return render_template("admin/new_post.html", form=form)
Exemple #7
0
def edit_post(post_id):
    post = Post.query.get(post_id)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.category = Category.query.get(form.category.data)
        db.session.commit()
        flash("edit success!", "success")
        return redirect(url_for("blog.show_post", post_id=post.id))
    form.category.data = post.category_id
    form.title.data = post.title
    form.body.data = post.body
    return render_template("admin/edit_post.html", form=form)
Exemple #8
0
def edit_post(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.category = Category.query.get(form.category.data)
        db.session.commit()
        flash('Post updated.', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    form.title.data = post.title
    form.body.data = post.body
    form.category.data = post.category_id
    return render_template('admin/edit_post.html', form=form)
Exemple #9
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        private = form.private.data
        # category = Category.query.get(form.category.data)
        categories = [Category.query.get(i) for i in form.categories.data]
        post = Post(title=title, body=body, private=private, categories=categories)
        db.session.add(post)
        db.session.commit()
        flash("Post created.", "success")
        return redirect(url_for("blog.show_post", post_id=post.id))
    return render_template("admin/new_post.html", form=form)
Exemple #10
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.category.data)
        post = Post(title=title, body=body, category=category, read=1)
        # same with:
        # category_id = form.category.data
        # post = Post(title=title, body=body, category_id=category_id)
        db.session.add(post)
        db.session.commit()
        flash('Post created.', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
Exemple #11
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        #save it into database
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        #end
        flash('your post has been creatd', 'success')
        return redirect(url_for('home'))
    return render_template('create_post.html',
                           title='Post',
                           form=form,
                           legend='Post')
Exemple #12
0
def NewPost():
    form = PostForm()
    if form.validate_on_submit():
        pst = Post(title=form.title.data,
                   content=form.content.data,
                   smallcontent=form.smallcontent.data,
                   author=current_user)
        db.session.add(pst)
        db.session.commit()
        flash(f'Post Added Succecfully', 'success')
        return redirect(url_for('UserProfile',
                                user_name=current_user.username))
    return render_template("NewPost.html",
                           form=form,
                           title="Add A New Post",
                           leg="Add New Post",
                           user=current_user)
Exemple #13
0
def add_post(request):
    """
    Add a new post to the blog and it will be saved into the model,
    then the last added element will be fetched to show the details
    """
    global cnt_create

    if request.method == 'POST' and request.POST.get('save'):
        form = PostForm(request.POST)
        if form.is_valid():
            form.save(commit=False)
            form.save()
            message =  "New post %d added!"
            request.session['create'] = True
            cnt_create = count_session(request, 'create', cnt_create)
            return HttpResponseRedirect('/myblog/')
    elif request.method == 'POST' and request.POST.get('cancel'):
        message = "Adding new post cancelled!"
        return HttpResponseRedirect('/myblog/')
    else:
        form = PostForm()
    ss_start = session_start_date(request)
    context = {'sessionStartTime' : ss_start,
               'sessionStat' : get_sessions(),
               }
    context.update(csrf(request))
    context['form'] = form
    res = render_to_response('addblog.html',
                             context,
                             context_instance=RequestContext(request))
    return res
Exemple #14
0
def edit_post(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.private = form.private.data
        # post.category = Category.query.get(form.category.data)
        categories = [Category.query.get(i) for i in form.categories.data]
        post.categories = categories
        db.session.commit()
        flash("Post <%s> updated." % post.title, "success")
        return redirect(url_for("blog.show_post", post_id=post.id))
    form.title.data = post.title
    form.body.data = post.body
    form.private.data = post.private
    form.categories.data = [category.id for category in post.categories]
    return render_template("admin/edit_post.html", form=form)
Exemple #15
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('your post has been updated', 'success')
        return redirect(url_for('post', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
    return render_template('create_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post')
Exemple #16
0
def edit_post(request, postid):
    """
    Fetch the nth post from the blog, validate the input data and update the model,
    """
    global cnt_edit

    queryset = Post.getPostByID(postid)
    ss_start = session_start_date(request)

    if queryset:
        queryset = Post.getPostByID(postid)
        context = {'post': queryset,
                   'sessionStartTime' : ss_start,
                   'sessionStat' : get_sessions(),
                   }
    else:
        queryset = Post.objects.get(id=postid)

    if request.method == 'POST' and request.POST.get('update'):
        # Create a form to edit an existing Post, but use
        # POST data to populate the form!
        p_instance = Post.objects.get(pk=postid)
        update_form = PostForm(request.POST, instance=p_instance)
        if update_form.is_valid():
            update_form.save()

            request.session["edit"] = True
            cnt_edit = count_session(request, 'edit', cnt_edit)
            ss_start = session_start_date(request)

            message = "Post %d: updated! " %(int(postid))
            context = {'post': queryset, 'message': message}
            return HttpResponseRedirect('/myblog/'+ postid)
    elif request.method == 'POST' and request.POST.get('cancel'):
        message = "Updating post cancelled!"
        return HttpResponseRedirect('/myblog/')
    else:
        message = "Updating post interrupted!"

    res = render_to_response('editblog.html',
                              context,
                              context_instance=RequestContext(request))
    return res
Exemple #17
0
def post_form_upload(request):
    if request.method == 'GET':
        form = PostForm()
    else:
        # A POST request: Handle Form Upload
        form = PostForm(request.POST) # Bind data from request.POST into a PostForm
 
        # If data is valid, proceeds to create a new post and redirect the user
        if form.is_valid():
            content = form.cleaned_data['content']
            created_at = form.cleaned_data['created_at']
            post = m.Post.objects.create(content=content,
                                         created_at=created_at)
            return HttpResponseRedirect(reverse('post_detail',
                                                kwargs={'post_id': post.id}))
 
    return render(request, 'post/post_form_upload.html', {
        'form': form,
    })
def add_view(request):
    user = request.user
    if not user.is_authenticated:
        raise PermissionDenied
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid:
            post = form.save()
            msg = "post '%s' saved" % post
            messages.add_message(request, messages.INFO, msg)
            return HttpResponseRedirect(reverse('blog_index'))
        else:
            messages.add_message("please fix the errors below")
    else:
        initial = {'author': user}
        form = PostForm(initial=initial)
    
    context = {'form': form}
    return render(request, 'add.html', context)
Exemple #19
0
 def get(self, request):
     forms = {'post': PostForm(), 'tag': TagForm()}
     return render(
         request,
         'create_update_post.html',
         {
             'forms': forms,
             'action': 'new',
             'header': 'Novo Post'
         },
     )
Exemple #20
0
 def get(self, request, pk):
     post = Post.objects.get(pk=pk)
     forms = {'post': PostForm(instance=post), 'tag': TagForm()}
     return render(
         request,
         'create_update_post.html',
         {
             'forms': forms,
             'action': 'edit',
             'header': 'Editar Post'
         },
     )
Exemple #21
0
def add_post(request):
    """
    Add a new post to the blog and it will be saved into the model,
    then the last added element will be fetched to show the details
    """
    if request.method == 'POST' and request.POST.get('save'):
        form = PostForm(request.POST)
        if form.is_valid():
            new_blogpost = form.save(commit=False)
            new_blogpost.author = request.user
            form.save()
            message =  "New post %d added!"
            return HttpResponseRedirect('/myblog/')
    elif request.method == 'POST' and request.POST.get('cancel'):
        message = "Adding new post cancelled!"
        return HttpResponseRedirect('/myblog/')
    else:
        form = PostForm()

    context = {}
    context.update(csrf(request))
    context['form'] = form
    return render_to_response('addblog.html',
                             context,
                             context_instance=RequestContext(request))
Exemple #22
0
def UpdatePost(post_id):
    post = Post.query.get(post_id)
    if (post.author != current_user):
        abort(403)
    else:
        form = PostForm()
        if form.validate_on_submit():
            post.title = form.title.data
            post.content = form.content.data
            post.smallcontent = form.smallcontent.data
            db.session.commit()
            flash(f'Post Updated Succecfully', 'success')
            return redirect(url_for('FullPost', post_id=post.id))
        elif request.method == "GET":
            form.title.data = post.title
            form.smallcontent.data = post.smallcontent
            form.content.data = post.content
        return render_template("NewPost.html",
                               form=form,
                               title="Update Post :" + str(post_id),
                               leg="Update Poste",
                               user=current_user)
Exemple #23
0
def edit_post(request, postid):
    """
    Fetch the nth post from the blog, validate the input data and update the model,
    """
    try:
        queryset = Post.objects.get(id=postid)
        context = {'post': queryset,}
        if request.method == 'POST' and request.POST.get('update'):
            # Create a form to edit an existing Post, but use
            # POST data to populate the form!
            p_instance = Post.objects.get(pk=postid)
            if p_instance.author == request.user:
                update_form = PostForm(request.POST, instance=p_instance)
                if update_form.is_valid():
                    update_form.save()
                    msg = "Post %d: updated! " %(int(postid))
                    context = {'post': queryset, 'message': msg}
                    return HttpResponseRedirect('/myblog/'+ postid)
            else:
                msg = "You can only edit your blog posts."
                context = {'message': msg}
                return render_to_response('index.html',
                              {'message': msg},
                              context_instance=RequestContext(request))

        elif request.method == 'POST' and request.POST.get('cancel'):
            msg = "Updating post cancelled!"
            return HttpResponseRedirect('/myblog/')
        else:
            msg = "Updating post interrupted!"
            return render_to_response('editblog.html',
                              context,
                              context_instance=RequestContext(request))
    except Post.DoesNotExist:
        msg = "No blog post found for edit."
        context = {'message': msg,}
        return render_to_response('index.html',context,
                                context_instance=RequestContext(request))
Exemple #24
0
def post_form_view(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.timestamp = timezone.now()
            model_instance.save()
            return redirect('/blog')
    else:
        form = PostForm()
        return render(request, "post_form.html", {'form': form})
Exemple #25
0
def post_edit(request, pk):#edit an existing post
        post = get_object_or_404(Post, pk=pk)
        if request.method == "POST":
            form = PostForm(request.POST, instance=post)
            if form.is_valid():
                post = form.save(commit=False)
                post.author = request.user
                post.save()
                return redirect('myblog.views.post_detail', pk=post.pk)
        else:
            form = PostForm(instance=post)
        return render(request, 'myblog/post_edit.html', {'form': form})
def add_view(request):

    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.published_date = timezone.now()
            model_instance.save()
            return redirect('/')

    else:

        form = PostForm()

        return render(request, 'add_post.html', {'form': form})
Exemple #27
0
def new_post(request):#creation of new post
    if request.method == "POST":#if the form is completed, we go to post_detail page
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            if post.is_draft == False:
                post.published_date = timezone.now()
            else:#post is draft
                post.published_date = None
            post.save()
            return redirect('myblog.views.post_detail', pk=post.pk)
    else:#want to create a new post, so turn on page post_edit
        form = PostForm()
    return render(request, 'myblog/post_edit.html', {'form': form})
Exemple #28
0
def add_model(request):

    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.created_date = datetime.datetime.now()
            model_instance.published_date = datetime.datetime.now()
            model_instance.save()
            return redirect('/')

    else:

        form = PostForm()

        return render(request, "my_template.html", {'form': form})
Exemple #29
0
def addpost(request):
	global previousRequest
	previousRequest = "addpost"
	if request.method == 'POST':
		post_form = PostForm(request.POST)
		if post_form.is_valid():
			post = post_form.save(commit = False)
			post.published_date = timezone.now();
			post.author = request.user
			post.save()
			return HttpResponseRedirect(reverse('myblog:index'))
		else:
			context_dict = {'post_form': post_form}
			return render(request, 'myblog/addpost.html', context_dict)
	else:
		post_form = PostForm()
		context_dict = {'post_form': post_form}
		return render(request, 'myblog/addpost.html', context_dict)
def addpost(request):
    if request.method == 'POST':
        post_form = PostForm(request.POST)
        if post_form.is_valid():
            post = post_form.save(commit=False)
            post.published_date = timezone.now()
            post.author = request.user
            post.save()
            global posts
            posts = Post.objects.filter(
                author=request.user).order_by('-published_date')
            return HttpResponseRedirect(reverse('myblog:index'))
        else:
            context_dict = {'post_form': post_form}
            return render(request, 'myblog/addpost.html', context_dict)
    else:
        post_form = PostForm()
        context_dict = {'post_form': post_form}
        return render(request, 'myblog/addpost.html', context_dict)
Exemple #31
0
def editpost(request, pk):
	post = get_object_or_404(Post, pk = pk)
	global previousRequest
	if post.author != request.user:
		previousRequest = "bug"
		return HttpResponseRedirect(reverse('myblog:index'))
	else:
		previousRequest = "editpost"
	if request.method == 'POST':
		post_form = PostForm(request.POST, instance = post)
		if post_form.is_valid():
			post = post_form.save(commit = False)
			post.published_date = timezone.now();
			post.author = request.user
			post.save()
			return HttpResponseRedirect(reverse('myblog:index'))
		else:
			context_dict = {'post_form': post_form}
			return render(request, 'myblog/editpost.html', context_dict)
	else:
		post_form = PostForm(instance = post)
		context_dict = {'post_form': post_form, 'pk': pk}
		return render(request, 'myblog/editpost.html', context_dict)
Exemple #32
0
 def post(self, request, pk):
     post = Post.objects.get(pk=pk)
     form = PostForm(request.POST, instance=post)
     form.save()
     return HttpResponseRedirect(reverse('post_detail', args=(pk, )))
Exemple #33
0
 def post(self, request):
     form = PostForm(request.POST)
     new_post_instance = form.save()
     return HttpResponseRedirect(
         reverse('post_detail', args=(new_post_instance.id, )))