def new_comment(request, pk): proposal = get_object_or_404(Proposal, pk=pk) # redirect if comments are not allowed if not proposal.theme.track.allow_comments: return redirect('review:proposal', pk=proposal.pk) # if valid post... if request.method == "POST": form = EditCommentForm(request.POST) if form.is_valid(): # save the comment comment = form.save(commit=False) comment.proposal = proposal comment.created_by = request.user comment.save() return redirect('review:proposal', pk=proposal.pk) else: #show errors messages.error(request, 'Please correct the errors below.') else: form = EditCommentForm() return render(request, 'review/comments/new_comment.html', { 'proposal': proposal, 'form': form })
def edit_comment(request): if request.method == 'GET': comment_id = request.GET['comment_id'] comment = Comment.objects.get(pk=comment_id) start = comment.start end = comment.end try: similar_comment = comment.similar_comment.id except: similar_comment = -1 form = EditCommentForm(initial={ 'text': comment.text, 'comment_id': comment.id, 'similar_comment': similar_comment, }) chunk = Chunk.objects.get(pk=comment.chunk.id) markLogStart(request.user, { 'type': 'edit comment', 'text': comment.text, 'comment_id': comment.id, 'similar_comment': similar_comment, }) return render(request, 'review/edit_comment_form.html', { 'form': form, 'start': start, 'end': end, 'snippet': chunk.generate_snippet(start, end), 'chunk': chunk, 'comment_id': comment.id, 'reply': comment.is_reply(), }) else: form = EditCommentForm(request.POST) if form.is_valid(): comment_id = form.cleaned_data['comment_id'] comment = Comment.objects.get(id=comment_id) comment.text = form.cleaned_data['text'] comment.edited = datetime.datetime.now() try: comment.similar_comment = find_similar_comment(form.cleaned_data['similar_comment'], form.cleaned_data['text']) except: comment.similar_comment = None comment.save() chunk = comment.chunk return render(request, 'review/comment.html', { 'comment': comment, 'chunk': chunk, 'snippet': chunk.generate_snippet(comment.start, comment.end), 'full_view': True, 'file': chunk.file, })