def edit_image(request): profile = get_profile(request) if request.method == "GET": c = {"profile":profile} update_context(request, c) return my_render(request, 'profile/profile_edit_image.html', c) elif request.method == "POST": form = ImageUploadForm(request.POST, request.FILES) im_path = request.POST.get("im_path") if form.is_valid(): profile.image = form.cleaned_data['image'] elif im_path: x1 = request.POST.get("x1") y1 = request.POST.get("y1") x2 = request.POST.get("x2") y2 = request.POST.get("y2") img = Image.open(im_path) cropped = img.crop(map(int, [x1,y1,x2,y2])) t = os.path.splitext(profile.image.path) crop_image = t[0]+"_crop"+t[1] cropped.save(crop_image) profile.image.save(profile.image.path, File(open(crop_image, 'rb'))) profile.save() cache_clear() # FIXME request.session["ok_message"] = u"프로필 이미지가 수정되었습니다" return redirect('scode.profile_views.edit_image')
def recommend(request, q_id): profile = get_profile(request) try: question = Question.objects.get(id=q_id) except Question.DoesNotExist: request.session["error_message"] = u"해당 글을 찾을 수 없습니다." return redirect('/') if request.method == "GET": c = {"question": question} update_context(request, c) if profile == question.profile: request.session["error_message"] = u"본인이 작성한 글은 추천할 수 없습니다." return redirect(question.get_absolute_url()) if profile in question.recommender.all(): request.session["error_message"] = u"이미 추천하셨습니다" return redirect(question.get_absolute_url()) return my_render(request, 'scode/scode_recommend.html', c) elif request.method == "POST": if profile not in question.recommender.all(): question.recommender.add(profile) request.session["ok_message"] = u"이 글을 추천하였습니다" cache_remove(question.id) return redirect(question.get_absolute_url())
def feedback(request): if request.method == "GET": c = {} update_context(request, c) return my_render(request, 'help/feedback.html', c) elif request.method == "POST": content = request.POST.get("content", "") if not content: request.session["error_message"] = u"피드백 내용을 입력 해 주세요." return redirect('/help/feedback') profile = get_profile(request) if profile: content = u'"%s (%s)" 님의 피드백.' % (profile.name, profile.email) \ + "\n\n" + content # send mail send_mail(u"코딩도장 피드백이 도착했습니다.", content, settings.DEFAULT_FROM_EMAIL, [settings.ADMINS[0][1]]) request.session["ok_message"] = u"감사합니다. 피드백이 전송되었습니다." return redirect('/help/feedback')
def comment_delete(request, c_id): profile = get_profile(request) comment = Comment.objects.get(id=c_id) if comment.profile == profile: pass elif profile.user.is_staff: pass elif profile.belt >= profile.BELT_BLACK: pass else: q_id = comment.get_question().id request.session["error_message"] = u"댓글을 삭제할 수 있는 권한이 없습니다" return redirect('/scode/%s' % q_id) if request.method == "GET": c = {"comment": comment} update_context(request, c) return my_render(request, 'scode/comment_delete.html', c) elif request.method == "POST": answer = comment.get_answer() question = comment.get_question() comment.delete() request.session["ok_message"] = u"댓글이 삭제되었습니다" cache_remove(question.id) if answer: return redirect(answer.get_absolute_url()) else: return redirect('/scode/%s' % question.id)
def comment_not_recommend(request, c_id): profile = get_profile(request) try: comment = Comment.objects.get(id=c_id) except Comment.DoesNotExist: request.session["error_message"] = u"댓글을 찾을 수 없습니다." return redirect('/') question = comment.get_question() if profile == comment.profile: request.session["error_message"] = u"본인이 작성한 댓글은 비추천할 수 없습니다." return redirect('/scode/%s' % question.id) if profile in comment.recommender.all() or profile in comment.not_recommender.all(): request.session["error_message"] = u"이미 추천 또는 비추천하셨습니다" return redirect('/scode/%s' % question.id) if request.method == "GET": c = {"comment": comment} update_context(request, c) return my_render(request, 'scode/comment_not_recommend.html', c) elif request.method == "POST": if profile not in comment.not_recommender.all(): comment.not_recommender.add(profile) request.session["ok_message"] = u"댓글을 비추천하였습니다" cache_remove(question.id) return redirect(comment.get_absolute_url())
def subscribe(request, q_id): profile = get_profile(request) try: question = Question.objects.get(id=q_id) except Question.DoesNotExist: request.session["error_message"] = u"해당 글을 찾을 수 없습니다." return redirect('/') if request.method == "GET": if profile == question.profile: request.session["error_message"] = u"본인이 작성한 글은 구독할 수 없습니다." return redirect(question.get_absolute_url()) c = {"question": question, "profile": profile, } update_context(request, c) return my_render(request, 'scode/scode_subscribe.html', c) elif request.method == "POST": _type = request.POST.get("_type", "") if _type == "register": if profile not in question.subscriber.all(): question.subscriber.add(profile) request.session["ok_message"] = u"구독 신청이 완료되었습니다." elif _type == "cancel": if profile in question.subscriber.all(): question.subscriber.remove(profile) request.session["ok_message"] = u"구독 신청이 취소되었습니다." cache_remove(question.id, extra=False) return redirect('/scode/subscribe/%s' % question.id)
def comment_edit(request, c_id): profile = get_profile(request) if request.method == "GET": comment = Comment.objects.get(id=c_id) if comment.profile == profile: pass elif profile.user.is_staff: pass elif profile.belt >= Profile.BELT_BLACK: pass else: q_id = comment.get_question().id request.session["error_message"] = u"댓글을 수정할 수 있는 권한이 없습니다" return redirect('/scode/%s' % q_id) c = {"comment": comment} update_context(request, c) return my_render(request, 'scode/comment_edit.html', c) elif request.method == "POST": comment = Comment.objects.get(id=c_id) content = unicode.strip(request.POST.get("content", "")) comment.content = content comment.modify_time = datetime.datetime.today() comment.save() question = comment.get_question() question.save() request.session["ok_message"] = u"댓글이 수정되었습니다" cache_remove(question.id) return redirect(comment.get_absolute_url())
def edit_base(request): profile = get_profile(request) if request.method == "GET": c = {"profile":profile} update_context(request, c) return my_render(request, 'profile/profile_edit_base.html', c) elif request.method == "POST": profile_name = request.POST.get("name", "") profile_summary = request.POST.get("summary", "") profile_computer = request.POST.get("computer", "") profile_editor = request.POST.get("editor", "") if profile_name: profile.name = profile_name profile.summary = profile_summary profile.computer = profile_computer profile.editor = profile_editor profile.save() cache_clear() # FIXME request.session["ok_message"] = u"기본정보가 수정되었습니다" else: request.session["error_message"] = u"이름을 입력 해 주세요" return redirect('scode.profile_views.edit_base')
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 answer_delete(request, a_id): profile = get_profile(request) answer = Answer.objects.get(id=a_id) if answer.profile != profile and not profile.user.is_staff: 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_delete.html', c) elif request.method == "POST": question = answer.get_question() answer.comments.all().delete() answer.delete() request.session["ok_message"] = u"풀이가 삭제되었습니다" cache_remove(question.id) return redirect('/scode/%s' % question.id)
def delete(request, q_id): profile = get_profile(request) question = Question.objects.get(id=q_id) if question.profile != profile and not profile.user.is_staff: request.session["error_message"] = u"글을 삭제할 수 있는 권한이 없습니다" return redirect('/scode/%s' % q_id) if request.method == "GET": c = {"question": question} update_context(request, c) return my_render(request, 'scode/scode_delete.html', c) elif request.method == "POST": question.comments.all().delete() for answer in question.answers.all(): answer.comments.all().delete() answer.delete() question.delete() request.session["ok_message"] = "글이 삭제되었습니다" cache_remove(question.id) return redirect('scode.scode_views.index')
def edit_password(request): profile = get_profile(request) if request.method == "GET": c = {"profile":profile} update_context(request, c) return my_render(request, 'profile/profile_edit_password.html', c) elif request.method == "POST": cpasswd = request.POST["cpasswd"] npasswd = request.POST["npasswd"] u = User.objects.get(username=profile.email) if not cpasswd or not npasswd: request.session["error_message"] = u"입력값을 확인하세요" elif not u.check_password(cpasswd): request.session["error_message"] = u"기존 비밀번호가 틀리게 입력되었습니다" else: u.set_password(npasswd) u.save() request.session["ok_message"] = u"비밀번호가 변경되었습니다" return redirect('scode.profile_views.edit_password')
def answer_recommend(request, a_id): profile = get_profile(request) try: answer = Answer.objects.get(id=a_id) except Answer.DoesNotExist: request.session["error_message"] = u"풀이를 찾을 수 없습니다." return redirect('/') # # check auth # question = answer.get_question() if profile == answer.profile: request.session["error_message"] = u"본인이 작성한 글은 추천할 수 없습니다." return redirect('/scode/%s' % question.id) if profile in answer.recommender.all(): request.session["error_message"] = u"이미 추천하셨습니다" return redirect('/scode/%s' % question.id) # # 스태프, 검은띠는 Multi vote 가능 # if profile.belt != Profile.BELT_BLACK and not profile.user.is_staff: for _a in question.answers.all(): if profile in _a.recommender.all(): request.session["error_message"] = u"이미 추천하셨습니다" return redirect('/scode/%s' % question.id) if request.method == "GET": c = {"answer": answer} update_context(request, c) return my_render(request, 'scode/answer_recommend.html', c) elif request.method == "POST": answer.recommender.add(profile) 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 index(request): profile = get_profile(request) return redirect(profile.get_absolute_url())
def comment_save(request, question_or_answer, q_id, a_id): profile = get_profile(request) if profile.belt < Profile.BELT_YELLOW and not profile.user.is_staff: request.session["error_message"] = u"댓글은 노란 띠 이상부터 작성가능합니다." return redirect('/scode/%s' % q_id) content = request.POST.get("content", "") if not content: request.session["error_message"] = u"댓글의 내용을 입력하세요" if a_id: answer = Answer.objects.get(id=a_id) return redirect(answer.get_absolute_url()) else: return redirect('/scode/%s' % q_id) content = unicode.strip(content) emails = [] comment = Comment() comment.profile = profile comment.content = content comment.save() if question_or_answer == "question": question = Question.objects.get(id=q_id) question.comments.add(comment) # question.modify_time = datetime.datetime.today() question.save() elif question_or_answer == "answer": answer = Answer.objects.get(id=a_id) answer.comments.add(comment) # answer.modify_time = datetime.datetime.today() answer.save() question = answer.get_question() 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": comment.time.strftime(u"%Y-%m-%d %X"), "content": content, "subject": question.subject, "url": comment.get_absolute_url(), } if question_or_answer == "question": obj = question elif question_or_answer == "answer": obj = answer if obj.profile != profile: emails.append(obj.profile.email) # 댓글에 이름 언급 시 이메일 송신 for c in obj.comments.all(): if content.find(c.profile.name) != -1 \ and c.profile != profile and c.profile.email not in emails: emails.append(c.profile.email) if emails: send_mail(u"[%s] 글에 댓글이 작성되었습니다." % (question.subject), body, settings.DEFAULT_FROM_EMAIL, emails) cache_remove(question.id) return redirect(comment.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)
def level(request, q_id): profile = get_profile(request) try: question = Question.objects.get(id=q_id) except Question.DoesNotExist: request.session["error_message"] = u"해당 글을 찾을 수 없습니다." return redirect('/') if request.method == "GET": levels = [0] * 5 total = 0 all_levels = question.level.all() for lv in all_levels: total += 1 levels[lv.level - 1] += 1 levels_count = levels[:] if total: for i in range(len(levels)): levels[i] = round(float(levels[i]) / float(total) * 100, 2) questionLevel = question.level.filter(profile=profile) if questionLevel: currentLevel = questionLevel[0].level else: currentLevel = 3 questionLevel = question.getLevel() level_label = get_level_label(questionLevel) c = {"question": question, "currentLevel": currentLevel, "questionLevel": questionLevel, "level_label": level_label, "levels_count": levels_count, "levels": levels} update_context(request, c) return my_render(request, 'scode/scode_level.html', c) elif request.method == "POST": if not request.user.is_authenticated(): request.session["error_message"] = u"난이도 투표는 로그인이 필요합니다." return redirect('/scode/level/%s' % question.id) if profile.belt < Profile.BELT_BLUE and not profile.user.is_staff: request.session["error_message"] = u"난이도 투표는 파란 띠 이상부터 가능합니다" return redirect('/scode/level/%s' % question.id) level = request.POST.get("level", "") if question.level.filter(profile=profile): questionLevel = question.level.get(profile=profile) questionLevel.level = int(level) questionLevel.save() #request.session["error_message"] = u"이미 난이도 투표를 했습니다." #return redirect('/scode/level/%s' % question.id) else: questionLevel = QuestionLevel(profile=profile, level=int(level)) questionLevel.save() question.level.add(questionLevel) question.strlevel = question._getLevel() question.save() request.session["ok_message"] = u"난이도 투표를 했습니다." cache_remove(question.id) return redirect('/scode/level/%s' % question.id)