diff --git a/application.py b/application.py index e6e7306..934164d 100644 --- a/application.py +++ b/application.py @@ -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/") 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", diff --git a/database.py b/database.py new file mode 100644 index 0000000..58323b3 --- /dev/null +++ b/database.py @@ -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)] diff --git a/static/style.css b/static/style.css index af1c1f9..32b44e6 100644 --- a/static/style.css +++ b/static/style.css @@ -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; +} diff --git a/templates/index.html b/templates/index.html index aac71f0..e1e2df5 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,63 +1,129 @@ - + {{ title }} + - + + - -
+ +

Quiz The Word

-
-

{{ question }}

- -

{{ answer }}

-
-

{{ difficulty }}

+
+
+

{{ easy["Question"] }}

+ +

{{ easy["Answer"] }}

+
+

{{ easy["Difficulty"] }}

+
-
+ + +
+
+ + + +
+
+
+
+
+ Created By ItIsGood.com +
+
\ No newline at end of file