Esempio n. 1
0
    def from_user(user, n_questions=settings.QUESTIONS_PER_SET):
        """
        Generate a new test set for this user using their profile to determine
        the appropriate syllabus.
        """
        set_type, plugin_set = TestSet._get_plugin_set(user)
        test_set = TestSet(user=user, random_seed=random.randrange(0, 2**30),
                set_type=set_type)
        test_set.save()

        from kanji_test.drill import load_plugins
        from kanji_test.drill.plugin_api import UnsupportedItem
        question_plugins = load_plugins(plugin_set)
        items = user.get_profile().syllabus.get_random_items(n_questions)
        questions = []
        for item in items:
            has_kanji = item.has_kanji()
            available_plugins = [p for p in question_plugins if \
                    p.requires_kanji == has_kanji]
            question = None
            while question == None and available_plugins:
                i = random.randrange(len(available_plugins))
                chosen_plugin = available_plugins[i]
                try:
                    question = chosen_plugin.get_question(item, user)
                    questions.append(question)
                except UnsupportedItem:
                    # Oh well, try again with another plugin
                    del available_plugins[i]

        test_set.questions = questions
        return test_set
    def forwards(self, orm):
        "Write your forwards migration here"
        renames = {}
        for plugin in load_plugins():
            old_name = str(plugin.__class__.__name__)
            renames[old_name] = plugin.verbose_name

        for question_plugin in QuestionPlugin.objects.all():
            if question_plugin.name in renames:
                new_name = renames[question_plugin.name]
                print "%s -> %s" % (question_plugin.name, new_name)
                try:
                    existing_rename = QuestionPlugin.objects.get(name=new_name)
                    question_plugin.question_set.update(
                            question_plugin=existing_rename)
                    question_plugin.delete()
                except ObjectDoesNotExist:
                    question_plugin.name = new_name
                    question_plugin.save()