def api_answers(request): if request.method != "GET": return bad_request("Must use GET.") tablenum = request.GET.get("tablenum") wwg = WordwallsGame() answer_list = wwg.answer_list(tablenum) if answer_list: return response(answer_list) return bad_request("Could not fetch answer list")
def api_challengers_by_tablenum(request): if request.method != "GET": return bad_request("Must use GET.") tablenum = request.GET.get("tablenum") tiebreaker = request.GET.get("tiebreaker", "errors") wwg = WordwallsGame() dc_id = wwg.get_dc_id(tablenum) if dc_id > 0: dc = DailyChallenge.objects.get(pk=dc_id) leaderboard_data = get_leaderboard_data_for_dc_instance(dc, tiebreaker) return response(leaderboard_data) return bad_request("No such daily challenge.")
def wrap(request, *args, **kwargs): """ A decorator for all the functions that load new words. """ try: body = json.loads(request.body) except (TypeError, ValueError): return bad_request("Badly formatted body.") # First verify that the user has access to this table. if not access_to_table(body["tablenum"], request.user): return bad_request("User is not in this table.") parsed_req = { # If tablenum is None, the utility functions in game.py know # to create a new table, instead of using an existing table # number. "tablenum": body["tablenum"] if body["tablenum"] != 0 else None } lex_id = body.get("lexicon") try: lexicon = Lexicon.objects.get(pk=lex_id) except Lexicon.DoesNotExist: return bad_request("Bad lexicon.") parsed_req["lexicon"] = lexicon parsed_req["challenge"] = body.get("challenge") parsed_req["dt"] = body.get("date") if "desiredTime" in body: quiz_time_secs = int(round(body["desiredTime"] * 60)) if quiz_time_secs < 1 or quiz_time_secs > 3600: return bad_request( "Desired time must be between 1 and 3600 " "seconds." ) parsed_req["quiz_time_secs"] = quiz_time_secs parsed_req["questions_per_round"] = body.get("questionsPerRound", 50) if ( parsed_req["questions_per_round"] > 200 or parsed_req["questions_per_round"] < 10 ): return bad_request( "Questions per round must be between 10 and 200." ) parsed_req["search_criteria"] = body.get("searchCriteria", []) parsed_req["list_option"] = body.get("listOption") parsed_req["selectedList"] = body.get("selectedList") parsed_req["multiplayer"] = body.get("multiplayer", False) parsed_req["raw_questions"] = body.get("rawQuestions", False) return f(request, parsed_req, *args, **kwargs)
def api_challengers(request): if request.method != "GET": return bad_request("Must use GET.") lex = request.GET.get("lexicon") ch_id = request.GET.get("challenge") ch_date = date_from_request_dict(request.GET) tiebreaker = request.GET.get("tiebreaker", "errors") try: lex = Lexicon.objects.get(pk=lex) ch_name = DailyChallengeName.objects.get(pk=ch_id) except (ObjectDoesNotExist, ValueError, TypeError): return bad_request("Bad lexicon or challenge.") return response(get_leaderboard_data(lex, ch_name, ch_date, tiebreaker))
def special_challenges(request): lex = request.GET.get("lexicon") ch_date = date_from_request_dict(request.GET) try: lex = Lexicon.objects.get(pk=lex) except Lexicon.DoesNotExist: return bad_request("Bad lexicon.") challenges = DailyChallenge.objects.filter( date=ch_date, lexicon=lex, name__orderPriority=DailyChallengeName.SPECIAL_CHALLENGE_ORDER_PRIORITY, ).order_by( # noqa "id" ) resp = [] for challenge in challenges: resp.append( { "id": challenge.name.id, "seconds": challenge.seconds, "numQuestions": len(json.loads(challenge.alphagrams)), "name": challenge.visible_name, "orderPriority": challenge.name.orderPriority, } ) return response(resp)
def main(event, context): post_data = event.get("body") post_data = json.loads(post_data) (is_valid, validation_error) = validate(post_data) if is_valid: return response.success(create(post_data)) else: return response.bad_request({"error": validation_error})
def main(event, context): post_data = event.get("body") post_data = json.loads(post_data) q_id = event.get("pathParameters").get("id") (is_valid, validation_error) = validate(post_data) if is_valid: return update(post_data, q_id) else: return response.bad_request({"error": validation_error})
def load_aerolith_list(request, parsed_req_body): """ Load an Aerolith list (a pre-defined list) into this table. """ try: named_list = NamedList.objects.get(pk=parsed_req_body["selectedList"]) except NamedList.DoesNotExist: return bad_request("List does not exist.") except (TypeError, ValueError): return bad_request("Please select a list.") tablenum = WordwallsGame().initialize_by_named_list( parsed_req_body["lexicon"], request.user, named_list, parsed_req_body["quiz_time_secs"], parsed_req_body["questions_per_round"], use_table=parsed_req_body["tablenum"], multiplayer=parsed_req_body["multiplayer"], ) return table_response(tablenum)
def configure(request): if request.method != "POST": return bad_request("Must use POST.") # XXX: User can put any old JSON here. We should do some backend # validation. prefs = json.loads(request.body) profile = request.user.aerolithprofile profile.customWordwallsStyle = json.dumps(prefs) profile.save() return response("OK")
def new_challenge(request, parsed_req_body): """ Load a new challenge into this table. """ try: challenge_name = DailyChallengeName.objects.get( pk=parsed_req_body["challenge"] ) except DailyChallengeName.DoesNotExist: return bad_request("Bad challenge.") try: tablenum = WordwallsGame().initialize_daily_challenge( request.user, parsed_req_body["lexicon"], challenge_name, date_from_str(parsed_req_body["dt"]), use_table=parsed_req_body["tablenum"], ) except GameInitException as e: return bad_request(str(e)) return table_response(tablenum)
def load_raw_questions(request, parsed_req_body): try: tablenum = WordwallsGame().initialize_by_raw_questions( parsed_req_body["lexicon"], request.user, parsed_req_body["raw_questions"], parsed_req_body["quiz_time_secs"], parsed_req_body["questions_per_round"], use_table=parsed_req_body["tablenum"], multiplayer=parsed_req_body["multiplayer"], ) except GameInitException as e: return bad_request(str(e)) return table_response(tablenum)
def load_saved_list(request, parsed_req_body): """ Load a user Saved List into this table. """ try: saved_list = WordList.objects.get( user=request.user, pk=parsed_req_body["selectedList"] ) except WordList.DoesNotExist: return bad_request("List does not exist.") try: tablenum = WordwallsGame().initialize_by_saved_list( parsed_req_body["lexicon"], request.user, saved_list, convert_to_form_option(parsed_req_body["list_option"]), parsed_req_body["quiz_time_secs"], parsed_req_body["questions_per_round"], use_table=parsed_req_body["tablenum"], multiplayer=parsed_req_body["multiplayer"], ) except GameInitException as e: return bad_request(str(e)) return table_response(tablenum)
def set_default_lexicon(request): try: profile = AerolithProfile.objects.get(user=request.user) except AerolithProfile.DoesNotExist: raise Http404 body = json.loads(request.body) lex = body.get('defaultLexicon', -1) try: lexicon = Lexicon.objects.get(pk=lex) except Lexicon.DoesNotExist: return bad_request('Lexicon not found') profile.defaultLexicon = lexicon profile.save() return response('OK')
def bad_rpc_response(req_id, error_message, error_code=400, error_data=None): """ :error Must be an object {"code": 200, "message": "blah", "data": "optional"} """ err_obj = { 'jsonrpc': '2.0', 'error': { 'code': error_code, 'message': error_message, }, 'id': req_id } if error_data: err_obj['error']['data'] = error_data return bad_request(err_obj)
def default_lists(request): lex_id = request.GET.get("lexicon") try: lex = Lexicon.objects.get(pk=lex_id) except Lexicon.DoesNotExist: return bad_request("Bad lexicon.") ret_data = [] for nl in NamedList.objects.filter(lexicon=lex).order_by("id"): ret_data.append( { "name": nl.name, "lexicon": nl.lexicon.lexiconName, "numAlphas": nl.numQuestions, "wordLength": nl.wordLength, "id": nl.pk, } ) return response(ret_data)
def challenges_played(request): """ Get the challenges for the given day, played by the logged-in user. """ lex = request.GET.get("lexicon") ch_date = date_from_request_dict(request.GET) try: lex = Lexicon.objects.get(pk=lex) except Lexicon.DoesNotExist: return bad_request("Bad lexicon.") challenges = DailyChallenge.objects.filter(date=ch_date, lexicon=lex) entries = DailyChallengeLeaderboardEntry.objects.filter( board__challenge__in=challenges, user=request.user ) resp = [] for entry in entries: resp.append({"challengeID": entry.board.challenge.name.pk}) # Search for toughies challenge as well. toughies_date = toughies_challenge_date(ch_date) try: relevant_toughie = DailyChallenge.objects.get( date=toughies_date, lexicon=lex, name__name=DailyChallengeName.WEEKS_BINGO_TOUGHIES, ) except DailyChallenge.DoesNotExist: return response(resp) # If the toughies date is not the date in question, we need to see if # the user has an entry for this challenge. if toughies_date != ch_date: try: entry = DailyChallengeLeaderboardEntry.objects.get( board__challenge=relevant_toughie, user=request.user ) except DailyChallengeLeaderboardEntry.DoesNotExist: return response(resp) resp.append({"challengeID": entry.board.challenge.name.pk}) return response(resp)
def new_search(request, parsed_req_body): """ Load a new search into this table. """ try: search = build_search_criteria( request.user, parsed_req_body["lexicon"], parsed_req_body["search_criteria"], ) tablenum = WordwallsGame().initialize_by_search_params( request.user, search, parsed_req_body["quiz_time_secs"], parsed_req_body["questions_per_round"], use_table=parsed_req_body["tablenum"], multiplayer=parsed_req_body["multiplayer"], ) except GameInitException as e: return bad_request(str(e)) return table_response(tablenum)