コード例 #1
0
def new_quiz(request):
    """
    Create a new quiz but doesn't create any 'card' models.
    Card models will only be used for cardbox in future.
    """
    body = json.loads(request.body)
    logger.debug(body)
    lexicon = Lexicon.objects.get(lexiconName=body['lexicon'])
    try:
        search_description = build_search_criteria(request.user, lexicon,
                                                   body['searchCriteria'])
    except GameInitException as e:
        return response(str(e), status=400)

    questions = word_search(search_description, expand=True)
    if questions.size() == 0:
        return response('No questions were found.', status=400)
    wl = WordList()
    wl.initialize_list(list(questions.to_python()),
                       lexicon,
                       None,
                       shuffle=True,
                       save=False)
    q_map = generate_question_map(questions)
    quiz_name = temporary_list_name(search_description, lexicon.lexiconName)
    # XXX add 1000-question limit?
    return response({
        'list': wl.to_python(),
        'q_map': q_map,
        'quiz_name': quiz_name,
    })
コード例 #2
0
ファイル: game.py プロジェクト: adrianiainlam/Webolith
    def initialize_by_named_list(
        self,
        lex,
        user,
        named_list,
        secs,
        questions_per_round=None,
        use_table=None,
        multiplayer=None,
    ):
        qs = json.loads(named_list.questions)
        if named_list.isRange:
            questions = questions_from_probability_range(
                lex, qs[0], qs[1], named_list.wordLength)
            wl = self.initialize_word_list(questions, lex, user)
        else:
            # Initialize word list directly.
            wl = WordList()
            wl.initialize_list(qs, lex, user, shuffle=True)

        wgm = self.create_or_update_game_instance(
            user,
            lex,
            wl,
            use_table,
            multiplayer,
            timerSecs=secs,
            temp_list_name=named_list.name,
            questionsToPull=questions_per_round,
        )
        return wgm.pk
コード例 #3
0
ファイル: game.py プロジェクト: adrianiainlam/Webolith
    def initialize_word_list(self,
                             questions,
                             lexicon,
                             user,
                             category=WordList.CATEGORY_ANAGRAM):
        """
        Initializes a word list with the given questions and
        returns it.

        questions - An instance of Questions.
        lexicon - An instance of base.Lexicon
        user - The user.

        """
        if questions.size() == 0:
            raise GameInitException("No questions were found.")
        wl = WordList()
        wl.initialize_list(
            questions.to_python(),
            lexicon,
            user,
            shuffle=True,
            category=category,
        )
        return wl
コード例 #4
0
ファイル: views.py プロジェクト: domino14/Webolith
def new_quiz(request):
    """
    Create a new quiz but doesn't create any 'card' models.
    Card models will only be used for cardbox in future.
    """
    body = json.loads(request.body)
    logger.debug(body)
    lexicon = Lexicon.objects.get(lexiconName=body['lexicon'])
    try:
        search_description = build_search_criteria(
            request.user, lexicon, body['searchCriteria']
        )
    except GameInitException as e:
        return response(str(e), status=400)

    questions = word_search(search_description)
    if questions.size() == 0:
        return response('No questions were found.', status=400)
    wl = WordList()
    wl.initialize_list(list(questions.to_python()), lexicon, None,
                       shuffle=True, save=False)
    q_map = generate_question_map(questions)
    quiz_name = temporary_list_name(search_description,
                                    lexicon.lexiconName)
    # XXX add 1000-question limit?
    return response({
        'list': wl.to_python(),
        'q_map': q_map,
        'quiz_name': quiz_name,
    })
コード例 #5
0
def savedlist_from_probabilities(lexicon, p_min, p_max, length):
    """
    Creates a WordList instance from a list of Alphagram pks (indices)
    but *does not save it*.
    """
    db = WordDB(lexicon.lexiconName)
    questions = db.get_questions_for_probability_range(p_min, p_max, length)

    wl = WordList()
    wl.initialize_list(questions.to_python(), lexicon, None, shuffle=True,
                       save=False)
    q_map = generate_question_map(questions)
    return wl, q_map
コード例 #6
0
def create_user_list(contents: str, filename, lex, user):
    """
    Creates a user list from file contents, a filename, a lexicon,
    and a user. Checks to see if the user can create more lists.

    """
    filename_stripped, extension = os.path.splitext(filename)
    try:
        WordList.objects.get(name=filename_stripped, user=user, lexicon=lex)
        # uh oh, it exists!
        return (
            False,
            _("A list by the name {} already exists for this "
              "lexicon! Please rename your file.").format(filename_stripped))
    except WordList.DoesNotExist:
        pass
    t1 = time.time()
    try:
        alphas = get_alphas_from_words(
            contents, wordwalls.settings.UPLOAD_FILE_LINE_LIMIT)
    except UserListParseException as e:
        return (False, str(e))

    profile = user.aerolithprofile
    num_saved_alphas = profile.wordwallsSaveListSize
    limit = settings.SAVE_LIST_LIMIT_NONMEMBER

    if (num_saved_alphas + len(alphas)) > limit and not profile.member:
        return False, _(
            'This list would exceed your total list size limit. You can '
            'remove this limit by upgrading your membership!')

    questions = questions_from_alphagrams(lex, alphas)
    num_alphagrams = questions.size()

    logger.info('number of uploaded alphagrams: %d', num_alphagrams)
    logger.info('elapsed time: %f', time.time() - t1)
    logger.info('user: %s, filename: %s', user.username, filename)

    wl = WordList()
    wl.name = filename_stripped
    wl.initialize_list(questions.to_python(),
                       lex,
                       user,
                       shuffle=True,
                       keep_old_name=True)
    profile.wordwallsSaveListSize += num_alphagrams
    profile.save()

    return True, ''
コード例 #7
0
def create_user_list(contents, filename, lex, user):
    """
    Creates a user list from file contents, a filename, a lexicon,
    and a user. Checks to see if the user can create more lists.

    """
    filename_stripped, extension = os.path.splitext(filename)
    try:
        WordList.objects.get(name=filename_stripped, user=user, lexicon=lex)
        # uh oh, it exists!
        return (
            False,
            _("A list by the name {} already exists for this "
              "lexicon! Please rename your file.").format(filename_stripped))
    except WordList.DoesNotExist:
        pass
    t1 = time.time()
    try:
        alphas = get_alphas_from_words(
            contents, wordwalls.settings.UPLOAD_FILE_LINE_LIMIT)
    except UserListParseException as e:
        return (False, str(e))

    profile = user.aerolithprofile
    num_saved_alphas = profile.wordwallsSaveListSize
    limit = settings.SAVE_LIST_LIMIT_NONMEMBER

    if (num_saved_alphas + len(alphas)) > limit and not profile.member:
        return False, _("This list would exceed your total list size limit")
    db = WordDB(lex.lexiconName)

    questions = db.get_questions(alphas)
    num_alphagrams = questions.size()

    logger.info('number of uploaded alphagrams: %d', num_alphagrams)
    logger.info('elapsed time: %f', time.time() - t1)
    logger.info('user: %s, filename: %s', user.username, filename)

    wl = WordList()
    wl.name = filename_stripped
    wl.initialize_list(questions.to_python(), lex, user, shuffle=True,
                       keep_old_name=True)
    profile.wordwallsSaveListSize += num_alphagrams
    profile.save()

    return True, ''