From bc31348313c5cbb4354aef38a4a75ee381cf7a2e Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Sat, 6 Mar 2021 14:29:44 -0800 Subject: [PATCH] Move from AWS to GCP --- .ebignore | 8 --- .gcloudignore | 7 ++ .gitignore | 6 -- app.yaml | 1 + application.py | 52 -------------- config.py | 12 +++- database.py | 100 ++++++++++++++++++++------- main.py | 23 +++++++ requirements.txt | 15 ++-- static/style.css | 7 ++ templates/base.html | 25 +++++++ templates/hidden_answer.html | 66 ++++++++++++++++++ templates/index.html | 129 ----------------------------------- 13 files changed, 218 insertions(+), 233 deletions(-) delete mode 100644 .ebignore create mode 100644 .gcloudignore create mode 100644 app.yaml delete mode 100644 application.py create mode 100644 main.py create mode 100644 templates/base.html create mode 100644 templates/hidden_answer.html delete mode 100644 templates/index.html diff --git a/.ebignore b/.ebignore deleted file mode 100644 index 0a389e1..0000000 --- a/.ebignore +++ /dev/null @@ -1,8 +0,0 @@ -# Elastic Beanstalk Files -.elasticbeanstalk/* -!.elasticbeanstalk/*.cfg.yml -!.elasticbeanstalk/*.global.yml - -.idea/ -__pycache__/ -venv/ diff --git a/.gcloudignore b/.gcloudignore new file mode 100644 index 0000000..e6605a4 --- /dev/null +++ b/.gcloudignore @@ -0,0 +1,7 @@ +.idea/ +__pycache__/ +venv/ +.gcloudignore +.git +.gitignore +*.tsv \ No newline at end of file diff --git a/.gitignore b/.gitignore index c7fcc5e..a023bfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,3 @@ - -# Elastic Beanstalk Files -.elasticbeanstalk/* -!.elasticbeanstalk/*.cfg.yml -!.elasticbeanstalk/*.global.yml - .env .idea/ __pycache__/ diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..ca36be9 --- /dev/null +++ b/app.yaml @@ -0,0 +1 @@ +runtime: python39 \ No newline at end of file diff --git a/application.py b/application.py deleted file mode 100644 index 934164d..0000000 --- a/application.py +++ /dev/null @@ -1,52 +0,0 @@ -from flask import Flask, render_template, redirect, url_for -from botocore.exceptions import ClientError -import database - -application = Flask(__name__) -application.config.from_pyfile("config.py") - - -@application.route("/") -def index(): - try: - 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="Quiz The Word", - easy=easy, - medium=medium, - hard=hard, - ) - except ClientError as e: - return e.response["Error"]["Message"] - except Exception as e: - return str(e) - - -@application.route("/question/") -def question(question_index): - try: - 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", - 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 index eaebaf6..d8fd835 100644 --- a/config.py +++ b/config.py @@ -5,6 +5,12 @@ from dotenv import load_dotenv load_dotenv(".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") +DB_USER = environ.get("DB_USER") +DB_PASSWORD = environ.get("DB_PASSWORD") +DB_HOST = environ.get("DB_HOST") +DB_PORT = environ.get("DB_PORT") +DB_NAME = environ.get("DB_NAME") +DB_SOCKET_DIR = environ.get("DB_SOCKET_DIR", "/cloudsql") +CLOUD_SQL_CONNECTION_NAME = environ.get("CLOUD_SQL_CONNECTION_NAME") +# DB_URL = f"postgres+psycopg2://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}?host={DB_SOCKET_DIR}/{CLOUD_SQL_CONNECTION_NAME}" +DB_URL = f"postgres+psycopg2://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}" diff --git a/database.py b/database.py index 58323b3..0b686fe 100644 --- a/database.py +++ b/database.py @@ -1,36 +1,86 @@ from flask import current_app, g -import boto3 -from boto3.dynamodb.conditions import Key, Attr -import random -from dynamodb_json import json_util +import sqlalchemy +from sqlalchemy import Column, JSON, String, Integer, create_engine, ForeignKey, func +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.ext.associationproxy import association_proxy +from sqlalchemy.orm import sessionmaker, relationship +from config import DB_URL + +engine = create_engine(DB_URL) +Session = sessionmaker(bind=engine) +Base = declarative_base() -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 +class AllQuestions(Base): + __tablename__ = "all_questions" + + question_id = Column(Integer, primary_key=True, nullable=False) + question = Column(String) + answer = Column(String) + addresses = Column(String) + + def __repr__(self): + return f"" -def get_question_count(): - table = get_table("BibleQuestions") - return table.item_count +class HiddenAnswer(Base): + __tablename__ = "category_hidden_answer" + + question_id = Column(Integer, ForeignKey("all_questions.question_id"), primary_key=True) + difficulty = Column(Integer) + hint = Column(JSON) + + all_question_relationship = relationship("AllQuestions", primaryjoin="HiddenAnswer.question_id==AllQuestions.question_id", lazy="joined", uselist=False) + question = association_proxy("all_question_relationship", "question") + answer = association_proxy("all_question_relationship", "answer") + addresses = association_proxy("all_question_relationship", "addresses") + + def __repr__(self): + return f"