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))
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))
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))
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))
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))
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))
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))
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))
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/')
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))
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))
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))
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))
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))
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))