Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kirkman central db #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
5 changes: 1 addition & 4 deletions api/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

app = Flask(__name__)

# Import credentials module
from app import creds

# import endpoint packages
from app import test
from app import tool
from app import index
from app.admin.members import members
from app import vend
#from app import vend
42 changes: 7 additions & 35 deletions api/app/admin/members/members.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,26 @@
from app import app
import sqlalchemy
import urllib.parse
from flask import jsonify, request
import db

from app.creds import db_password, DEVICE, DEVICE_KEY

#? is there a way for use to move this out of this file?
# I think a lot of files will be using this data along with the `url` var and db connection.
# can we do this in __init__.py?
user = "access"
host = "honeycomb.at.hive13.org"
dbname = "door"
driver = "pg8000"

# designate url
# format is: dialect+driver://username:password@host:port/database
url = "postgresql+{}://{}:{}@{}/{}".format(
driver,
user,
urllib.parse.quote_plus(db_password),
host,
dbname,
)

# create sqlalchemy engine
# ? Should echo be True here?
engine = sqlalchemy.create_engine(url, echo=True)

# Bind to engine & connect (comment out for dev outisde of hive):
db.metadata.bind = engine
conn = engine.connect()
from db import db_conn

# member profile
@app.route('/api/admin/members/profile/', methods=['GET', 'POST'])
@app.route('/api/admin/members/profile', methods=['GET', 'POST'])
def members():
req_data = request.get_json()

member_id = req_data['member_id']
# build query, execute on get request
s = sqlalchemy.text("""SELECT *
FROM members
s = sqlalchemy.text("""SELECT lname, fname, phone, created_at
FROM public.members
WHERE member_id = :x
""")

# try to execute the query, except when it returns an error.
try:
result = conn.execute(s, x=str(member_id))
result = db_conn.execute(s, x=str(member_id))
rows = [dict(row) for row in result]
return jsonify(rows)
except:
error_message = [{"result": 0, "message": "Invalid Member ID"}]
except Exception as ex:
error_message = [{"result": 0, "message": str(ex)}]
return jsonify(error_message)
4 changes: 4 additions & 0 deletions api/app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
db_host = 'honeycomb.at.hive13.org'
db_user = 'access'
db_name = 'door'
db_driver = 'pg8000'
23 changes: 23 additions & 0 deletions api/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Import global config
from db import config

# Import credentials module
from app import creds

# Initialize database
import sqlalchemy
from db import db
import urllib

db_url = "postgresql+{}://{}:{}@{}/{}".format(
config.db_driver,
config.db_user,
urllib.parse.quote_plus(creds.db_password),
config.db_host,
config.db_name,
)

db_engine = sqlalchemy.create_engine(db_url, echo=True)

db.metadata.bind = db_engine
db_conn = db_engine.connect()
4 changes: 4 additions & 0 deletions api/db/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
db_host = 'honeycomb.at.hive13.org'
db_user = 'access'
db_name = 'door'
db_driver = 'pg8000'
File renamed without changes.