Beispiel #1
0
def edit(request, pk=0, template_name="edit.html"):
    """ Edit a post """
    post = get_object_or_404(Post, pk=pk, author=request.user)
    posts = Post.objects.filter(author=request.user).order_by('-pk')
    media_set = Media.objects.filter(uploader=request.user).order_by('-pk')
    media_form = MediaForm()

    if request.method == "POST":

        # media post
        if 'file' in request.FILES:
            media_form = MediaForm(request.POST, request.FILES)
            if media_form.is_valid():
                media = media_form.save(commit=False)
                media.uploader = request.user
                media.filename = unicode(media_form.cleaned_data.get('file', ''))
                media.save()

        # post post  hehe
        if 'title' in request.POST:
            form = PostForm(request.POST, instance=post)
            if form.is_valid() and 'submit_post' in request.POST:
                post = form.save(commit=False)
                if post.status == 'published':
                    if not post.publish_dt:
                        post.publish_dt = datetime.now()
                if post.status == "draft":
                    post.publish_dt = None;
                post.save()
                return render(request, template_name, {
                    'form':form, 
                    'post':post,
                    'posts':posts,
                    'media_set': media_set,
                    'media_form': media_form,
                })
        
            return render(request, template_name, {
                'form': form,
                'post': post,
                'posts': posts,
                'media_set': media_set,
                'media_form': media_form,
            })

    form = PostForm(instance=post)
    return render(request, template_name, {
        'form': form,
        'post': post,
        'posts': posts,
        'media_set': media_set,
        'media_form': media_form,
    })
Beispiel #2
0
def add(request, template_name="add.html"):
    """ Add a post """

    posts = Post.objects.filter(
        author=request.user,
        status__in=['draft', 'published']
    ).order_by('-pk')
    media_set = Media.objects.filter(uploader=request.user).order_by('-pk')
    media_form = MediaForm()

    if request.method == "POST":
        # media
        media_form = MediaForm(request.POST, request.FILES)
        if media_form.is_valid():
            media = media_form.save(commit=False)
            media.uploader = request.user
            media.filename = unicode(media_form.cleaned_data.get('file', ''))
            media.save()
            messages.info(
                request,
                'Media %s has been uploaded.' % media.filename,
                extra_tags='alert-success'
            )

        # post
        form = PostForm(request.POST, user=request.user)
        if form.is_valid() and 'submit_post' in request.POST:
            post = form.save(commit=False)

            # if something to submit
            if post.title or post.content:
                post.author = request.user
                if post.status == 'published' and not post.publish_dt:
                    post.publish_dt = datetime.now()
                post.save()
                messages.info(
                    request,
                    'Added post "%s".' % post,
                    extra_tags='alert-success')
                return redirect('edit', pk=post.pk)

    else:
        form = PostForm(user=request.user)

    return render(request, template_name, {
        'form': form,
        'posts': posts,
        'media_set': media_set,
        'media_form': media_form,
    })
Beispiel #3
0
def edit(request, pk=0, template_name="edit.html"):
    """ Edit a post """
    post = get_object_or_404(Post, pk=pk, author=request.user)
    posts = Post.objects.filter(~Q(id=post.pk), author=request.user, status__in=["draft", "published"]).order_by("-pk")
    media_set = Media.objects.filter(uploader=request.user).order_by("-pk")
    media_form = MediaForm()

    if request.method == "POST":

        # media post
        if "file" in request.FILES:
            media_form = MediaForm(request.POST, request.FILES)
            if media_form.is_valid():
                media = media_form.save(commit=False)
                media.uploader = request.user
                media.filename = unicode(media_form.cleaned_data.get("file", ""))
                media.save()

        # post post  hehe
        if "title" in request.POST:
            form = PostForm(request.POST, instance=post, user=request.user)
            if form.is_valid() and "submit_post" in request.POST:
                post = form.save(commit=False)
                if post.status == "published":
                    if not post.publish_dt:
                        post.publish_dt = datetime.now()
                if post.status == "draft":
                    post.publish_dt = None
                post.save()
                messages.info(request, 'Edited post "%s".' % post, extra_tags="alert-success")
                return render(
                    request,
                    template_name,
                    {"form": form, "post": post, "posts": posts, "media_set": media_set, "media_form": media_form},
                )

            return render(
                request,
                template_name,
                {"form": form, "post": post, "posts": posts, "media_set": media_set, "media_form": media_form},
            )

    form = PostForm(instance=post, user=request.user)
    return render(
        request,
        template_name,
        {"form": form, "post": post, "posts": posts, "media_set": media_set, "media_form": media_form},
    )
Beispiel #4
0
def add(request, template_name="add.html"):
    """ Add a post """

    posts = Post.objects.filter(author=request.user).order_by('-pk')
    media_set = Media.objects.filter(uploader=request.user).order_by('-pk')
    media_form = MediaForm()

    if request.method == "POST":

        # media
        media_form = MediaForm(request.POST, request.FILES)
        if media_form.is_valid():
            media = media_form.save(commit=False)
            media.uploader = request.user
            media.filename = unicode(media_form.cleaned_data.get('file', ''))
            media.save()

        # post
        form = PostForm(request.POST)
        if form.is_valid() and 'submit_post' in request.POST:
            post = form.save(commit=False)

            # if something to submit
            if post.title or post.content:
                post.author = request.user
                if post.status == 'published':
                    post.publish_dt = datetime.now()
                post.save()
                return redirect('edit', pk=post.pk)

    else:
        form = PostForm()

    return render(request, template_name, {
        'form': form,
        'posts': posts,
        'media_set': media_set,
        'media_form': media_form,
    })
Beispiel #5
0
def add(request, template_name="add.html"):
    """ Add a post """

    posts = Post.objects.filter(author=request.user, status__in=["draft", "published"]).order_by("-pk")
    media_set = Media.objects.filter(uploader=request.user).order_by("-pk")
    media_form = MediaForm()

    if request.method == "POST":
        # media
        media_form = MediaForm(request.POST, request.FILES)
        if media_form.is_valid():
            media = media_form.save(commit=False)
            media.uploader = request.user
            media.filename = unicode(media_form.cleaned_data.get("file", ""))
            media.save()
            messages.info(request, "Media %s has been uploaded." % media.filename, extra_tags="alert-success")

        # post
        form = PostForm(request.POST, user=request.user)
        if form.is_valid() and "submit_post" in request.POST:
            post = form.save(commit=False)

            # if something to submit
            if post.title or post.content:
                post.author = request.user
                if post.status == "published" and not post.publish_dt:
                    post.publish_dt = datetime.now()
                post.save()
                messages.info(request, 'Added post "%s".' % post, extra_tags="alert-success")
                return redirect("edit", pk=post.pk)

    else:
        form = PostForm(user=request.user)

    return render(
        request, template_name, {"form": form, "posts": posts, "media_set": media_set, "media_form": media_form}
    )
Beispiel #6
0
def add(request, template_name="add.html"):
    """ Add a post """

    posts = Post.objects.filter(author=request.user,
                                status__in=['draft',
                                            'published']).order_by('-pk')
    media_set = Media.objects.filter(uploader=request.user).order_by('-pk')
    media_form = MediaForm()

    if request.method == "POST":
        # media
        media_form = MediaForm(request.POST, request.FILES)
        if media_form.is_valid():
            media = media_form.save(commit=False)
            media.uploader = request.user
            media.filename = unicode(media_form.cleaned_data.get('file', ''))
            media.save()
            messages.info(request,
                          'Media %s has been uploaded.' % media.filename,
                          extra_tags='alert-success')

        # post
        form = PostForm(request.POST, user=request.user)
        if form.is_valid() and 'submit_post' in request.POST:
            post = form.save(commit=False)

            # if something to submit
            if post.title or post.content:
                post.author = request.user
                if post.status == 'published' and not post.publish_dt:
                    post.publish_dt = datetime.now()
                post.save()
                messages.info(request,
                              'Added post "%s".' % post,
                              extra_tags='alert-success')
                return redirect('edit', pk=post.pk)

    else:
        form = PostForm(user=request.user)

    return render(
        request, template_name, {
            'form': form,
            'posts': posts,
            'media_set': media_set,
            'media_form': media_form,
        })
Beispiel #7
0
def edit(request, pk=0, template_name="edit.html"):
    """ Edit a post """
    post = get_object_or_404(Post, pk=pk, author=request.user)
    posts = Post.objects.filter(~Q(id=post.pk),
                                author=request.user,
                                status__in=['draft',
                                            'published']).order_by('-pk')
    media_set = Media.objects.filter(uploader=request.user).order_by('-pk')
    media_form = MediaForm()

    if request.method == "POST":

        # media post
        if 'file' in request.FILES:
            media_form = MediaForm(request.POST, request.FILES)
            if media_form.is_valid():
                media = media_form.save(commit=False)
                media.uploader = request.user
                media.filename = unicode(
                    media_form.cleaned_data.get('file', ''))
                media.save()

        # post post  hehe
        if 'title' in request.POST:
            form = PostForm(request.POST, instance=post, user=request.user)
            if form.is_valid() and 'submit_post' in request.POST:
                post = form.save(commit=False)
                if post.status == 'published':
                    if not post.publish_dt:
                        post.publish_dt = datetime.now()
                if post.status == "draft":
                    post.publish_dt = None
                post.save()
                messages.info(request,
                              'Edited post "%s".' % post,
                              extra_tags='alert-success')
                return render(
                    request, template_name, {
                        'form': form,
                        'post': post,
                        'posts': posts,
                        'media_set': media_set,
                        'media_form': media_form,
                    })

            return render(
                request, template_name, {
                    'form': form,
                    'post': post,
                    'posts': posts,
                    'media_set': media_set,
                    'media_form': media_form,
                })

    form = PostForm(instance=post, user=request.user)
    return render(
        request, template_name, {
            'form': form,
            'post': post,
            'posts': posts,
            'media_set': media_set,
            'media_form': media_form,
        })