69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
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()
|