From f4de636a450c374329cf786432b4fe1e836d8d2c Mon Sep 17 00:00:00 2001 From: Jake Funke Date: Tue, 5 Jun 2018 00:02:01 +0000 Subject: [PATCH] todos updated --- botany.py | 9 ++++++ testsql.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 testsql.py diff --git a/botany.py b/botany.py index c634fdf..59d05ad 100755 --- a/botany.py +++ b/botany.py @@ -225,6 +225,12 @@ class Plant(object): if os.path.isfile(visitor_filepath): with open(visitor_filepath, 'r') as visitor_file: data = json.load(visitor_file) + # TODO: this needs to check if the latest timestamp is greater + # than 5 days + # need to check for delta of at minimum 5 days between waters + # to make sure plant is alive + # check each visitor time, calculate delta between this and last + # watering if data: for element in data: if element['user'] not in self.visitors: @@ -233,6 +239,9 @@ class Plant(object): visitors_this_check.append(element['user']) # prevent users from manually setting watered_time in the future if element['timestamp'] < int(time.time()): + # need to check here for delta between this + # element and last element (also json load might + # not be sorted... if element['timestamp'] > latest_timestamp: latest_timestamp = element['timestamp'] try: diff --git a/testsql.py b/testsql.py new file mode 100644 index 0000000..7692ced --- /dev/null +++ b/testsql.py @@ -0,0 +1,83 @@ +import sqlite3 + +garden_db_path = "sqlite/garden_db.sqlite" + +def init_database(): + #TODO: does this need permissions? + conn = sqlite3.connect(garden_db_path) + init_table_string = """CREATE TABLE IF NOT EXISTS garden ( + plant_id tinytext PRIMARY KEY, + owner text, + description text, + age text, + score integer, + is_dead text + )""" + + c = conn.cursor() + c.execute(init_table_string) + conn.close() +def update_garden_db(): + # insert or update this plant id's entry in DB (should happen + # regularly) + # TODO: create a second function that is called to retrieve garden + # when called by display controller + + + conn = sqlite3.connect(garden_db_path) + c = conn.cursor() + ##try to insert or replace + update_query = """INSERT OR REPLACE INTO garden ( + plant_id, owner, description, age, score, is_dead + ) VALUES ( + '{pid}', '{pown}', '{pdes}', '{page}', {psco}, '{pdead}' + ) + """.format(pid = "asdfaseeeedf", pown = "jaeeke", pdes = "bigger ceeooler plant", page="28dee", psco = str(244400), pdead = str(False)) + # update_query = """INSERT INTO garden ( + # plant_id, owner, description, age, score, is_dead + # ) VALUES ( + # '{pid}', '{pown}', '{pdes}', '{page}', {psco}, '{pdead}' + # ) + # """.format(pid = "asdfasdf", pown = "jake", pdes = "big cool plant", page="25d", psco = str(25), pdead = str(False)) + + print c.execute(update_query) + conn.commit() + conn.close() + #print "bigggg booom" + +def retrieve_garden_from_db(garden_db_path): + # Builds a dict of dicts from garden sqlite db + garden_dict = {} + conn = sqlite3.connect(garden_db_path) + conn.row_factory = sqlite3.Row + c = conn.cursor() + c.execute('SELECT * FROM garden ORDER BY owner') + tuple_list = c.fetchall() + conn.close() + # Building dict from table rows + for item in tuple_list: + garden_dict[item[0]] = { + "owner":item[1], + "description":item[2], + "age":item[3], + "score":item[4], + "dead":item[5], + } + return garden_dict + +#init_database() +#update_garden_db() +results = retrieve_garden_from_db(garden_db_path) +print results + + +# con = sqlite3.connect(garden_db_path) # +# con.row_factory = sqlite3.Row # +# cur = con.cursor() # +# cur.execute("select * from garden ORDER BY score desc") # +# blah = cur.fetchall() # +# con.close() +# print blah + + +