Example #1
0
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)
Example #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."