def named_lists_submit(user, post): lexForm = LexiconForm(post) timeForm = TimeForm(post) nlForm = NamedListForm(post) num_q_form = NumQuestionsForm(post) if not (lexForm.is_valid() and timeForm.is_valid() and nlForm.is_valid() and num_q_form.is_valid()): return response({'success': False, 'error': _('Please check that you have selected a ' 'list and that your quiz time is greater ' 'than 1 minute.')}) lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) quizTime = int( round(timeForm.cleaned_data['quizTime'] * 60)) wwg = WordwallsGame() questions_per_round = num_q_form.cleaned_data['num_questions'] tablenum = wwg.initialize_by_named_list( lex, user, nlForm.cleaned_data['namedList'], quizTime, questions_per_round) if tablenum == 0: raise Http404 return response({'url': reverse('wordwalls_table', args=(tablenum,)), 'success': True})
def search_params_submit(user, post): """ Called when user submits a search params query. """ lexForm = LexiconForm(post) timeForm = TimeForm(post) fwForm = FindWordsForm(post) num_q_form = NumQuestionsForm(post) # form bound to the POST data if not (lexForm.is_valid() and timeForm.is_valid() and fwForm.is_valid() and num_q_form.is_valid()): return response({'success': False, 'error': _('There was something wrong with your ' 'search parameters or time selection.')}) lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) quiz_time = int(round(timeForm.cleaned_data['quizTime'] * 60)) questions_per_round = num_q_form.cleaned_data['num_questions'] search = searchForAlphagrams(fwForm.cleaned_data, lex) wwg = WordwallsGame() tablenum = wwg.initialize_by_search_params(user, search, quiz_time, questions_per_round) return response({'url': reverse('wordwalls_table', args=(tablenum,)), 'success': True})
def search_params_submit(user, post): """ Called when user submits a search params query. """ lexForm = LexiconForm(post) timeForm = TimeForm(post) fwForm = FindWordsForm(post) # form bound to the POST data if not (lexForm.is_valid() and timeForm.is_valid() and fwForm.is_valid()): return response({ 'success': False, 'error': 'There was something wrong with your ' 'search parameters or time selection.' }) lex = Lexicon.objects.get(lexiconName=lexForm.cleaned_data['lexicon']) quizTime = int(round(timeForm.cleaned_data['quizTime'] * 60)) alphasSearchDescription = searchForAlphagrams(fwForm.cleaned_data, lex) wwg = WordwallsGame() tablenum = wwg.initializeBySearchParams(user, alphasSearchDescription, quizTime) return response({ 'url': reverse('wordwalls_table', args=(tablenum, )), 'success': True })
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})
def named_lists_submit(user, post): lexForm = LexiconForm(post) timeForm = TimeForm(post) nlForm = NamedListForm(post) if not (lexForm.is_valid() and timeForm.is_valid() and nlForm.is_valid()): return response({ 'success': False, 'error': 'Please check that you have selected a ' 'list and that your quiz time is greater ' 'than 1 minute.' }) lex = Lexicon.objects.get(lexiconName=lexForm.cleaned_data['lexicon']) quizTime = int(round(timeForm.cleaned_data['quizTime'] * 60)) wwg = WordwallsGame() tablenum = wwg.initializeByNamedList(lex, user, nlForm.cleaned_data['namedList'], quizTime) if tablenum == 0: raise Http404 return response({ 'url': reverse('wordwalls_table', args=(tablenum, )), 'success': True })
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 })
def ajax_upload(request): if request.method != "POST": return response(_('This endpoint only accepts POST'), StatusCode.BAD_REQUEST) lex_form = LexiconForm(request.POST) if lex_form.is_valid(): lex = Lexicon.objects.get( lexiconName=lex_form.cleaned_data['lexicon']) else: logger.debug(lex_form.errors) return response(_('Bad lexicon.'), StatusCode.BAD_REQUEST) uploaded_file = request.FILES['file'] if uploaded_file.multiple_chunks(): return response(_('Your file is too big.'), StatusCode.BAD_REQUEST) filename = uploaded_file.name try: file_contents = uploaded_file.read().decode('utf-8') except UnicodeDecodeError: return response(_('Please make sure your file is utf-8 encoded.'), StatusCode.BAD_REQUEST) # save the file success, msg = create_user_list(file_contents, filename, lex, request.user) if not success: return response(msg, StatusCode.BAD_REQUEST) return response(msg)
def ajax_upload(request): if request.method != "POST": return response(_('This endpoint only accepts POST'), StatusCode.BAD_REQUEST) lex_form = LexiconForm(request.POST) if lex_form.is_valid(): lex = Lexicon.objects.get(lexiconName=lex_form.cleaned_data['lexicon']) else: logger.warning(lex_form.errors) return response(_('Bad lexicon.'), StatusCode.BAD_REQUEST) uploaded_file = request.FILES['file'] if uploaded_file.multiple_chunks(): return response(_('Your file is too big.'), StatusCode.BAD_REQUEST) filename = uploaded_file.name try: # utf-8-sig will throw away the UTF8 BOM if found at the beginning of # the file. file_contents = uploaded_file.read().decode('utf-8-sig') except UnicodeDecodeError: return response(_('Please make sure your file is utf-8 encoded.'), StatusCode.BAD_REQUEST) # save the file success, msg = create_user_list(file_contents, filename, lex, request.user) if not success: return response(msg, StatusCode.BAD_REQUEST) return response(msg)
def saved_list_delete(user, post): lexForm = LexiconForm(post) slForm = SavedListForm(post) if not (lexForm.is_valid() and slForm.is_valid()): return response({'success': False, 'error': _('You did not select a list to delete.')}) deletedListPk = slForm.cleaned_data['wordList'].pk deleteSavedList(slForm.cleaned_data['wordList'], user) return response({'deleted': True, 'wordList': deletedListPk})
def saved_list_delete(user, post): lexForm = LexiconForm(post) slForm = SavedListForm(post) if not (lexForm.is_valid() and slForm.is_valid()): return response({ 'success': False, 'error': 'You did not select a list to delete.' }) deletedListPk = slForm.cleaned_data['wordList'].pk deleteSavedList(slForm.cleaned_data['wordList'], user) return response({'deleted': True, 'wordList': deletedListPk})
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 })
def ajax_upload(request): if request.method == "POST": lexForm = LexiconForm(request.GET) if lexForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) else: raise Http404("Bad lexicon") if request.is_ajax(): # the file is stored raw in the request upload = request # AJAX Upload will pass the filename in the querystring if # it is the "advanced" ajax upload try: filename = request.GET['qqfile'] except KeyError: return HttpResponseBadRequest("AJAX request not valid") # Not an ajax upload, so it was the "basic" iframe version # with submission via form else: if len(request.FILES) == 1: # FILES is a dictionary in Django but Ajax Upload gives # the uploaded file an ID based on a random number, so # it cannot be guessed here in the code. Rather than # editing Ajax Upload to pass the ID in the querystring, # observe that each upload is a separate request, so # FILES should only have one entry. Thus, we can just # grab the first (and only) value in the dict. upload = request.FILES.values()[0] else: raise Http404("Bad Upload") filename = upload.name # save the file success, msg = createUserList(upload, filename, lex, request.user) # let Ajax Upload know whether we saved it or not ret_json = {'success': success, 'msg': msg} return HttpResponse(json.dumps(ret_json))
def createQuiz(request): if request.method == 'GET': return render(request, 'whitleyCards/index.html', {'accessedWithGet': True}) elif request.method == 'POST': action = request.POST.get('action') if action == 'searchParamsFlashcard': lexForm = LexiconForm(request.POST) # form bound to the POST data fwForm = FindWordsForm(request.POST) if lexForm.is_valid() and fwForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) asd = searchForAlphagrams(fwForm.cleaned_data, lex) return response({ 'url': reverse('flashcards_by_prob_range', args=(asd['lexicon'].pk, asd['length'], asd['min'], asd['max'])), 'success': True}) elif action == 'namedListsFlashcard': lexForm = LexiconForm(request.POST) nlForm = NamedListForm(request.POST) if lexForm.is_valid() and nlForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) # lex doesn't matter return response({ 'url': reverse( 'flashcards_by_namedList_pk', args=(nlForm.cleaned_data['namedList'].pk,)), 'success': True}) elif (action == 'savedListsFlashcardEntire' or action == 'savedListsFlashcardFM'): lexForm = LexiconForm(request.POST) slForm = SavedListForm(request.POST) if lexForm.is_valid() and slForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) # lex doesn't matter if request.POST['action'] == 'savedListsFlashcardEntire': option = SavedListForm.RESTART_LIST_CHOICE elif request.POST['action'] == 'savedListsFlashcardFM': option = SavedListForm.FIRST_MISSED_CHOICE return response({ 'url': reverse('flashcards_by_savedList_pk', args=(slForm.cleaned_data['wordList'].pk, option)), 'success': True}) # don't do any checking right now for user access to # other user lists. why? maybe people can share lists # this way as long as we're not letting the users delete # lists, i think it should be fine. return response({'success': False, 'error': 'Did you select a list to flashcard?'}, status=400)
def saved_lists_submit(user, post): lexForm = LexiconForm(post) timeForm = TimeForm(post) slForm = SavedListForm(post) if not (lexForm.is_valid() and timeForm.is_valid() and slForm.is_valid()): return response({'success': False, 'error': 'Please check that you have selected ' 'a word list and a time greater than ' '1 minute.'}) lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) quizTime = int( round(timeForm.cleaned_data['quizTime'] * 60)) wwg = WordwallsGame() tablenum = wwg.initializeBySavedList( lex, user, slForm.cleaned_data['wordList'], slForm.cleaned_data['listOption'], quizTime) if tablenum == 0: raise Http404 return response({'url': reverse('wordwalls_table', args=(tablenum,)), 'success': True})
def handle_create_post(request): action = request.POST.get('action') if action == 'searchParamsFlashcard': # form bound to the POST data lexForm = LexiconForm(request.POST) if lexForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) logger.debug('POST data %s', request.POST) try: encoded_search = search_criteria_to_b64(request.POST) except ImproperCodingException: return response( { 'success': False, 'error': 'Please refresh; the app has changed.' }, status=400) return response({ 'url': reverse('flashcards_by_search', args=(lex.pk, encoded_search.decode())), 'success': True }) elif action == 'namedListsFlashcard': lexForm = LexiconForm(request.POST) nlForm = NamedListForm(request.POST) if lexForm.is_valid() and nlForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) # lex doesn't matter return response({ 'url': reverse('flashcards_by_namedList_pk', args=(nlForm.cleaned_data['namedList'].pk, )), 'success': True }) elif (action == 'savedListsFlashcardEntire' or action == 'savedListsFlashcardFM'): lexForm = LexiconForm(request.POST) slForm = SavedListForm(request.POST) if lexForm.is_valid() and slForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) # lex doesn't matter if request.POST['action'] == 'savedListsFlashcardEntire': option = SavedListForm.RESTART_LIST_CHOICE elif request.POST['action'] == 'savedListsFlashcardFM': option = SavedListForm.FIRST_MISSED_CHOICE return response({ 'url': reverse('flashcards_by_savedList_pk', args=(slForm.cleaned_data['wordList'].pk, option)), 'success': True }) # don't do any checking right now for user access to # other user lists. why? maybe people can share lists # this way as long as we're not letting the users delete # lists, i think it should be fine. return response( { 'success': False, 'error': 'Did you select a list to flashcard?' }, status=400)
def createQuiz(request): if request.method == 'GET': return render_to_response('whitleyCards/index.html', {'accessedWithGet': True}, context_instance=RequestContext(request)) elif request.method == 'POST': action = request.POST.get('action') if action == 'searchParamsFlashcard': lexForm = LexiconForm(request.POST) fwForm = FindWordsForm(request.POST) # form bound to the POST data if lexForm.is_valid() and fwForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) asd = searchForAlphagrams(fwForm.cleaned_data, lex) #questionData = getWordDataByProb(alphasSearchDescription['min'], alphasSearchDescription['max']) response = HttpResponse(json.dumps({ 'url': reverse('flashcards_by_prob_pk_range', args=(asd['min'], asd['max'])), 'success': True }), mimetype="application/javascript") response['Content-Type'] = 'text/plain; charset=utf-8' return response elif action == 'namedListsFlashcard': lexForm = LexiconForm(request.POST) nlForm = NamedListForm(request.POST) if lexForm.is_valid() and nlForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) # lex doesn't matter response = HttpResponse(json.dumps({ 'url': reverse('flashcards_by_namedList_pk', args=(nlForm.cleaned_data['namedList'].pk, )), 'success': True }), mimetype="application/javascript") response['Content-Type'] = 'text/plain; charset=utf-8' return response elif action == 'savedListsFlashcardEntire' or action == 'savedListsFlashcardFM': lexForm = LexiconForm(request.POST) slForm = SavedListForm(request.POST) if lexForm.is_valid() and slForm.is_valid(): lex = Lexicon.objects.get( lexiconName=lexForm.cleaned_data['lexicon']) # lex doesn't matter if request.POST['action'] == 'savedListsFlashcardEntire': option = SavedListForm.RESTART_LIST_CHOICE elif request.POST['action'] == 'savedListsFlashcardFM': option = SavedListForm.FIRST_MISSED_CHOICE response = HttpResponse(json.dumps({ 'url': reverse('flashcards_by_savedList_pk', args=(slForm.cleaned_data['wordList'].pk, option)), 'success': True }), mimetype="application/javascript") response['Content-Type'] = 'text/plain; charset=utf-8' return response # don't do any checking right now for user access to other user lists. why? maybe people can share lists this way # as long as we're not letting the users delete lists, i think it should be fine. response = HttpResponse(json.dumps({ 'success': False, 'error': 'Did you select a list to ' 'flashcard?' }), mimetype="application/javascript") response['Content-Type'] = 'text/plain; charset=utf-8' return response
def createQuiz(request): if request.method == 'GET': return render_to_response('whitleyCards/index.html', {'accessedWithGet': True }, context_instance=RequestContext(request)) elif request.method == 'POST': action = request.POST.get('action') if action == 'searchParamsFlashcard': lexForm = LexiconForm(request.POST) fwForm = FindWordsForm(request.POST) # form bound to the POST data if lexForm.is_valid() and fwForm.is_valid(): lex = Lexicon.objects.get(lexiconName=lexForm.cleaned_data['lexicon']) asd = searchForAlphagrams(fwForm.cleaned_data, lex) #questionData = getWordDataByProb(alphasSearchDescription['min'], alphasSearchDescription['max']) response = HttpResponse(json.dumps( {'url': reverse('flashcards_by_prob_pk_range', args=(asd['min'], asd['max'])), 'success': True} ), mimetype="application/javascript") response['Content-Type'] = 'text/plain; charset=utf-8' return response elif action == 'namedListsFlashcard': lexForm = LexiconForm(request.POST) nlForm = NamedListForm(request.POST) if lexForm.is_valid() and nlForm.is_valid(): lex = Lexicon.objects.get(lexiconName=lexForm.cleaned_data['lexicon']) # lex doesn't matter response = HttpResponse(json.dumps( {'url': reverse('flashcards_by_namedList_pk', args=(nlForm.cleaned_data['namedList'].pk,)), 'success': True} ), mimetype="application/javascript") response['Content-Type'] = 'text/plain; charset=utf-8' return response elif action == 'savedListsFlashcardEntire' or action == 'savedListsFlashcardFM': lexForm = LexiconForm(request.POST) slForm = SavedListForm(request.POST) if lexForm.is_valid() and slForm.is_valid(): lex = Lexicon.objects.get(lexiconName=lexForm.cleaned_data['lexicon']) # lex doesn't matter if request.POST['action'] == 'savedListsFlashcardEntire': option = SavedListForm.RESTART_LIST_CHOICE elif request.POST['action'] == 'savedListsFlashcardFM': option = SavedListForm.FIRST_MISSED_CHOICE response = HttpResponse(json.dumps( {'url': reverse('flashcards_by_savedList_pk', args=(slForm.cleaned_data['wordList'].pk, option)), 'success': True} ), mimetype="application/javascript") response['Content-Type'] = 'text/plain; charset=utf-8' return response # don't do any checking right now for user access to other user lists. why? maybe people can share lists this way # as long as we're not letting the users delete lists, i think it should be fine. response = HttpResponse(json.dumps({'success': False, 'error': 'Did you select a list to ' 'flashcard?'}), mimetype="application/javascript") response['Content-Type'] = 'text/plain; charset=utf-8' return response