Example #1
0
def edit_comment_sign_up(request, slug, comment_id):
    comment = get_object_or_404(PageComment, page__project__slug=slug,
        page__slug='sign-up', id=comment_id)
    if not comment.can_edit(request.user):
        return http.HttpResponseForbidden(_("You can't edit this comment"))
    abs_reply_to = comment
    while abs_reply_to.reply_to:
        abs_reply_to = abs_reply_to.reply_to
    if abs_reply_to == comment:
        abs_reply_to = reply_to = None
    else:
        reply_to = comment.reply_to
    preview = False
    profile = comment.author
    if request.method == 'POST':
        form = CommentForm(request.POST, instance=comment)
        profile_form = ProfileEditForm(request.POST, instance=profile)
        profile_image_form = ProfileImageForm()
        if form.is_valid() and (reply_to or profile_form.is_valid()):
            if not reply_to:
                profile = profile_form.save(commit=False)
            comment = form.save(commit=False)
            if 'show_preview' in request.POST:
                preview = True
                comment.content = bleach.clean(comment.content,
                    tags=settings.RICH_ALLOWED_TAGS,
                    attributes=settings.RICH_ALLOWED_ATTRIBUTES,
                    styles=settings.RICH_ALLOWED_STYLES, strip=True)
                if not reply_to:
                    profile.bio = bleach.clean(profile.bio,
                        tags=settings.REDUCED_ALLOWED_TAGS,
                        attributes=settings.REDUCED_ALLOWED_ATTRIBUTES,
                        strip=True)
            else:
                if not reply_to:
                    profile.save()
                comment.save()
                if reply_to:
                    success_msg = _('Comment updated!')
                else:
                    success_msg = _('Answer updated!')
                messages.success(request, success_msg)
                return http.HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request, _('Please correct errors bellow.'))
    else:
        profile_form = ProfileEditForm(instance=comment.author)
        profile_image_form = ProfileImageForm()
        form = CommentForm(instance=comment)
    return render_to_response('content/comment_sign_up.html', {
        'profile_image_form': profile_image_form,
        'profile_form': profile_form,
        'profile': profile,
        'form': form,
        'project': comment.page.project,
        'page': comment.page,
        'reply_to': reply_to,
        'comment': comment,
        'preview': preview,
    }, context_instance=RequestContext(request))
Example #2
0
def edit_comment(request, slug, page_slug, comment_id):
    comment = get_object_or_404(PageComment, id=comment_id,
        page__slug=page_slug, page__project__slug=slug)
    if not comment.can_edit(request.user):
        return http.HttpResponseForbidden(_("You can't edit this page"))
    preview = False
    if request.method == 'POST':
        form = CommentForm(request.POST, instance=comment)
        if form.is_valid():
            comment = form.save(commit=False)
            if 'show_preview' in request.POST:
                preview = True
                comment.content = bleach.clean(comment.content,
                    tags=settings.RICH_ALLOWED_TAGS,
                    attributes=settings.RICH_ALLOWED_ATTRIBUTES,
                    styles=settings.RICH_ALLOWED_STYLES, strip=True)
            else:
                comment.save()
                messages.success(request, _('Comment updated!'))
                return http.HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request, _('Please correct errors bellow.'))
    else:
        form = CommentForm(instance=comment)
    return render_to_response('content/comment_page.html', {
        'form': form,
        'comment': comment,
        'page': comment.page,
        'project': comment.page.project,
        'reply_to': comment.reply_to,
        'preview': preview,
    }, context_instance=RequestContext(request))
Example #3
0
def view_post(id):
    post = Post.query.get_or_404(id)
    form = CommentForm()
    
    if form.validate_on_submit():
        if not current_user.can_comment:
          abort(404)
        comment = Comment()
        form.populate_obj(comment)
        comment.author_id = current_user.id
        comment.post_id = post.id
        db.session.add(comment)
        db.session.commit()
        flash("New Comment successfully added")
        return redirect(url_for("view_post", id=post.id))
    return render_template("content/view_post.html", post=post, form=form)
Example #4
0
def post_view(request,id):
    comments=Comment.objects.filter(post_to=id)
    comment=CommentForm()
    post=Post.objects.get(id=id)
    if request.method=='POST':
        #Создание коммента
        if 'cre_comm' in request.POST:
            comment=CommentForm(request.POST)
            if comment.is_valid():
                comment.create_comm(request,id)
                return HttpResponseRedirect(reverse('post_view',args=[id,]))
        #Удаление коммента
        if 'delete_comment_id' in request.POST:
            comment_del=Comment.objects.get(id=int(request.POST['delete_comment_id']))
            comment_del.delete_comment(id)
    return render_to_response('post_view.html',{'post':post,'comments':comments,'comment':comment},context_instance=RequestContext(request))
Example #5
0
def comment_page(request, slug, page_slug, comment_id=None):
    page = get_object_or_404(Page, project__slug=slug, slug=page_slug)
    if not page.editable:
        return http.HttpResponseForbidden(_("You can't edit this page"))
    user = request.user.get_profile()
    reply_to = abs_reply_to = None
    if comment_id:
        reply_to = page.comments.get(pk=comment_id)
        abs_reply_to = reply_to
        while abs_reply_to.reply_to:
            abs_reply_to = abs_reply_to.reply_to
    preview = False
    comment = None
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.page = page
            comment.author = user
            comment.reply_to = reply_to
            comment.abs_reply_to = abs_reply_to
            if 'show_preview' in request.POST:
                preview = True
                comment.content = bleach.clean(comment.content,
                    tags=settings.RICH_ALLOWED_TAGS,
                    attributes=settings.RICH_ALLOWED_ATTRIBUTES,
                    styles=settings.RICH_ALLOWED_STYLES, strip=True)
            else:
                comment.save()
                messages.success(request, _('Comment posted!'))
                return http.HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request, _('Please correct errors bellow.'))
    else:
        form = CommentForm()
    return render_to_response('content/comment_page.html', {
        'form': form,
        'project': page.project,
        'page': page,
        'reply_to': reply_to,
        'comment': comment,
        'create': True,
        'preview': preview,
    }, context_instance=RequestContext(request))
Example #6
0
def comment_page(request, slug, page_slug, comment_id=None):
    page = get_object_or_404(Page, project__slug=slug, slug=page_slug)
    if not page.editable:
        return HttpResponseForbidden(_("You can't edit this page"))
    user = request.user.get_profile()
    reply_to = abs_reply_to = None
    if comment_id:
        reply_to = page.comments.get(pk=comment_id)
        abs_reply_to = reply_to
        while abs_reply_to.reply_to:
            abs_reply_to = abs_reply_to.reply_to
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.page = page
            comment.author = user
            comment.reply_to = reply_to
            comment.abs_reply_to = abs_reply_to
            comment.save()
            messages.success(request, _('Comment posted!'))
            return HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request,
                           _('There was a problem posting the comment. Comments cannot be empty.'))
    else:
        form = CommentForm()
    return render_to_response('content/comment_page.html', {
        'form': form,
        'project': page.project,
        'page': page,
        'reply_to': reply_to,
    }, context_instance=RequestContext(request))
Example #7
0
def post(request, slug, error_messages=None):
    context = RequestContext(request)

    if not error_messages:
        comment_form = CommentForm(initial={'slug': slug})

    post = get_object_or_404(Post, slug=slug)
    if post.deleted:
        raise Http404

    post.created_on_humanize = arrow.get(post.created_on).humanize()

    if request.user.is_authenticated():
        can_comment = get_user_permissions(request)['can_comment']
        try:
            post.vote = PostVote.objects.get(post=post, user=request.user).vote
        except PostVote.DoesNotExist:
            post.vote = 0
    else:
        can_comment = None

    post.score = PostVote.objects.filter(post=post).aggregate(Sum('vote'))['vote__sum']

    if post.post_type == 'link':
        post.domain = domain_name(post.url)
    post.is_recent = (timezone.now() - post.created_on) < timedelta(days=1)

    all_comments = Comment.objects.filter(post=post).order_by('-created_on')
    for comment in all_comments:
        comment.created_on_humanize = arrow.get(comment.created_on).humanize()
        comment.delete_allowed = comment.can_delete(request.user)
        if request.user.is_authenticated():
            try:
                comment.vote = CommentVote.objects.get(comment=comment,
                                                       user=request.user).vote
            except CommentVote.DoesNotExist:
                comment.vote = 0

        comment.score = CommentVote.objects.filter(comment=comment).aggregate(Sum('vote'))['vote__sum']

    context_dict = {
        'post': post,
        'user': request.user,
        'can_comment': can_comment,
        'can_delete': post.can_delete(request.user),
        'comments': all_comments,
        'comment_form': comment_form,
        'moderator': request.user.username in MODERATORS
    }

    return render_to_response('content/post.html', context_dict, context)
Example #8
0
def add_comment(request):
    context = RequestContext(request)

    if request.method == 'POST':
        comment_form = CommentForm(request.POST)

        if comment_form.is_valid():
            post_slug = comment_form.cleaned_data['slug']
            post = get_object_or_404(Post, slug=post_slug)
            text = comment_form.cleaned_data['text']

            # Get commenting permission for the specific post
            permissions = get_user_permissions(request)
            # Create comment slug
            comment_slug = orig_slug = slugify(text)[:SLUG_MAX_LENGTH].strip('-')
            for x in itertools.count(1):
                if not Comment.objects.filter(post=post, slug=comment_slug).exists():
                    break
                comment_slug = '%s-%d' % (orig_slug[:SLUG_MAX_LENGTH - len(str(x)) - 1], x)

            if permissions['can_comment']:
                comment = Comment.objects.create(
                    post=post,
                    user=request.user,
                    slug=comment_slug,
                    text=text
                )
                comment.save()
        else:
            print comment_form.errors['text'][0]
            post_slug = comment_form.cleaned_data['slug']

        return HttpResponseRedirect(
            reverse('content.views.post',
                    args=(),
                    kwargs={'slug': post_slug}))
    else:
        return redirect('/c/')
Example #9
0
def details_file_view(request, id, *args, **kwargs):
    context = base_auth(req=request)
    try:
        shared_file_obj = SharedFile.objects.filter(id=id,expiration_date__gte=datetime.datetime.now()).filter(Q(author=request.user) | Q(user_shared_file__user=request.user)).distinct().get()
    except:
        raise Http404
    comment_form = CommentForm()
    if shared_file_obj.author == request.user or shared_file_obj.user_shared_file_name.filter(user=request.user,permission_type=FILETYPES_CHOICES[2][0]):
        context['comment_form'] = comment_form
    context['page_title'] = shared_file_obj.title
    context['page_icon'] = "download"
    context['shared_file_obj'] = shared_file_obj



    return render(request, "pages/files/details.html", context)
Example #10
0
def comment_page(request, slug, page_slug, comment_id=None):
    page = get_object_or_404(Page, project__slug=slug, slug=page_slug)
    if not page.editable:
        return http.HttpResponseForbidden(_("You can't edit this page"))
    user = request.user.get_profile()
    reply_to = abs_reply_to = None
    if comment_id:
        reply_to = page.comments.get(pk=comment_id)
        abs_reply_to = reply_to
        while abs_reply_to.reply_to:
            abs_reply_to = abs_reply_to.reply_to
    preview = False
    comment = None
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.page = page
            comment.author = user
            comment.reply_to = reply_to
            comment.abs_reply_to = abs_reply_to
            if 'show_preview' in request.POST:
                preview = True
                comment.content = bleach.clean(
                    comment.content,
                    tags=settings.RICH_ALLOWED_TAGS,
                    attributes=settings.RICH_ALLOWED_ATTRIBUTES,
                    styles=settings.RICH_ALLOWED_STYLES,
                    strip=True)
            else:
                comment.save()
                messages.success(request, _('Comment posted!'))
                return http.HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request, _('Please correct errors bellow.'))
    else:
        form = CommentForm()
    return render_to_response('content/comment_page.html', {
        'form': form,
        'project': page.project,
        'page': page,
        'reply_to': reply_to,
        'comment': comment,
        'create': True,
        'preview': preview,
    },
                              context_instance=RequestContext(request))
Example #11
0
def edit_comment_sign_up(request, slug, comment_id):
    comment = get_object_or_404(PageComment, page__project__slug=slug, page__slug='sign-up', id=comment_id)
    if not comment.can_edit(request.user):
        return HttpResponseForbidden(_("You can't edit this comment"))
    abs_reply_to = comment
    while abs_reply_to.reply_to:
        abs_reply_to = abs_reply_to.reply_to
    if abs_reply_to == comment:
        abs_reply_to = reply_to = None
    else:
        reply_to = comment.reply_to

    if request.method == 'POST':
        form = CommentForm(request.POST, instance=comment)
        profile_form = ProfileEditForm(request.POST, instance=comment.author)
        profile_image_form = ProfileImageForm()
        if form.is_valid() and (reply_to or profile_form.is_valid()):
            if not reply_to:
                profile_form.save()
            form.save()
            success_msg = _('Comment updated!') if reply_to else _('Answer updated!')
            messages.success(request, success_msg)
            return HttpResponseRedirect(comment.get_absolute_url())
        else:
            error_msg = _('There was a problem updating your comment. Comments cannot be empty. ') if reply_to \
                        else _('There was a problem updating your answer. Answers cannot be empty. ')
            
            messages.error(request, error_msg)
    else:
        profile_form = ProfileEditForm(instance=comment.author)
        profile_image_form = ProfileImageForm()
        form = CommentForm(instance=comment)
    return render_to_response('content/comment_sign_up.html', {
        'profile_image_form': profile_image_form,
        'profile_form': profile_form,
        'profile': comment.author,
        'form': form,
        'project': comment.page.project,
        'page': comment.page,
        'reply_to': reply_to,
        'comment': comment,
    }, context_instance=RequestContext(request))
Example #12
0
def edit_comment(request, slug, page_slug, comment_id):
    comment = get_object_or_404(PageComment, id=comment_id, page__slug=page_slug, page__project__slug=slug)
    if not comment.can_edit(request.user):
        return HttpResponseForbidden(_("You can't edit this page"))
    if request.method == 'POST':
        form = CommentForm(request.POST, instance=comment)
        if form.is_valid():
            form.save()
            messages.success(request, _('Comment updated!'))
            return HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request,
                           _('There was a problem saving the comment.'))
    else:
        form = CommentForm(instance=comment)
    return render_to_response('content/comment_page.html', {
        'form': form,
        'comment': comment,
        'page': comment.page,
        'project': comment.page.project,
    }, context_instance=RequestContext(request))
Example #13
0
def edit_comment(request, slug, page_slug, comment_id):
    comment = get_object_or_404(PageComment,
                                id=comment_id,
                                page__slug=page_slug,
                                page__project__slug=slug)
    if not comment.can_edit(request.user):
        return http.HttpResponseForbidden(_("You can't edit this page"))
    preview = False
    if request.method == 'POST':
        form = CommentForm(request.POST, instance=comment)
        if form.is_valid():
            comment = form.save(commit=False)
            if 'show_preview' in request.POST:
                preview = True
                comment.content = bleach.clean(
                    comment.content,
                    tags=settings.RICH_ALLOWED_TAGS,
                    attributes=settings.RICH_ALLOWED_ATTRIBUTES,
                    styles=settings.RICH_ALLOWED_STYLES,
                    strip=True)
            else:
                comment.save()
                messages.success(request, _('Comment updated!'))
                return http.HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request, _('Please correct errors bellow.'))
    else:
        form = CommentForm(instance=comment)
    return render_to_response('content/comment_page.html', {
        'form': form,
        'comment': comment,
        'page': comment.page,
        'project': comment.page.project,
        'reply_to': comment.reply_to,
        'preview': preview,
    },
                              context_instance=RequestContext(request))
Example #14
0
def comment_sign_up(request, slug, comment_id=None):
    page = get_object_or_404(Page, project__slug=slug, slug='sign-up')
    project = page.project
    user = request.user.get_profile()
    is_organizing = project.organizers().filter(user=user).exists()
    is_participating = project.participants().filter(user=user).exists()
    reply_to = abs_reply_to = None
    if comment_id:
        reply_to = page.comments.get(pk=comment_id)
        abs_reply_to = reply_to
        while abs_reply_to.reply_to:
            abs_reply_to = abs_reply_to.reply_to
        if not is_organizing:
            if is_participating:
                if not project.is_participating(abs_reply_to.author.user):
                    return HttpResponseForbidden(_("You can't see this page"))
            elif abs_reply_to.author != user:
                return HttpResponseForbidden(_("You can't see this page"))
    elif project.signup_closed or is_organizing or is_participating:
        return HttpResponseForbidden(_("You can't see this page"))
    else:
        answers = page.comments.filter(reply_to__isnull=True, deleted=False,
            author=user)
        if answers.exists():
            return HttpResponseForbidden(_("There exists already an answer"))

    if request.method == 'POST':
        form = CommentForm(request.POST)
        profile_form = ProfileEditForm(request.POST, instance=user)
        profile_image_form = ProfileImageForm()
        if form.is_valid() and (reply_to or profile_form.is_valid()):
            if not reply_to:
                profile_form.save()
                new_rel = Relationship(source=user, target_project=project)
                try:
                    new_rel.save()
                except IntegrityError:
                    pass
            comment = form.save(commit=False)
            comment.page = page
            comment.author = user
            comment.reply_to = reply_to
            comment.abs_reply_to = abs_reply_to
            comment.save()
            success_msg = _('Reply posted!') if reply_to else _('Answer submitted!')
            messages.success(request, success_msg)
            return HttpResponseRedirect(comment.get_absolute_url())
        else:
            error_msg = _('There was a problem posting your reply. Reply cannot be empty. ') if reply_to \
                        else _('There was a problem submitting your answer. Answer cannot be empty. ')
            
            messages.error(request, error_msg)
    else:
        profile_form = ProfileEditForm(instance=user)
        profile_image_form = ProfileImageForm()
        form = CommentForm()
    return render_to_response('content/comment_sign_up.html', {
        'profile_image_form': profile_image_form,
        'profile_form': profile_form,
        'profile': user,
        'form': form,
        'project': project,
        'page': page,
        'reply_to': reply_to,
    }, context_instance=RequestContext(request))
Example #15
0
def comment_sign_up(request, slug, comment_id=None):
    page = get_object_or_404(Page, project__slug=slug, slug='sign-up')
    project = page.project
    profile = request.user.get_profile()
    is_organizing = project.organizers().filter(user=profile).exists()
    is_participating = project.participants().filter(user=profile).exists()
    reply_to = abs_reply_to = None
    if comment_id:
        reply_to = page.comments.get(pk=comment_id)
        abs_reply_to = reply_to
        while abs_reply_to.reply_to:
            abs_reply_to = abs_reply_to.reply_to
        if not is_organizing:
            if is_participating:
                if not project.is_participating(abs_reply_to.author.user):
                    return http.HttpResponseForbidden(
                        _("You can't see this page"))
            elif abs_reply_to.author != profile:
                return http.HttpResponseForbidden(_("You can't see this page"))
    elif project.signup_closed or is_organizing or is_participating:
        return http.HttpResponseForbidden(_("You can't see this page"))
    else:
        answers = page.comments.filter(reply_to__isnull=True, deleted=False,
            author=profile)
        if answers.exists():
            return http.HttpResponseForbidden(
                _("There exists already an answer"))
    preview = False
    comment = None
    if request.method == 'POST':
        form = CommentForm(request.POST)
        profile_form = ProfileEditForm(request.POST, instance=profile)
        profile_image_form = ProfileImageForm()
        if form.is_valid() and (reply_to or profile_form.is_valid()):
            if not reply_to:
                profile = profile_form.save()
            comment = form.save(commit=False)
            comment.page = page
            comment.author = profile
            comment.reply_to = reply_to
            comment.abs_reply_to = abs_reply_to
            if 'show_preview' in request.POST:
                preview = True
                comment.content = bleach.clean(comment.content,
                    tags=settings.RICH_ALLOWED_TAGS,
                    attributes=settings.RICH_ALLOWED_ATTRIBUTES,
                    styles=settings.RICH_ALLOWED_STYLES, strip=True)
                if not reply_to:
                    profile.bio = bleach.clean(profile.bio,
                        tags=settings.REDUCED_ALLOWED_TAGS,
                        attributes=settings.REDUCED_ALLOWED_ATTRIBUTES,
                        strip=True)
            else:
                if not reply_to:
                    profile.save()
                    new_rel, created = Relationship.objects.get_or_create(
                        source=profile, target_project=project)
                    new_rel.deleted = False
                    new_rel.save()
                comment.save()
                if reply_to:
                    success_msg = _('Reply posted!')
                else:
                    success_msg = _('Answer submitted!')
                messages.success(request, success_msg)
                return http.HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request, _('Please correct errors bellow.'))
    else:
        profile_form = ProfileEditForm(instance=profile)
        profile_image_form = ProfileImageForm()
        form = CommentForm()
    return render_to_response('content/comment_sign_up.html', {
        'profile_image_form': profile_image_form,
        'profile_form': profile_form,
        'profile': profile,
        'form': form,
        'project': project,
        'page': page,
        'reply_to': reply_to,
        'comment': comment,
        'create': True,
        'preview': preview,
    }, context_instance=RequestContext(request))
Example #16
0
def edit_comment_sign_up(request, slug, comment_id):
    comment = get_object_or_404(PageComment,
                                page__project__slug=slug,
                                page__slug='sign-up',
                                id=comment_id)
    if not comment.can_edit(request.user):
        return http.HttpResponseForbidden(_("You can't edit this comment"))
    abs_reply_to = comment
    while abs_reply_to.reply_to:
        abs_reply_to = abs_reply_to.reply_to
    if abs_reply_to == comment:
        abs_reply_to = reply_to = None
    else:
        reply_to = comment.reply_to
    preview = False
    profile = comment.author
    if request.method == 'POST':
        form = CommentForm(request.POST, instance=comment)
        profile_form = ProfileEditForm(request.POST, instance=profile)
        profile_image_form = ProfileImageForm()
        if form.is_valid() and (reply_to or profile_form.is_valid()):
            if not reply_to:
                profile = profile_form.save(commit=False)
            comment = form.save(commit=False)
            if 'show_preview' in request.POST:
                preview = True
                comment.content = bleach.clean(
                    comment.content,
                    tags=settings.RICH_ALLOWED_TAGS,
                    attributes=settings.RICH_ALLOWED_ATTRIBUTES,
                    styles=settings.RICH_ALLOWED_STYLES,
                    strip=True)
                if not reply_to:
                    profile.bio = bleach.clean(
                        profile.bio,
                        tags=settings.REDUCED_ALLOWED_TAGS,
                        attributes=settings.REDUCED_ALLOWED_ATTRIBUTES,
                        strip=True)
            else:
                if not reply_to:
                    profile.save()
                comment.save()
                if reply_to:
                    success_msg = _('Comment updated!')
                else:
                    success_msg = _('Answer updated!')
                messages.success(request, success_msg)
                return http.HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request, _('Please correct errors bellow.'))
    else:
        profile_form = ProfileEditForm(instance=comment.author)
        profile_image_form = ProfileImageForm()
        form = CommentForm(instance=comment)
    return render_to_response('content/comment_sign_up.html', {
        'profile_image_form': profile_image_form,
        'profile_form': profile_form,
        'profile': profile,
        'form': form,
        'project': comment.page.project,
        'page': comment.page,
        'reply_to': reply_to,
        'comment': comment,
        'preview': preview,
    },
                              context_instance=RequestContext(request))
Example #17
0
def comment_sign_up(request, slug, comment_id=None):
    page = get_object_or_404(Page, project__slug=slug, slug='sign-up')
    project = page.project
    profile = request.user.get_profile()
    is_organizing = project.organizers().filter(user=profile).exists()
    is_participating = project.participants().filter(user=profile).exists()
    reply_to = abs_reply_to = None
    if comment_id:
        reply_to = page.comments.get(pk=comment_id)
        abs_reply_to = reply_to
        while abs_reply_to.reply_to:
            abs_reply_to = abs_reply_to.reply_to
        if not is_organizing:
            if is_participating:
                if not project.is_participating(abs_reply_to.author.user):
                    return http.HttpResponseForbidden(
                        _("You can't see this page"))
            elif abs_reply_to.author != profile:
                return http.HttpResponseForbidden(_("You can't see this page"))
    elif project.signup_closed or is_organizing or is_participating:
        return http.HttpResponseForbidden(_("You can't see this page"))
    else:
        answers = page.comments.filter(reply_to__isnull=True,
                                       deleted=False,
                                       author=profile)
        if answers.exists():
            return http.HttpResponseForbidden(
                _("There exists already an answer"))
    preview = False
    comment = None
    if request.method == 'POST':
        form = CommentForm(request.POST)
        profile_form = ProfileEditForm(request.POST, instance=profile)
        profile_image_form = ProfileImageForm()
        if form.is_valid() and (reply_to or profile_form.is_valid()):
            if not reply_to:
                profile = profile_form.save()
            comment = form.save(commit=False)
            comment.page = page
            comment.author = profile
            comment.reply_to = reply_to
            comment.abs_reply_to = abs_reply_to
            if 'show_preview' in request.POST:
                preview = True
                comment.content = bleach.clean(
                    comment.content,
                    tags=settings.RICH_ALLOWED_TAGS,
                    attributes=settings.RICH_ALLOWED_ATTRIBUTES,
                    styles=settings.RICH_ALLOWED_STYLES,
                    strip=True)
                if not reply_to:
                    profile.bio = bleach.clean(
                        profile.bio,
                        tags=settings.REDUCED_ALLOWED_TAGS,
                        attributes=settings.REDUCED_ALLOWED_ATTRIBUTES,
                        strip=True)
            else:
                if not reply_to:
                    profile.save()
                    new_rel, created = Relationship.objects.get_or_create(
                        source=profile, target_project=project)
                    new_rel.deleted = False
                    new_rel.save()
                comment.save()
                if reply_to:
                    success_msg = _('Reply posted!')
                else:
                    success_msg = _('Answer submitted!')
                messages.success(request, success_msg)
                return http.HttpResponseRedirect(comment.get_absolute_url())
        else:
            messages.error(request, _('Please correct errors bellow.'))
    else:
        profile_form = ProfileEditForm(instance=profile)
        profile_image_form = ProfileImageForm()
        form = CommentForm()
    return render_to_response('content/comment_sign_up.html', {
        'profile_image_form': profile_image_form,
        'profile_form': profile_form,
        'profile': profile,
        'form': form,
        'project': project,
        'page': page,
        'reply_to': reply_to,
        'comment': comment,
        'create': True,
        'preview': preview,
    },
                              context_instance=RequestContext(request))
Example #18
0
def comment_sign_up(request, slug, comment_id=None):
    page = get_object_or_404(Page, project__slug=slug, slug='sign-up')
    project = page.project
    user = request.user.get_profile()
    reply_to = abs_reply_to = None
    if comment_id:
        reply_to = page.comments.get(pk=comment_id)
        abs_reply_to = reply_to
        while abs_reply_to.reply_to:
            abs_reply_to = abs_reply_to.reply_to
    elif project.signup_closed:
        return HttpResponseForbidden()

    if user != project.created_by:
        participants = project.participants()
        author = abs_reply_to.author if abs_reply_to else user
        if participants.filter(user=user).exists():
            if author != project.created_by and not participants.filter(user=author).exists():
                return HttpResponseForbidden()
        elif author != user:
            return HttpResponseForbidden()

    if request.method == 'POST':
        form = CommentForm(request.POST)
        profile_form = ProfileEditForm(request.POST, instance=user)
        profile_image_form = ProfileImageForm()
        if form.is_valid() and (reply_to or profile_form.is_valid()):
            if not reply_to:
                profile_form.save()
                new_rel = Relationship(source=user, target_project=project)
                try:
                    new_rel.save()
                except IntegrityError:
                    pass
            comment = form.save(commit=False)
            comment.page = page
            comment.author = user
            comment.reply_to = reply_to
            comment.abs_reply_to = abs_reply_to
            comment.save()
            success_msg = _('Reply posted!') if reply_to else _('Answer submitted!')
            messages.success(request, success_msg)
            return HttpResponseRedirect(reverse('page_show', kwargs={
                'slug': slug,
                'page_slug': page.slug,
            }))
        else:
            error_msg = _('There was a problem posting your reply. Reply cannot be empty. ') if reply_to \
                        else _('There was a problem submitting your answer. Answer cannot be empty. ')
            
            messages.error(request, error_msg)
    else:
        profile_form = ProfileEditForm(instance=user)
        profile_image_form = ProfileImageForm()
        form = CommentForm()
    return render_to_response('content/comment_sign_up.html', {
        'profile_image_form': profile_image_form,
        'profile_form': profile_form,
        'profile': user,
        'form': form,
        'project': project,
        'page': page,
        'reply_to': reply_to,
    }, context_instance=RequestContext(request))