Exemplo n.º 1
0
def create_post(request):

    post_created = False

    if request.method == 'POST':
        post_form = NewPostForm(data=request.POST)

        if post_form.is_valid():
            new_post = post_form.save(commit=False)
            new_post.author = request.user

            if 'featured_image' in request.FILES:
                new_post.featured_image = request.FILES['featured_image']

            new_post.save()

            post_created = True

        else:
            print(post_form.errors)
    else:
        post_form = NewPostForm
    return render(request, 'createpost.html', {
        'post_form': post_form,
        'post_created': post_created,
    })
Exemplo n.º 2
0
def new_post():
    form = NewPostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash("Your post has been created", 'success')
        return redirect(url_for("index"))
    return render_template("new_post.html",
                           title="New Post",
                           form=form,
                           legend="New Post")
Exemplo n.º 3
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = NewPostForm()
    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("index"))
    elif request.method == "GET":
        form.title.data = post.title
        form.content.data = post.content
    return render_template("new_post.html",
                           title="Update Post",
                           form=form,
                           legend="Update Post")
Exemplo n.º 4
0
def do_post(request):
    form = NewPostForm()
    if request.method == "POST":
        post = request.POST.copy()
        form = NewPostForm(post, user=request.user)
        if form.is_valid():
            new_post = Post()
            new_post.user = request.user
            new_post.title = post.get("title")
            new_post.draft = post.get("draft", False)
            new_post.source = post.get("content")
            new_post.save()
            new_post.tags = post.get("tags", "")
            return HttpResponseRedirect(reverse("profile", kwargs={"slug": request.user.get_profile().slug}))
        try:
            original_post = Post.objects.get(blog__owner=request.user, slug=slugify(post.get("title"))).html
        except:
            original_post = None
    return "post.html", {"post_form": form, "editing": True, "original_post": original_post}
Exemplo n.º 5
0
def add_post(request):
    try:
        if not request.user.is_authenticated():
            return HttpResponseRedirect('/login')

        template = 'blog/add-post.html'
        args = {}

        if request.method == 'POST':
            form = NewPostForm(request.POST)
            if form.is_valid():
                visible = True if form.cleaned_data['visible'] == 'public' else False
                new_post = Post(user=request.user,
                                title=form.cleaned_data['title'],
                                body=form.cleaned_data['body'],
                                visible=visible,)
                new_post.save()

                tags = form.cleaned_data['tags'].split()
                existing_tags = [tag.tag for tag in Tag.objects.all()]
                for tag in tags:
                    if tag not in existing_tags:
                        new_tag = Tag(tag=tag)
                        existing_tags.append(tag)
                        new_tag.save()
                    else:
                        new_tag = Tag.objects.get(tag__contains=tag)

                    new_post.tag.add(new_tag)

                return HttpResponseRedirect('/' + request.user.username + '/post' + str(new_post.id))
        else:
            form = NewPostForm()

        args.update({'form': form})
    except Exception as e:
        e = str(e)
        raise Http404(e)

    return render_to_response(template, args, context_instance=RequestContext(request))
Exemplo n.º 6
0
def new_post(request, name):
    blog = Blog.objects.get(name = name)
    if request.method == 'POST':
        form = NewPostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.blog = blog
            post.save()
        else:
            return HttpResponse("Niepoprawnie wypelniony formularz.")

    else:
        blog_authors = Post.objects.values('author').distinct()
        form = NewPostForm({'blog': blog, 'comments': 0})    
        return render_to_response('blog/new_post.html', {'form': form,
                                                         'blog': blog,
                                                         'blog_authors': blog_authors,
                                                         'user': request.user,
                                                         }, context_instance = RequestContext(request)
                                  )
    
    return HttpResponseRedirect(reverse('blog.views.blog', args = (blog.name, )))
Exemplo n.º 7
0
def PostIndexView(request):
    if request.method == 'POST':
        fr = NewPostForm(request.POST)
        if fr.is_valid():
            fr.save()
            return redirect('blog_urls:blog_url')
    else:
        posts = Post.objects.all().order_by('-created_date')
        Form = NewPostForm()
    return render(request, 'index.html', {'tPosts': posts, 'tForm': Form})