Esempio n. 1
0
def delete_post(request, postid):
    """
    Delete the nth post where n = postId entered from the user url
    """
    global cnt_delete
    context = {}

    if Post.exists(postid):
        try:
            fetch = Post.getPostByID(postid)
            if fetch:
                message = 'Post %d Deleted!' %(int(postid))
                fetch.delete()

                request.session['delete'] = True
                cnt_delete = count_session(request, 'delete', cnt_delete)
        except ValueError:
            raise Http404()

        return HttpResponseRedirect('/myblog/')
    else:
        msg = 'Nothing to delete!'
        ss_start = session_start_date(request)
        context = {}
        context.update(csrf(request))
        context = {'message': msg,
                   'sessionStartTime' : ss_start,
                   'sessionStat' : get_sessions(),
                   }

    res = render_to_response('../../Pythonidae/templates/index.html',
                             context,
                             context_instance=RequestContext(request))
    return res
Esempio n. 2
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
Esempio n. 3
0
def detail_post(request, postid):
    """
    Display details of the nth post where n = postId from the user url
    """
    global cnt_visit

    request.session['detail'] = True
    cnt_visit = count_session(request, 'detail', cnt_visit)
    ss_start = session_start_date(request)

    fetch = Post.getPostByID(postid)
    if fetch:
       msg = "Detail view post %d: " % (int(postid))
       context = {'post': fetch,
                  'message': msg,
                  'sessionStartTime' : ss_start,
                  'sessionStat' : get_sessions(),
                  }
       res = render_to_response('singlepost.html',
                                context,
                                context_instance=RequestContext(request))
       return res
    return HttpResponseRedirect('/myblog/')
Esempio n. 4
0
def delete_post(request, postid):
    """
    Delete the nth post where n = postId entered from the user url
    """
    context = {}
    if Post.exists(postid):
        queryset = Post.getPostByID(postid)
        if queryset:
            if queryset.author == request.user:
                message = 'Post %d Deleted!' %(int(postid))
                queryset.delete()
                return HttpResponseRedirect('/myblog/')
            else:
                return render_to_response('index.html',
                              {'message':  "You can only delete your blog posts."},
                              context_instance=RequestContext(request))
    else:
        context = {}
        context.update(csrf(request))
        context = {'message': "Nothing to delete!"}

    return render_to_response('index.html',
                             context,
                             context_instance=RequestContext(request))