Add habit duplicate checking

This commit is contained in:
Marty Oehme 2021-08-28 10:35:29 +02:00
parent 52fa6474ad
commit 8d4c32a652
Signed by: Marty
GPG key ID: B7538B8F50A1C800
2 changed files with 41 additions and 16 deletions

View file

@ -1,32 +1,56 @@
def migrate(db, trackers): def migrate(db, trackers):
c = db.cursor() c = db.cursor()
tracker = trackers["cigarette"] habits = trackers_to_habits(trackers)
habit_dict = { for habit in habits:
"archived": 0, existing_habit = check_habit_duplicate(c, habit)
"color": 11, if not existing_habit:
"name": tracker["label"], add_to_database(c, habit)
"description": "smoking", else:
"freq_den": 1, print(f"Found duplicate Habit: {existing_habit} - skipping.")
"freq_num": 1,
"highlight": 0,
"position": 4,
"question": "Did you smoke today punk?",
"uuid": tracker["id"],
}
add_to_database(c, habit_dict)
# DEBUGGING # DEBUGGING
c.execute("""select * from Habits""") c.execute("""select * from Habits""")
print(c.fetchall()) print(c.fetchall())
def trackers_to_habits(trackers):
habits = []
# for each list entry tracker dict transform to habit dict
for tracker_name in trackers.keys():
t = trackers[tracker_name]
habits.append(
{
"archived": 0 if t["hidden"] == False else 1,
"color": 11,
"description": t["label"],
"freq_den": 1,
"freq_num": 1,
"highlight": 0,
"name": f"{t['emoji']} {t['label']}",
"question": "",
"unit": "" if t["uom"] == "num" else t["uom"],
"uuid": t["id"],
}
# FIXME conditional for ranges (if type = range > habits[-1]['type'] = intforrangetype; habits[-1]['max'] =)
)
return habits
def check_habit_duplicate(cursor, habit):
cursor.execute("select name from Habits where uuid = ?", [habit["uuid"]])
name = cursor.fetchone()
if name:
return name[0]
return False
def add_to_database(cursor, habit_dict): def add_to_database(cursor, habit_dict):
placeholder = ", ".join("?" * len(habit_dict)) placeholder = ", ".join("?" * len(habit_dict))
columns = ", ".join(habit_dict.keys()) columns = ", ".join(habit_dict.keys())
values = list(habit_dict.values())
sql = "insert into `{table}` ({columns}) values ({values});".format( sql = "insert into `{table}` ({columns}) values ({values});".format(
table="Habits", columns=columns, values=placeholder table="Habits", columns=columns, values=placeholder
) )
values = list(habit_dict.values())
cursor.execute(sql, values) cursor.execute(sql, values)

View file

@ -2,6 +2,8 @@
import json import json
import sys import sys
# TODO is pandas necessary? should only be for csv, not built for now
import pandas as pd import pandas as pd
import loop.migration as loop import loop.migration as loop
@ -45,7 +47,6 @@ def verify_continue(data):
if __name__ == "__main__": if __name__ == "__main__":
# load nomie json # load nomie json
nomie_data = load_file(sys.argv[1]) nomie_data = load_file(sys.argv[1])
# DISABLED FOR DEBUGGING
verify_continue(nomie_data) verify_continue(nomie_data)
trackers = nomie_data["trackers"] trackers = nomie_data["trackers"]
events = nomie_data["events"] events = nomie_data["events"]