示例#1
0
文件: views.py 项目: fizzix/Webolith
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))