Exemplo n.º 1
0
def post(request, post_id, action):

        is_edit = action == 'edit'
        the_post = None
        if post_id:
                the_post = get_post_by_id(post_id)
                if not the_post:
                        return message(request,
                                        "The post you asked for doesn't exist")

        if is_edit and not request.user.has_object_perm(the_post, 'edit'):
                return message(request,
                                "You have no permissions to edit this post")

        if request.method == "POST":
                if not is_edit:
                        the_post = Post()
                        the_post.user_id = request.user.id
                        the_post.posted_at = datetime.datetime.utcnow()
                        the_post.views = 0
                form = PostForm(request.POST, instance=the_post)
                if form.is_valid():
                        form.save()
                        request.user.grant_object_perm(the_post,
                                        ['edit', 'delete'])
                        return HttpResponseRedirect(reverse('main.views.index',
                                args=(request.user.username,)))
                else:
                        return render_to_response(request, 'post.html',
                                        {'form':form},
                                        context_instance=RequestContext(request))
        else:
                instance = None
                if is_edit:
                        instance = the_post
                form = PostForm(instance=instance)
                return render_to_response(request, 'post.html', {'form':form},
                                 context_instance=RequestContext(request))
Exemplo n.º 2
0
def view(request, post_id):
        the_post = get_post_by_id(post_id)
        if not the_post:
                return message(request, "The post you asked for doesn't exist")
        if request.method == "POST":
                the_comment = Comment()
                the_comment.on_post_id = the_post.id
                the_comment.user_id = request.user.id
                form = CommentForm(request.POST, instance=the_comment)
                if form.is_valid():
                        form.save()
                        return HttpResponseRedirect(reverse('main.views.view',
                                args=(post_id,)))
        comments = Comment.objects.filter(on_post__id=the_post.id).order_by('posted_at')
        d = {'post':the_post, 'comments':comments}
        if request.user.is_authenticated():
                d['comment_form'] = CommentForm()
        the_post.views += 1
        the_post.save()
        return render_to_response(request, 'view.html', d,
                        context_instance=RequestContext(request))
Exemplo n.º 3
0
def index(request, username, year, month):
        posts = get_posts(username=username).order_by("-posted_at")

        if year:
                posts = posts.filter(posted_at__year=year)
        if month:
                posts = posts.filter(posted_at__month=month)
        for p in posts:
                p.shorten_post()
        pages = Paginator(posts, settings.ITEMS_PER_PAGE)
        if request.GET.has_key('p'):
                try:
                        curr_page = int(request.GET['p'])
                except ValueError:
                        curr_page = 1

                if curr_page > pages.num_pages:
                        curr_page = pages.num_pages
                elif curr_page < 1:
                        curr_page = 1
        else:
                curr_page = 1
        d = {'posts':pages.page(curr_page),
             'sidebar':get_sidebar(posts=posts, username=username)}
        if pages.num_pages > 1:
                last_page = pages.num_pages
                first_page = 1
                prev_page = curr_page
                next_page = curr_page
                if curr_page > 1:
                        prev_page -= 1
                if curr_page < pages.num_pages:
                        next_page += 1
                paging = {'first':first_page, 'last':last_page, 'next':next_page,
                                'prev':prev_page}
                d['paging'] = paging
        return render_to_response(request, 'index.html', d)
Exemplo n.º 4
0
def message(request, msg):
        return render_to_response(request, 'message.html',
                        {'message':msg},
                         context_instance=RequestContext(request))
Exemplo n.º 5
0
def search(request):
        posts = Post.objects.filter(post__contains=request.GET['s']).order_by('-posted_at')
        return render_to_response(request, 'index.html',
                        {'posts':posts, 'sidebar':get_sidebar()})