def log_stream(request): if not request.is_ajax(): return response.HttpResponseBadRequest( "Only Ajax requests are accepted.") if request.method != 'POST': return response.HttpResponseNotAllowed(request.method) search_str = request.POST.get("search") aggregates = [] if search_str != "": aggregates.append({"$match": {"$text": {"$search": search_str}}}) last_requests = log_records.aggregate(aggregates + [{ "$sort": { "time": -1 } }, { "$limit": 100 }, { "$project": { "_id": 0 } }]) data = {"last_requests": list(last_requests)} return response.JsonResponse(data)
def not_allowed(methods): payload = _get_error_payload( 'Method Not Allowed', 'Method Not Allowed', '405') return django_resp.HttpResponseNotAllowed( methods, content=json.dumps(payload), content_type=const.HTTP_RESPONSE_CONTENT_TYPE)
def object_tool_view(self, request, action_name, object_id=None, extra_context=None): if action_name not in self.get_object_tools(request): return response.HttpResponseForbidden() action = self.get_object_tool(action_name)[0] allow_get = getattr(action, "allow_get", False) if not allow_get and request.method != "POST": return response.HttpResponseNotAllowed() obj = object_id and self.get_object(request, unquote(object_id)) return self.response_object_tool(request, action, obj, extra_context)
def request_month(request): if not request.is_ajax(): return response.HttpResponseBadRequest( "Only Ajax requests are accepted.") if request.method != 'POST': return response.HttpResponseNotAllowed(request.method) year = request.POST.get("year") month = request.POST.get("month") return response.JsonResponse(requests_month_bar(year=year, month=month))
def get_speech_from_text(request): if request.method == 'GET' and request.is_ajax(): try: res = requests.get(url=TTS_URL, params={ 'key': TTS_KEY, 'keyword': request.GET['keyword'], 'return_type': 'code' }) data = json.loads(res.text) return response.JsonResponse( {'content': data['return_object']['content']}) except Exception: return response.HttpResponseNotFound() else: return response.HttpResponseNotAllowed(['GET'])
def guess_letter(request: HttpRequest): if not request.method == "POST": return response.HttpResponseNotAllowed(request.method) letter = request.POST["letter"] letter = letter.lower() if len(letter) != 1: return validation_content( request, "You didn't provide a valid letter (must be one and only one).") if g_convict.is_game_finished(): return validation_content(request, "Game is already over.") __is_correct = g_convict.guess_letter(letter) g_guess_history.add_guess(letter) if g_convict.is_game_finished(): g_progress.game_history.add_game_result(__is_correct) # TODO: use redirect here return render( request=request, template_name="hangman/hangman.html", context=__game_content( { "secret_word": g_secret_word.get_word() if g_convict.is_game_finished() else None, "is_game_finished": g_convict.is_game_finished(), "is_correct": __is_correct, }, g_known_word_printer.get_known_word(), g_convict.get_game_status(), g_guess_history.get_history(), ), )
def hangman(request): if not request.method == "GET": return response.HttpResponseNotAllowed(request.method) global g_secret_word global g_convict global g_known_word_printer global g_guess_history g_progress.evaluate_game_level() __difficulty = progress.DifficultyFactory.create_difficulty( g_progress.get_game_level()) __word_length_range = ( __difficulty.get_word_length_min(), __difficulty.get_word_length_max(), ) g_secret_word = game.SecretWordFactory( word_provider=g_wordProvider).create_secret_word( word_length_range=__word_length_range) g_convict = game.ConvictFactory.create_convict(secret_word=g_secret_word) g_known_word_printer = KnownWordPrinter(convict=g_convict, secret_word=g_secret_word) g_guess_history = statistics.GuessHistory() return render( request=request, template_name="hangman/hangman.html", context=__game_content( {}, g_known_word_printer.get_known_word(), g_convict.get_game_status(), g_guess_history.get_history(), ), )