Skip to content

Commit

Permalink
Updated to v2.0, all basic features should work for multiple users in…
Browse files Browse the repository at this point in the history
… production
  • Loading branch information
AdditionalPylons committed Jul 17, 2020
1 parent bc31e77 commit 81a7bd1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 74 deletions.
122 changes: 62 additions & 60 deletions database_commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Standardlib Imports
from contextlib import contextmanager
import datetime
import os

Expand Down Expand Up @@ -52,77 +53,78 @@
"""


@contextmanager
def connect():
connection = pymysql.connect(
host=instance1_host, port=instance1_port, user=instance1_user, password=instance1_password, database=database)
connection.close()

try:
yield connection
finally:
connection.commit()
connection.close()

def query_all():
connection = pymysql.connect(
host=instance1_host, port=instance1_port, user=instance1_user, password=instance1_password, database=database)
cursor = connection.cursor()
cursor.execute("SELECT * FROM expenses")
print(cursor.fetchall())
connection.commit()
connection.close()

with connect() as con:
with con.cursor() as cursor:
cursor.execute("SELECT * FROM expenses")
print(cursor.fetchall())

def query_all_today():
connection = pymysql.connect(
host=instance1_host, port=instance1_port, user=instance1_user, password=instance1_password, database=database)
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM expenses WHERE date = '{today}'")
print(cursor.fetchall())
connection.commit()
connection.close()


def query_spent_today():
connection = pymysql.connect(
host=instance1_host, port=instance1_port, user=instance1_user, password=instance1_password, database=database)
cursor = connection.cursor()
cursor.execute(f"SELECT SUM(cost) FROM expenses WHERE date = '{today}'")
result = cursor.fetchone()[0]
if result is None:
return 0
else:
return result
connection.commit()
connection.close()

with connect() as con:
with con.cursor() as cursor:
cursor.execute(f"SELECT * FROM expenses WHERE date = '{today}'")
print(cursor.fetchall())

def query_spent(user_id, date=today):
with connect() as con:
with con.cursor() as cursor:
cursor.execute(f"""SELECT SUM(cost) FROM expenses
WHERE user_id = {user_id} AND date = '{date}'""")
result = cursor.fetchone()[0]
if result is None:
return 0
else:
return result

def add_entry(user_id, date, cost, item='NULL', location='NULL'):
connection = pymysql.connect(
host=instance1_host, port=instance1_port, user=instance1_user, password=instance1_password, database=database)
cursor = connection.cursor()
cursor.execute(
f"""INSERT INTO {table}{table_columns}
VALUES('{user_id}','{date}', {cost}, '{item}', '{location}')""")
connection.commit()
connection.close()
with connect() as con:
with con.cursor() as cursor:
cursor.execute(
f"""INSERT INTO {table}{table_columns}
VALUES('{user_id}','{date}', {cost}, '{item}', '{location}')""")

# Returns a list for easy concat with parse_expense
def get_user_id(phone_number):
connection = pymysql.connect(
host=instance1_host, port=instance1_port, user=instance1_user, password=instance1_password, database=database)
cursor = connection.cursor()
cursor.execute(f"SELECT user_id FROM users WHERE phone_number = '{phone_number}'")
result = cursor.fetchone()
if result is None:
raise Exception(f"No user ID found for phone number '{phone_number}'")
else:
return list(result)
connection.commit()
connection.close()

with connect() as con:
with con.cursor() as cursor:
cursor.execute(f"SELECT user_id FROM users WHERE phone_number = '{phone_number}'")
result = cursor.fetchone()
if result is None:
raise Exception(f"No user ID found for phone number '{phone_number}'")
else:
return list(result)

def get_budget(user_id,period):
with connect() as con:
with con.cursor() as cursor:
cursor.execute(
f"""SELECT {period}_budget FROM users WHERE user_id = '{user_id}'""")
result = cursor.fetchone()
#arbitrarily large non-infinite number used for non-existent budget
if result is None:
return 100**100
else:
return result

def set_budget(user_id,period,amount):
with connect() as con:
with con.cursor() as cursor:
cursor.execute(
f"""UPDATE users SET {period}_budget = {amount}
WHERE user_id = {user_id}""")

def custom_query(query):
connection = pymysql.connect(
host=instance1_host, port=instance1_port, user=instance1_user, password=instance1_password, database=database)
cursor = connection.cursor()
cursor.execute(query)
print(cursor.fetchall())
connection.commit()
connection.close()
with connect() as con:
with con.cursor() as cursor:
cursor.execute(query)
print(cursor.fetchall())
7 changes: 4 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ def ingest_message():
message_body = request.form['Body']
response = MessagingResponse()

user_id = get_user_id(number)
try:
# USAGE NOTE: When using add_entry with parse_expense,
# you MUST unpack the resulting list with * as follows:
# add_entry(*parse_expense('123 for some thing at some place'))
if message_body in shortcuts:
add_entry(*get_user_id(number)+parse_expense(shortcuts[message_body]))
add_entry(*user_id+parse_expense(shortcuts[message_body]))
else:
add_entry(*get_user_id(number)+parse_expense(message_body))
add_entry(*user_id+parse_expense(message_body))
response.message('Entry successfully added!')
check_alerts()
except Exception:
response.message('Failed to add entry.')
finally:
check_alerts(user_id, number)
return str(response)


Expand Down
2 changes: 1 addition & 1 deletion parsing_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def parse_expense(input):
r'(?: at (?P<location>.+?))?'
# Everything after "at" = location, optional
r'(?: on (?P<date>.+))?$'
# Everything after "at" = location, optional
# Everything after "on" = date, optional
)
if re.search('(?: on (?P<date>.+))$', input):
parsed_result = list(sms_syntax.search(input).group(
Expand Down
17 changes: 7 additions & 10 deletions send_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
client = Client(account_sid, auth_token)


def custom_notif(notif):
def custom_notif(phone_number,notif):

client.messages.create(

to=my_cell_number,
from_=twilio_number,
to=phone_number,
from_=TWILIO_NUMBER,
body=notif

)


def check_alerts():
budget = 300
spent = int(query_spent_today())
def check_alerts(user_id, phone_number, peroid='daily'):
budget = int(get_budget(user_id, peroid))
spent = int(query_spent(user_id))
remaining = budget - spent
infinity = float("inf")

Expand All @@ -43,7 +43,4 @@ def check_alerts():

for (a, b) in alerts.keys():
if a <= spent < b:
custom_notif(alerts[(a, b)])


check_alerts()
custom_notif(phone_number, alerts[(a, b)])

0 comments on commit 81a7bd1

Please sign in to comment.