def answer_edit(request, a_id): profile = get_profile(request) answer = Answer.objects.get(id=a_id) # # 수정권한 체크 # if answer.profile == profile: pass elif profile.user.is_staff: pass elif profile.belt >= Profile.BELT_RED: # 빨간띠 이상 pass else: q_id = answer.get_question().id request.session["error_message"] = u"풀이를 수정할 수 있는 권한이 없습니다" return redirect('/scode/%s' % q_id) if request.method == "GET": c = {"answer": answer} update_context(request, c) return my_render(request, 'scode/answer_edit.html', c) elif request.method == "POST": content = unicode.strip(request.POST.get("content", "")) prev_content = answer.content answer.content = content answer.modify_time = datetime.datetime.today() answer.save() if content != prev_content: # make revision revision = Revision(profile=profile, content=content) revision.save() answer.revisions.add(revision) question = answer.get_question() # # 풀이 수정 시 문제의 modify_time 변경, 최근목록으로 표시되기 위해 # # question.modify_time = datetime.datetime.today() # question.save() request.session["ok_message"] = u"풀이가 수정되었습니다" cache_remove(question.id) return redirect(answer.get_absolute_url())
def tag_edit(request, tagid): profile = get_profile(request) # # check authority # if profile.user.is_staff: pass elif profile.belt >= profile.BELT_BLUE: pass else: request.session["error_message"] = u"알고리즘 수정은 파란 띠 이상부터 가능합니다" return redirect('/scode/tag/%s' % tagid) if request.method == "GET": tag = Tag.objects.get(id=tagid) c = {"tag": tag} update_context(request, c) return my_render(request, 'scode/tag_edit.html', c) elif request.method == "POST": tag = Tag.objects.get(id=tagid) prev_content = tag.content content = unicode.strip(request.POST.get("content", "")) tag.content = content tag.modify_time = datetime.datetime.today() tag.save() if content != prev_content: # make revision revision = Revision(profile=profile, content=content) revision.save() tag.revisions.add(revision) request.session["ok_message"] = u"알고리즘이 수정되었습니다" return redirect(tag.get_absolute_url())
def answer_save(request, q_id): question = Question.objects.get(id=q_id) content = request.POST.get("content", "") if not content: request.session["error_message"] = u"풀이의 내용을 입력하세요" return redirect(question.get_absolute_url()) content = unicode.strip(content) profile = get_profile(request) # # 풀이작성 시 알림메일 발송 대상 # emails = [] answer = Answer() answer.profile = profile answer.content = content answer.save() revision = Revision(profile=profile, content=content) revision.save() answer.revisions.add(revision) question.answers.add(answer) question.modify_time = datetime.datetime.today() question.save() request.session["ok_message"] = u"풀이가 작성되었습니다" body = u"""%(user)s 님의 풀이 (작성일시: %(create_time)s) ----------------------------------------------------------------------------- %(content)s ----------------------------------------------------------------------------- %(subject)s - http://codingdojang.com%(url)s """ % { "user": profile.name, "create_time": answer.register_time.strftime(u"%Y-%m-%d %X"), "content": content, "subject": question.subject, "url": answer.get_absolute_url(), } if question.profile != profile: emails.append(question.profile.email) # # subscriber: 질문 구독자에게도 답글 알림메일을 발송 # for subscriber in question.subscriber.all(): if subscriber != profile: emails.append(subscriber.email) send_mail(u"[%s] 글에 풀이가 작성되었습니다." % (question.subject), body, settings.DEFAULT_FROM_EMAIL, emails) cache_remove(question.id) return redirect(answer.get_absolute_url())
def edit(request, q_id=None): profile = get_profile(request) # # edit form # if request.method == "GET": c = {} if q_id: # edit post question = Question.objects.get(id=q_id) # # check edit authority # if question.profile == profile: pass elif profile.user.is_staff: pass elif profile.belt >= Profile.BELT_RED: # 빨간띠 이상 pass else: request.session["error_message"] = u"글을 수정할 수 있는 권한이 없습니다" return redirect('/scode/%s' % q_id) c.update({"question": question}) else: # create new post # # check new post authority # if profile.belt < Profile.BELT_BLUE and not profile.user.is_staff: request.session["error_message"] = u"문제 작성은 파란 띠 이상부터 가능합니다" return redirect('/scode/list') update_context(request, c) return my_render(request, 'scode/scode_edit.html', c) # # save form # elif request.method == "POST": q_id = request.POST.get("q_id", "") subject = unicode.strip(request.POST.get("subject", "")) tags = unicode.strip(request.POST.get("tags", "")) content = unicode.strip(request.POST.get("content", "")) # # prev_content needed for revisions. # prev_content = "" if not subject or not content: request.session["error_message"] = u"제목 또는 내용을 입력 해 주세요" return redirect('scode.scode_views.edit') else: if q_id: # edit post q = Question.objects.get(id=q_id) prev_content = q.content # # check authority for save # if q.profile == profile: pass elif profile.user.is_staff: pass elif profile.belt >= Profile.BELT_RED: # 빨간띠 이상 pass else: request.session["error_message"] = u"글을 수정할 수 있는 권한이 없습니다" return redirect('/scode/%s' % q_id) ok_message = u"글이 수정되었습니다" else: # # check new post authority # if profile.belt < Profile.BELT_BLUE and not profile.user.is_staff: request.session["error_message"] = u"글을 작성할 수 있는 권한이 없습니다" return redirect('/scode/list') q = Question() q.profile = profile ok_message = u"새로운 글이 작성되었습니다" q.subject = subject q.content = content q.category = 'Q' q.save() # # make revision # if content != prev_content: revision = Revision(profile=profile, content=content) revision.save() q.revisions.add(revision) make_question_tags(q, tags) request.session["ok_message"] = ok_message # # 캐시 삭제 # cache_remove(q.id) # # 연관 태그 있는 글의 캐시도 모두 삭제한다. # similar_questions = Question.objects.filter(tags__in=q.tags.all(), category='Q').distinct() for sq in similar_questions: cache_remove(sq.id) return redirect('/scode/%s' % q.id)