commit f9ea73071123d5076482ebf0565a5a7d861cf208 Author: Matthew Welch Date: Sat Nov 28 22:12:24 2020 -0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2feb93a --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ + +# Elastic Beanstalk Files +.elasticbeanstalk/* +!.elasticbeanstalk/*.cfg.yml +!.elasticbeanstalk/*.global.yml + +.env +.idea/ +__pycache__/ diff --git a/application.py b/application.py new file mode 100644 index 0000000..e6e7306 --- /dev/null +++ b/application.py @@ -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/") +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 "

Question not found.

" + except ClientError as e: + return e.response["Error"]["Message"] + except Exception as e: + return str(e) + + +if __name__ == "__main__": + application.run() diff --git a/config.py b/config.py new file mode 100644 index 0000000..3c6b608 --- /dev/null +++ b/config.py @@ -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") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..93b1557 --- /dev/null +++ b/requirements.txt @@ -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 diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..af1c1f9 --- /dev/null +++ b/static/style.css @@ -0,0 +1,11 @@ +.title { + text-align: center; +} + +.question { + font-size: 25px; +} + +#answer { + display: none; +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..aac71f0 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,63 @@ + + + + {{ title }} + + + + + + +
+

Quiz The Word

+
+

{{ question }}

+ +

{{ answer }}

+
+

{{ difficulty }}

+
+
+ + + \ No newline at end of file