habitmove/loop/repetitions.py

40 lines
1.2 KiB
Python
Raw Normal View History

2021-08-28 21:03:12 +00:00
import sqlite3
def migrate(db, habitlist, events):
c = db.cursor()
for event in events:
text = event["note"]
for habit in habitlist:
tag = f"#{habit['description']}"
if tag in text:
print(f"register: {habit['name']} in {text} at {event['end']}")
2021-08-29 08:13:28 +00:00
add_to_database(c, fetch_habit_id(c, habit), event["end"])
def fetch_habit_id(c, habit):
c.execute("select id from Habits where uuid = ?", [(habit["uuid"])])
return c.fetchone()[0]
2021-08-28 21:03:12 +00:00
# does not do:
# ranges in habits (should check for habit being range, then taking #habit(3) number into account)
# also, range trackers: multiply value by 1,000
# non-range habits but still #habit(3) number included, adding multiple?
2021-08-29 08:13:28 +00:00
def add_to_database(cursor, habit_id, timestamp, value=2):
try:
cursor.execute(
"""
INSERT INTO
Repetitions(id, habit, timestamp, value)
VALUES (NULL, ?, ?, ?)
""",
(habit_id, timestamp, value),
)
except sqlite3.IntegrityError:
# TODO better error handling
print("fail to register: not unique")
pass