def make_question_tags(question, tag_string): for tag in question.tags.all(): question.tags.remove(tag) tags = map(unicode.strip, tag_string.split(",")) for tag in tags: if not tag: continue try: tag = Tag.objects.get(name=tag) except Tag.DoesNotExist: tag = Tag(name=tag) tag.save() question.tags.add(tag)
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."