Esempio n. 1
0
def challenge_submit(user, post):
    """
        Called when a challenge is submitted.
    """
    lexForm = LexiconForm(post)
    dcForm = DailyChallengesForm(post)
    if not(lexForm.is_valid() and dcForm.is_valid()):
        return response({'success': False,
                         'error': _('No challenge was selected.')})
    lex = Lexicon.objects.get(
        lexiconName=lexForm.cleaned_data['lexicon'])
    wwg = WordwallsGame()
    challengeName = DailyChallengeName.objects.get(
        name=dcForm.cleaned_data['challenge'])
    chDate = dcForm.cleaned_data['challengeDate']
    logger.debug('Selected in form: %s, %s, %s',
                 dcForm.cleaned_data['challenge'],
                 dcForm.cleaned_data['challengeDate'],
                 lexForm.cleaned_data['lexicon'])
    if not chDate or chDate > date.today():
        chDate = date.today()

    tablenum = wwg.initialize_daily_challenge(user, lex, challengeName, chDate)
    if tablenum == 0:
        return response({'success': False,
                         'error': _('Challenge does not exist.')})

    return response(
        {'url': reverse('wordwalls_table',
                        args=(tablenum,)),
         'success': True})
Esempio n. 2
0
def challenge_submit(user, post):
    """
        Called when a challenge is submitted.
    """
    lexForm = LexiconForm(post)
    dcForm = DailyChallengesForm(post)
    if not (lexForm.is_valid() and dcForm.is_valid()):
        return response({
            'success': False,
            'error': 'No challenge was selected.'
        })
    lex = Lexicon.objects.get(lexiconName=lexForm.cleaned_data['lexicon'])
    wwg = WordwallsGame()
    challengeName = DailyChallengeName.objects.get(
        name=dcForm.cleaned_data['challenge'])
    chDate = dcForm.cleaned_data['challengeDate']
    if not chDate or chDate > date.today():
        chDate = date.today()

    tablenum = wwg.initializeByDailyChallenge(user, lex, challengeName, chDate)
    if tablenum == 0:
        return response({
            'success': False,
            'error': 'Challenge does not exist.'
        })

    return response({
        'url': reverse('wordwalls_table', args=(tablenum, )),
        'success': True
    })
Esempio n. 3
0
def homepage(request):
    #unbound forms
    lexForm = LexiconForm()
    timeForm = TimeForm()
    fwForm = FindWordsForm()
    dcForm = DailyChallengesForm()
    ulForm = UserListForm()
    slForm = SavedListForm()
    nlForm = NamedListForm()
    profile = request.user.get_profile()

    if request.method == 'POST':
        return handle_homepage_post(profile, request)

    lengthCounts = dict([(l.lexiconName, l.lengthCounts)
                         for l in Lexicon.objects.all()])
    # Create a random token for socket connection and store in Redis
    # temporarily.
    # conn_token = get_connection_token(request.user)
    profile = request.user.get_profile()
    try:
        data = json.loads(profile.additional_data)
    except (TypeError, ValueError):
        data = {}
    return render(
        request,
        'wordwalls/index.html',
        {
            'fwForm':
            fwForm,
            'dcForm':
            dcForm,
            'challengeTypes': [(n.pk, n.name)
                               for n in DailyChallengeName.objects.all()],
            'ulForm':
            ulForm,
            'slForm':
            slForm,
            'lexForm':
            lexForm,
            'timeForm':
            timeForm,
            'nlForm':
            nlForm,
            'lengthCounts':
            json.dumps(lengthCounts),
            'upload_list_limit':
            wordwalls.settings.UPLOAD_FILE_LINE_LIMIT,
            'dcTimes':
            json.dumps(dcTimeMap),
            'defaultLexicon':
            profile.defaultLexicon,
            # 'connToken': conn_token,
            'chatEnabled':
            not data.get('disableChat', False),
            'socketUrl':
            settings.SOCKJS_SERVER,
            'CURRENT_VERSION':
            CURRENT_VERSION
        })
Esempio n. 4
0
def homepage(request):
    fwForm = FindWordsForm() # unbound
    dcForm = DailyChallengesForm() #unbound
    ulForm = UserListForm() # unbound
    slForm = SavedListForm()
    profile = request.user.get_profile()
    numAlphas = profile.wordwallsSaveListSize
    limit = 0
    if not profile.member:
        limit = wordwalls.settings.SAVE_LIST_LIMIT_NONMEMBER
    
    if request.method == 'POST':
        if 'searchParamsSubmit' in request.POST:
            fwForm = FindWordsForm(request.POST)   # form bound to the POST data
            if fwForm.is_valid():
                alphasSearchDescription = searchForAlphagrams(fwForm.cleaned_data)
                wwg = WordwallsGame()
                tablenum = wwg.initializeBySearchParams(request.user, alphasSearchDescription, 
                                fwForm.cleaned_data['playerMode'], int(round(fwForm.cleaned_data['quizTime'] * 60)))
    
                return HttpResponseRedirect(reverse('wordwalls_table', args=(tablenum,)))
        elif 'challengesSubmit' in request.POST:
            dcForm = DailyChallengesForm(request.POST)
            if dcForm.is_valid():
                wwg = WordwallsGame()
                lex = Lexicon.objects.get(lexiconName=dcForm.cleaned_data['lexicon_dc'])
                challengeName = DailyChallengeName.objects.get(name=dcForm.cleaned_data['challenge'])
                print 'challenge:', challengeName, lex
                tablenum = wwg.initializeByDailyChallenge(request.user, lex, challengeName)
                if tablenum == 0:
                    raise Http404
                else:
                    return HttpResponseRedirect(reverse('wordwalls_table', args=(tablenum,)))
                    
        elif 'userListsSubmit' in request.POST:
            # these are needed so that the forms are defined in case the ulForm.is_valid() fails. 
            # is there a better way to do this?
            
            ulForm = UserListForm(request.POST, request.FILES)
            if ulForm.is_valid():
                lex = Lexicon.objects.get(lexiconName=ulForm.cleaned_data['lexicon_ul'])
                wwg = WordwallsGame()
                
                tablenum = wwg.initializeByUserList(request.FILES['file'], lex, 
                            request.user, int(round(ulForm.cleaned_data['quizTime_ul'] * 60)))
                #return HttpResponseRedirect('/success/url/')
                if tablenum == 0:
                    raise Http404   # TODO better error message
                else:
                    return HttpResponseRedirect(reverse('wordwalls_table', args=(tablenum,)))
        
        elif 'savedListsSubmit' in request.POST:
            slForm = SavedListForm(request.POST)
            if slForm.is_valid():
                if slForm.cleaned_data['listOption'] == SavedListForm.DELETE_LIST_CHOICE:
                    deleteSavedList(slForm.cleaned_data['wordList'], request.user)
                    # todo AJAXify this; return a response. it must have the new total list size, for this user, and it must tell
                    # the javascript to delete the non-existent list model
                else:                    
                    lex = Lexicon.objects.get(lexiconName=slForm.cleaned_data['lexicon_sl'])
                    wwg = WordwallsGame()
                    tablenum = wwg.initializeBySavedList(lex, request.user, slForm.cleaned_data['wordList'],
                                                            slForm.cleaned_data['listOption'],
                                                            int(round(slForm.cleaned_data['quizTime_sl'] * 60)))
                    if tablenum == 0:
                        raise Http404
                    else:
                        return HttpResponseRedirect(reverse('wordwalls_table', args=(tablenum,)))
                
        
        elif 'action' in request.POST:
            print request.POST['action']
            if request.POST['action'] == 'getDcResults':
                try:
                    lex = request.POST['lexicon']
                    chName = DailyChallengeName.objects.get(name=request.POST['chName'])
                    leaderboardData = getLeaderboardData(lex, chName)
                    response = HttpResponse(json.dumps(leaderboardData, ensure_ascii=False), 
                                            mimetype="application/javascript")
                    response['Content-Type'] = 'text/plain; charset=utf-8'
                    return response
                except:
                    raise Http404
            elif request.POST['action'] == 'getSavedListList':
                # gets a list of saved lists!
                try:
                    lex = request.POST['lexicon']
                    lt = getSavedListList(lex, request.user)
                    print lt
                    response = HttpResponse(json.dumps(lt, ensure_ascii=False), 
                                            mimetype="application/javascript")
                    response['Content-Type'] = 'text/plain; charset=utf-8'
                    return response
                
                except:
                    raise Http404
                    
            elif request.POST['action'] == 'getSavedListNumAlphas':
                response = HttpResponse(json.dumps({'na': numAlphas, 'l': limit}), mimetype="application/javascript")
                response['Content-Type'] = 'text/plain; charset=utf-8'
                return response
                    
    lengthCounts = dict([(l.lexiconName, l.lengthCounts) for l in Lexicon.objects.all()])                            
    return render_to_response('wordwalls/index.html', 
                            {'fwForm': fwForm, 
                            'dcForm' : dcForm, 
                            'ulForm' : ulForm,
                            'slForm' : slForm,
                            'lengthCounts' : json.dumps(lengthCounts)}, 
                            context_instance=RequestContext(request))