Initial commit

This commit is contained in:
Matthew Welch 2020-11-28 22:12:24 -08:00
commit f9ea730711
6 changed files with 168 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
.env
.idea/
__pycache__/

68
application.py Normal file
View File

@ -0,0 +1,68 @@
from flask import Flask, render_template, redirect, url_for
from botocore.exceptions import ClientError
import boto3
import random
from dynamodb_json import json_util
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))
return render_template(
"index.html",
title="Bible Trivia",
question=item["Question"],
answer=item["Answer"],
addresses=item["Address"],
category=item["Category"],
difficulty=item["Difficulty"],
)
except ClientError as e:
return e.response["Error"]["Message"]
except Exception as e:
return str(e)
@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"])
return render_template(
"index.html",
title="Bible Trivia",
question=item["Question"],
answer=item["Answer"],
addresses=item["Address"],
category=item["Category"],
difficulty=item["Difficulty"],
)
else:
return "<h1>Question not found.</h1>"
except ClientError as e:
return e.response["Error"]["Message"]
except Exception as e:
return str(e)
if __name__ == "__main__":
application.run()

12
config.py Normal file
View File

@ -0,0 +1,12 @@
from os import environ
from pathlib import Path
from dotenv import load_dotenv
basedir = Path(__file__)
load_dotenv(str(basedir / ".env"))
SECRET_KEY = environ.get("SECRET_KEY")
AWS_ACCESS_KEY_ID = environ.get("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = environ.get("AWS_SECRET_ACCESS_KEY")
REGION_NAME = environ.get("REGION_NAME")

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
boto3==1.16.18
botocore==1.19.18
Flask==1.1.2
dynamodb-json==1.3
python-dotenv>=0.15.0

11
static/style.css Normal file
View File

@ -0,0 +1,11 @@
.title {
text-align: center;
}
.question {
font-size: 25px;
}
#answer {
display: none;
}

63
templates/index.html Normal file
View File

@ -0,0 +1,63 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ title }}</title>
<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>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<div class="container-md align-content-center">
<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>
</div>
</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();
}
}
}
if (address["Chapter"] != null) {
addresses.push(`${address["Book"]} ${address["Chapter"]}${verses}`);
} else if (address["Book"] != null) {
addresses.push(`${address["Book"]}`);
}
}
let bibleVerses = $("#bible-verses");
if (addresses.length > 0) {
for (let address of addresses) {
if (address != addresses[0]) {
bibleVerses.append(`; ${address}`);
} else {
bibleVerses.append(address);
}
}
}
function showAnswer() {
$("#answer").css("display", "block");
$("#show-answer").css("display", "none");
}
</script>
</html>