def edit_comment(request, thread_id, comment_id=None): if comment_id: comment = get_object_or_404(Comment, pk=comment_id) else: comment = Comment() comment.thread_id = thread_id if request.method == 'POST': # comment_form = CommentForm(request.POST, instance=comment) # v = comment_form.is_valid() # if v: # # スパム襲来のため投稿編集を禁止 # # comment_form.save() # pass # # # validationがtrueならjsonで結果を返す # ajax = request.GET.get('use-ajax', None) # if ajax != 'true': # # ajaxでない # if v: # # validならリダイレクト # return http.HttpResponseRedirect('/bbs/%s' % comment.thread_id) # else: # # invalidなら下で処理 # context = request.POST # else: # # ajaxなときvalid,invalid問わず # content = dict(result=v, errors=comment_form.errors, redirect_to=reverse( # 'bbs.thread.show', args=(comment.thread.id, ))) # return http.HttpResponse(json.dumps(content), mimetype='text/plain') return http.HttpResponse(content='', content_type='text/plain', status=400) else: # postでないajaxでない(普通のアクセス) context = {} comment_form = CommentForm(instance=comment, initial=dict(edit_key='')) return render_to_response('bbs/edit_comment.html', dict( comment=comment, comment_form=comment_form, ), context_instance=RequestContext(request, context))
def show_thread(request, thread_id): thread = get_object_or_404(Thread, pk=thread_id) if thread.locked: if not request.user.is_active: return http.HttpResponseRedirect('/auth/login?next=%s' % request.path) if request.method == 'POST': comment = Comment() comment.thread_id = thread_id comment_form = CommentForm(request.POST, instance=comment) v = comment_form.is_valid() if v: thread = comment_form.save() ajax = request.GET.get('use-ajax', None) if ajax != 'true': if v: return http.HttpResponseRedirect(reverse('bbs.thread.show', args=(thread_id,))) else: context = request.POST else: content = dict(result=v, errors=comment_form.errors, redirect_to=reverse( 'bbs.thread.show', args=(thread_id,))) return http.HttpResponse(json.dumps(content), mimetype='text/plain') else: context = {} comment_form = CommentForm() return render( request, 'bbs/show_thread.html', dict( thread=thread, comments=thread.comment_set.all(), comment_form=comment_form, delete_form=DeleteForm() ), context_instance=RequestContext(request, context) )