Beispiel #1
0
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())
Beispiel #2
0
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."