Moved database functions to database.py
main page shows 3 questions of easy medium and hard difficulty
This commit is contained in:
parent
fa5c9a1f1a
commit
dd763deaea
@ -1,38 +1,23 @@
|
||||
from flask import Flask, render_template, redirect, url_for
|
||||
from botocore.exceptions import ClientError
|
||||
import boto3
|
||||
import random
|
||||
from dynamodb_json import json_util
|
||||
import database
|
||||
|
||||
application = Flask(__name__)
|
||||
application.config.from_pyfile("config.py")
|
||||
|
||||
|
||||
def get_table(table_name):
|
||||
dynamo = boto3.resource("dynamodb", aws_access_key_id=application.config["AWS_ACCESS_KEY_ID"],
|
||||
aws_secret_access_key=application.config["AWS_SECRET_ACCESS_KEY"], region_name=application.config["REGION_NAME"])
|
||||
return dynamo.Table(table_name)
|
||||
|
||||
|
||||
def get_question(table, id):
|
||||
response = table.get_item(Key={"id": id})
|
||||
return json_util.loads(response["Item"])
|
||||
|
||||
|
||||
@application.route("/")
|
||||
def index():
|
||||
try:
|
||||
table = get_table("BibleQuestions")
|
||||
|
||||
item = get_question(table, random.randint(0, table.item_count - 1))
|
||||
easy = database.get_random_question_difficulty(1)
|
||||
medium = database.get_random_question_difficulty(2)
|
||||
hard = database.get_random_question_difficulty(3)
|
||||
return render_template(
|
||||
"index.html",
|
||||
title="Bible Trivia",
|
||||
question=item["Question"],
|
||||
answer=item["Answer"],
|
||||
addresses=item["Address"],
|
||||
category=item["Category"],
|
||||
difficulty=item["Difficulty"],
|
||||
title="Quiz The Word",
|
||||
easy=easy,
|
||||
medium=medium,
|
||||
hard=hard,
|
||||
)
|
||||
except ClientError as e:
|
||||
return e.response["Error"]["Message"]
|
||||
@ -43,10 +28,9 @@ def index():
|
||||
@application.route("/question/<int:question_index>")
|
||||
def question(question_index):
|
||||
try:
|
||||
table = get_table("BibleQuestions")
|
||||
if question_index < table.item_count:
|
||||
response = get_question(table, question_index)
|
||||
item = json_util.loads(response["Item"])
|
||||
item_count = database.get_question_count()
|
||||
if question_index < item_count:
|
||||
item = database.get_question(question_index)
|
||||
return render_template(
|
||||
"index.html",
|
||||
title="Bible Trivia",
|
||||
|
36
database.py
Normal file
36
database.py
Normal file
@ -0,0 +1,36 @@
|
||||
from flask import current_app, g
|
||||
import boto3
|
||||
from boto3.dynamodb.conditions import Key, Attr
|
||||
import random
|
||||
from dynamodb_json import json_util
|
||||
|
||||
|
||||
def get_table(table_name):
|
||||
table = getattr(g, table_name, None)
|
||||
if table is None:
|
||||
dynamo = boto3.resource("dynamodb", aws_access_key_id=current_app.config["AWS_ACCESS_KEY_ID"],
|
||||
aws_secret_access_key=current_app.config["AWS_SECRET_ACCESS_KEY"], region_name=current_app.config["REGION_NAME"])
|
||||
setattr(g, table_name, dynamo.Table(table_name))
|
||||
return dynamo.Table(table_name)
|
||||
return table
|
||||
|
||||
|
||||
def get_question_count():
|
||||
table = get_table("BibleQuestions")
|
||||
return table.item_count
|
||||
|
||||
|
||||
def get_question(id):
|
||||
table = get_table("BibleQuestions")
|
||||
response = table.get_item(Key={"id": id})
|
||||
return json_util.loads(response["Item"])
|
||||
|
||||
|
||||
def get_questions_of_difficulty(difficulty):
|
||||
table = get_table("BibleQuestions")
|
||||
return table.scan(FilterExpression=Attr("Difficulty").eq(difficulty))
|
||||
|
||||
|
||||
def get_random_question_difficulty(difficulty):
|
||||
questions = get_questions_of_difficulty(difficulty)
|
||||
return questions["Items"][random.randint(0, questions["Count"]-1)]
|
@ -2,10 +2,20 @@
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.question {
|
||||
.question-text {
|
||||
font-size: 25px;
|
||||
}
|
||||
|
||||
#answer {
|
||||
.answer {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.container {
|
||||
width: auto;
|
||||
max-width: 680px;
|
||||
padding: 0 15px;
|
||||
}
|
||||
|
||||
.difficulty {
|
||||
margin: 10px;
|
||||
}
|
||||
|
@ -1,63 +1,129 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<html class="h-100" lang="en">
|
||||
<head>
|
||||
<title>{{ title }}</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/cyborg/bootstrap.min.css">
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container-md align-content-center">
|
||||
<body class="d-flex flex-column h-100">
|
||||
<main class="flex-shrink-0">
|
||||
<h1 class="title">Quiz The Word</h1>
|
||||
<div class="card question">
|
||||
<p id="question">{{ question }}</p>
|
||||
<button id="show-answer" class="btn btn-secondary" onclick="showAnswer()">Show Answer</button>
|
||||
<p id="answer">{{ answer }}</p>
|
||||
<div id="bible-verses"></div>
|
||||
<p>{{ difficulty }}</p>
|
||||
<div id="easy-question" class="container-md align-content-center question">
|
||||
<div class="card question-text">
|
||||
<p id="easy-question">{{ easy["Question"] }}</p>
|
||||
<button class="btn btn-secondary show-answer" onclick="showAnswer()">Show Answer</button>
|
||||
<p id="easy-answer" class="answer">{{ easy["Answer"] }}</p>
|
||||
<div id="easy-bible-verses"></div>
|
||||
<p>{{ easy["Difficulty"] }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="medium-question" class="container-md align-content-center question" style="display: none">
|
||||
<div class="card question-text">
|
||||
<p id="medium-question">{{ medium["Question"] }}</p>
|
||||
<button class="btn btn-secondary show-answer" onclick="showAnswer()">Show Answer</button>
|
||||
<p id="medium-answer" class="answer">{{ medium["Answer"] }}</p>
|
||||
<div id="medium-bible-verses"></div>
|
||||
<p>{{ medium["Difficulty"] }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div id="hard-question" class="container-md align-content-center question" style="display: none">
|
||||
<div class="card question-text">
|
||||
<p id="hard-question">{{ hard["Question"] }}</p>
|
||||
<button class="btn btn-secondary show-answer" onclick="showAnswer()">Show Answer</button>
|
||||
<p id="hard-answer" class="answer">{{ hard["Answer"] }}</p>
|
||||
<div id="hard-bible-verses"></div>
|
||||
<p>{{ hard["Difficulty"] }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center difficulty">
|
||||
<div class="btn-group btn-group-toggle" data-toggle="buttons" onclick="change_difficulty()">
|
||||
<label class="btn btn-secondary active">
|
||||
<input type="radio" name="difficulty" id="easy" value="easy" checked>Easy
|
||||
</label>
|
||||
<label class="btn btn-secondary">
|
||||
<input type="radio" name="difficulty" id="medium" value="medium">Medium
|
||||
</label>
|
||||
<label class="btn btn-secondary">
|
||||
<input type="radio" name="difficulty" id="hard" value="hard">Hard
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
<footer class="mt-auto py-3">
|
||||
<div class="container text-center">
|
||||
<span class="">Created By ItIsGood.com</span>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
<script>
|
||||
let raw_addresses = {{ addresses|tojson }};
|
||||
let addresses = [];
|
||||
for (let address of raw_addresses) {
|
||||
let verses = "";
|
||||
if (address["Verses"] != null) {
|
||||
for (let verse of address["Verses"]) {
|
||||
if (verses != "") {
|
||||
verses += ", ";
|
||||
} else {
|
||||
verses = ":";
|
||||
}
|
||||
if (typeof verse != "number") {
|
||||
verses += `${verse[0]}-${verse[1]}`;
|
||||
} else {
|
||||
verses += verse.toString();
|
||||
let easy_addresses = {{ easy["Address"]|tojson }};
|
||||
let medium_addresses = {{ medium["Address"]|tojson }};
|
||||
let hard_addresses = {{ hard["Address"]|tojson }};
|
||||
|
||||
function parse_bible_verses(bible_verses) {
|
||||
let addresses = [];
|
||||
for (let address of bible_verses) {
|
||||
let verses = "";
|
||||
if (address["Verses"] != null) {
|
||||
for (let verse of address["Verses"]) {
|
||||
if (verses != "") {
|
||||
verses += ", ";
|
||||
} else {
|
||||
verses = ":";
|
||||
}
|
||||
if (typeof verse != "number") {
|
||||
verses += `${verse[0]}-${verse[1]}`;
|
||||
} else {
|
||||
verses += verse.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (address["Chapter"] != null) {
|
||||
addresses.push(`${address["Book"]} ${address["Chapter"]}${verses}`);
|
||||
} else if (address["Book"] != null) {
|
||||
addresses.push(`${address["Book"]}`);
|
||||
}
|
||||
}
|
||||
if (address["Chapter"] != null) {
|
||||
addresses.push(`${address["Book"]} ${address["Chapter"]}${verses}`);
|
||||
} else if (address["Book"] != null) {
|
||||
addresses.push(`${address["Book"]}`);
|
||||
return addresses;
|
||||
}
|
||||
|
||||
function show_bible_verses(element_id, bible_verses) {
|
||||
let bibleVerses = $(element_id);
|
||||
if (bible_verses.length > 0) {
|
||||
for (let address of bible_verses) {
|
||||
if (address != bible_verses[0]) {
|
||||
bibleVerses.append(`; ${address}`);
|
||||
} else {
|
||||
bibleVerses.append(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let bibleVerses = $("#bible-verses");
|
||||
if (addresses.length > 0) {
|
||||
for (let address of addresses) {
|
||||
if (address != addresses[0]) {
|
||||
bibleVerses.append(`; ${address}`);
|
||||
} else {
|
||||
bibleVerses.append(address);
|
||||
}
|
||||
|
||||
show_bible_verses("#easy-bible-verses", parse_bible_verses(easy_addresses));
|
||||
show_bible_verses("#medium-bible-verses", parse_bible_verses(medium_addresses));
|
||||
show_bible_verses("#hard-bible-verses", parse_bible_verses(hard_addresses));
|
||||
|
||||
function change_difficulty() {
|
||||
let difficulty = $("input[name='difficulty']:checked").val();
|
||||
console.log(difficulty);
|
||||
$(".question").css("display", "none");
|
||||
if (difficulty === "easy") {
|
||||
$("#easy-question").css("display", "block");
|
||||
} else if (difficulty === "medium") {
|
||||
$("#medium-question").css("display", "block");
|
||||
} else if (difficulty === "hard") {
|
||||
$("#hard-question").css("display", "block");
|
||||
}
|
||||
}
|
||||
|
||||
function showAnswer() {
|
||||
$("#answer").css("display", "block");
|
||||
$("#show-answer").css("display", "none");
|
||||
$(".question:visible").find(".answer").css("display", "block");
|
||||
$(".show-answer:visible").css("display", "none");
|
||||
}
|
||||
</script>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user