Example #1
0
def individual_post(request, year=None, month=None, day=None, slug=None):
    if not slug:
        return Http404
    post = get_object_or_404(Post, name=slug)
    if request.method == 'POST':
        if 'edit_comment' in request.POST or 'delete_comment' in request.POST:
            commentform = SimpleComment(request.POST, prefix='comment')
            comment_id = commentform.data.get('comment-id', None)
            if 'edit_comment' in request.POST:
                if commentform.is_valid():
                    comment = process_comment(request, commentform, post)
                    comment_id = comment.id
                if comment_id:
                    anchor = '#comment-%s' % comment_id
                else:
                    anchor = ''
                return HttpResponseRedirect(post.get_absolute_url() + anchor)
            else:
                try:
                    Comment.objects.get(id=comment_id).delete()
                except Comment.DoesNotExist:
                    pass
        elif 'edit' in request.POST:
            return HttpResponseRedirect(
                reverse('edit-post', args=(), kwargs={'post_id': post.id}))
        elif 'publish' in request.POST:
            post.published = True
            post.save()
        elif 'retract' in request.POST:
            post.published = False
            post.save()
        elif 'restore' in request.POST:
            post.published = True
            post.save()
        elif 'delete' in request.POST:
            profile = UserProfile.objects.get(user=post.author)
            if not post.published:
                del post
            return HttpResponseRedirect(profile.get_absolute_url())
        return HttpResponseRedirect(post.get_absolute_url())
    #Logic to decide whether or not to allow comment to be added
    add_comment = post.enable_comments and post.published and request.user.is_authenticated(
    )
    # Is there an existing comment by this user that should be edited or should we get a new one?
    commentset = Comment.objects.for_model(Post).filter(
        object_pk=post.id).filter(user=request.user)
    commentset = commentset.filter(
        submit_date__gt=datetime.datetime.now() -
        datetime.timedelta(seconds=60 * 10)).order_by('-submit_date')[0:1]
    if commentset:
        latest_comment = commentset[0]
        initial = {'comment': latest_comment.comment, 'id': latest_comment.id}
    else:
        latest_comment = None
        initial = None
    # Logic to decide whether to allow editing of the form
    if request.user.is_authenticated() and (request.user == post.author
                                            or request.user.is_staff):
        return render_to_response(
            'cms/post.html', {
                'object': post,
                'form': True,
                'add_comment': add_comment,
                'commentform': SimpleComment(prefix='comment', initial=initial)
            },
            context_instance=RequestContext(request))
    # Or it is publicly viewable
    elif post.date_published:
        return render_to_response('cms/post.html', {
            'object': post,
            'form': False,
            'add_comment': add_comment
        })
    # Or we shouldn't know it exists
    else:
        raise Http404
Example #2
0
def individual_post(request, year=None, month=None, day=None, slug=None):
    """ View a post and handle dispatching various POST requests to the url """
    if not slug:
        raise Http404
    post = get_object_or_404(Post, name=slug)

    # Only authenticated users are allowed to POST any data
    if request.method == 'POST' and request.user.is_authenticated():
        if 'edit_comment' in request.POST or 'delete_comment' in request.POST:
            commentform = SimpleComment(request.POST, prefix='comment')
            comment_id = commentform.data.get('comment-id', None)
            if 'edit_comment' in request.POST:
                if commentform.is_valid():
                    comment = process_comment(request, commentform, post)
                    comment_id = comment.id
                if comment_id:
                    anchor = '#c%s' % comment_id
                else:
                    anchor = ''
                return HttpResponseRedirect(post.get_absolute_url() + anchor) 
            else:
                try:
                    c = comments.get_model().objects.get(id=comment_id)
                    if c.can_be_edited_by(request.user):
                        c.is_removed = True
                        # Delete any pigeons waiting to be sent out
                        c_model = ContentType.objects.get_for_model(c)
                        Pigeon.objects.filter(source_id=c.id,
                                source_content_type=c_model, to_send=True).delete()
                        c.save()
                    else:
                        raise PermissionDenied
                except comments.get_model().DoesNotExist:
                    raise Http404
        else:
            if not post.can_user_modify(request.user):
                raise PermissionDenied

            if 'edit' in request.POST:
                return HttpResponseRedirect(reverse('edit-post', args=(), kwargs={'post_id': post.id}))
            elif 'publish' in request.POST or 'restore' in request.POST:
                post.published = True
                post.save()
            elif 'retract' in request.POST:
                post.published = False
                post.save()
            elif 'delete' in request.POST:
                listing = post.listing
                if not post.published:
                    post.delete()
                return HttpResponseRedirect(listing.get_absolute_url())
            return HttpResponseRedirect(post.get_absolute_url())

    # Decide whether to allow editing of the Post form
    if post.can_user_modify(request.user):
        # If a user can modify a post, we assume they can comment
        return render(request, 'cms/post.html', {
            'object': post,
            'form': True,
            'add_comment': True,
            }
            )
    elif post.can_user_comment(request.user):
        # We only provide a comment form
        return render(request, 'cms/post.html', {
            'object': post,
            'form': False,
            'add_comment': True,
            }
            )
    elif post.date_published and (post.listing.can_user_read(request.user)):
        # We show the post if it published, but provide no forms
        navigation = get_base_navigation(request)
        return render(request, 'cms/post.html', {
            'object': post,
            'form': False,
            'add_comment': False,
            'navigation': navigation['navigation'],
            })
    else:
        # Otherwise we don't know about it
        raise Http404
Example #3
0
def individual_post(request, year=None, month=None, day=None, slug=None):
    if not slug:
        return Http404
    post = get_object_or_404(Post, name=slug)
    if request.method == 'POST':
        if 'edit_comment' in request.POST or 'delete_comment' in request.POST:
            commentform = SimpleComment(request.POST, prefix='comment')
            comment_id = commentform.data.get('comment-id', None)
            if 'edit_comment' in request.POST:
                if commentform.is_valid():
                    comment = process_comment(request, commentform, post)
                    comment_id = comment.id
                if comment_id:
                    anchor = '#comment-%s'%comment_id
                else:
                    anchor = ''
                return HttpResponseRedirect(post.get_absolute_url() + anchor) 
            else:
                try:
                    Comment.objects.get(id=comment_id).delete()
                except Comment.DoesNotExist:
                    pass
        elif 'edit' in request.POST:
            return HttpResponseRedirect(reverse('edit-post', args=(), kwargs={'post_id': post.id}))
        elif 'publish' in request.POST:
            post.published = True
            post.save()
        elif 'retract' in request.POST:
            post.published = False
            post.save()
        elif 'restore' in request.POST:
            post.published = True
            post.save()
        elif 'delete' in request.POST:
            profile = UserProfile.objects.get(user = post.author)
            if not post.published:
                del post
            return HttpResponseRedirect(profile.get_absolute_url())
        return HttpResponseRedirect(post.get_absolute_url())
    #Logic to decide whether or not to allow comment to be added
    add_comment =  post.enable_comments and post.published and request.user.is_authenticated()
    # Is there an existing comment by this user that should be edited or should we get a new one?
    commentset = Comment.objects.for_model(Post).filter(object_pk=post.id).filter(user=request.user)
    commentset = commentset.filter(submit_date__gt=datetime.datetime.now() - datetime.timedelta(seconds=60*10)).order_by('-submit_date')[0:1]
    if commentset:
        latest_comment = commentset[0]
        initial = {'comment': latest_comment.comment, 'id': latest_comment.id }
    else:
        latest_comment = None
        initial = None
    # Logic to decide whether to allow editing of the form
    if request.user.is_authenticated() and (request.user == post.author or request.user.is_staff):
        return render_to_response('cms/post.html', {'object': post, 'form': True,
            'add_comment': add_comment, 'commentform': SimpleComment(prefix='comment', initial=initial)},
            context_instance=RequestContext(request))
    # Or it is publicly viewable
    elif post.date_published:
        return render_to_response('cms/post.html', {'object': post, 'form': False,
            'add_comment': add_comment})
    # Or we shouldn't know it exists
    else:
        raise Http404