Ejemplo n.º 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,
    })
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
def saved_list_sync(request):
    """
    Accept a POST of a NEW saved list.
    """
    if request.method != 'POST':
        return response('This endpoint only accepts a POST.', status=400)
    body = json.loads(request.body)
    profile = request.user.aerolithprofile
    num_saved_alphas = profile.wordwallsSaveListSize
    limit = settings.SAVE_LIST_LIMIT_NONMEMBER
    logger.info('Syncing %s' % body)
    orig_qs = body.get('origQuestions')

    # Try getting a saved list with the same name, lexicon, and user.
    sl = WordList.objects.filter(user=request.user,
                                 lexicon__lexiconName=body.get('lexicon'),
                                 name=body.get('name'))
    if len(sl):
        return response(
            'A list by that name already exists. Please remove '
            'that saved list and try again.',
            status=400)

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

    sl = WordList(user=request.user,
                  lexicon=Lexicon.objects.get(lexiconName=body.get('lexicon')),
                  name=body.get('name'),
                  numAlphagrams=body.get('numAlphagrams'),
                  numCurAlphagrams=body.get('numCurAlphagrams'),
                  numFirstMissed=body.get('numFirstMissed'),
                  numMissed=body.get('numMissed'),
                  goneThruOnce=body.get('goneThruOnce'),
                  questionIndex=body.get('questionIndex'),
                  origQuestions=json.dumps(orig_qs),
                  curQuestions=json.dumps(body.get('curQuestions')),
                  missed=json.dumps(body.get('missed')),
                  firstMissed=json.dumps(body.get('firstMissed')),
                  is_temporary=False,
                  version=2)
    try:
        sl.full_clean()
    except ValidationError as e:
        return response('Your saved list is improperly formatted: %s', e)
    sl.save()
    profile.wordwallsSaveListSize += len(orig_qs)
    profile.save()
    # Get stars.
    stars = body.get('starTags', {})
    if stars:
        save_stars(request, stars, sl)

    return response(sl.to_python())
Ejemplo n.º 5
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, ''