コード例 #1
0
ファイル: scode_views.py プロジェクト: pahkey/codingdojo
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())
コード例 #2
0
ファイル: migration.py プロジェクト: pahkey/codingdojo
def run():
    old_questions = _Question.objects.using("old").all()
    for old_question in old_questions:

        print old_question.subject, "start.."

        old_email = old_question.profile.email
        profile = Profile.objects.get(email=old_email)

        recommend_count = old_question.up.count()

        # make question
        q = Question()
        q.profile = profile
        q.subject = old_question.subject
        q.content = old_question.content
        q.register_time = old_question.register_time
        q.modify_time = old_question.modify_time
        q.save()

        for i in range(recommend_count):
            ip = get_ip(str(i))
            q.recommend.add(ip)


        print "make question end."

        # make tag
        for codetag in old_question.codetag.all():
            tag =codetag.name
            try:
                tag = Tag.objects.get(name=tag)
            except Tag.DoesNotExist:
                tag = Tag(name=tag)
                tag.save()

            q.tags.add(tag)

        print "make tag end."

        # make question comment.
        for o_comment in old_question.comment.all():
            c_content = o_comment.content
            c_email = o_comment.profile.email
            c_profile = Profile.objects.get(email=c_email)

            comment = Comment()
            comment.profile = c_profile
            comment.content = c_content
            comment.time = o_comment.time
            comment.save()
            q.comments.add(comment)

        print "make question comment end."

        # make answer
        for answer in old_question.answer.all():
            c_content = answer.content
            c_email = answer.profile.email
            c_profile = Profile.objects.get(email=c_email)
            answer_recommend_count = answer.up.count()

            new_answer = Answer()
            new_answer.profile = c_profile
            new_answer.content = c_content
            new_answer.register_time = answer.register_time
            new_answer.modify_time = answer.modify_time
            new_answer.save()

            for i in range(answer_recommend_count):
                ip = get_ip(str(i))
                new_answer.recommend.add(ip)

            q.answers.add(new_answer)
            # parent = comment

             # make answer comment.
            for o_comment in answer.comment.all():
                a_content = o_comment.content
                a_email = o_comment.profile.email
                a_profile = Profile.objects.get(email=a_email)

                comment = Comment()
                comment.profile = a_profile
                comment.content = a_content
                comment.time = o_comment.time
                comment.save()

                new_answer.comments.add(comment)

                # q.comments.add(comment)


        print old_question.subject, "end."