Skip to content

Commit ab4ab75

Browse files
committed
Add daily streaks
1 parent cb37706 commit ab4ab75

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

app.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class User(db.Model):
2727
tasks_completed = db.Column(
2828
db.Integer, default=0, nullable=False
2929
) # number of times tasks has completed
30+
last_completion_date = db.Column(
31+
db.Date, default=datetime.now().date, nullable=False
32+
) # user last task completion date
33+
daily_streak = db.Column(
34+
db.Integer, default=0, nullable=False
35+
) # user daily task streak
3036

3137
def add_xp(self, amount): # add XP
3238
self.xp += amount # add XP by amount
@@ -239,6 +245,7 @@ def complete_task(task_id): # complete task from task id
239245
* (1 + math.log(max(task.times_completed, 1)))
240246
* (1 + math.log(max(user.tasks_completed, 1)))
241247
* (1 + math.log(max(active_tasks, 1)))
248+
* (1 + user.daily_streak / 10)
242249
)
243250
) # add XP
244251
db.session.commit() # commit database changes
@@ -295,9 +302,6 @@ def calculate_next_recurring_event(
295302
def init_db(): # initialize database
296303
with app.app_context():
297304
db.create_all() # create tables if they don't exist
298-
today = datetime.now().strftime(
299-
"%Y-%m-%d"
300-
) # get today's date in YYYY-MM-DD format
301305
if "tasks_completed" not in [
302306
column["name"] for column in db.inspect(db.engine).get_columns("user")
303307
]: # check if tasks completed column is not in user table
@@ -306,6 +310,21 @@ def init_db(): # initialize database
306310
"ALTER TABLE user ADD COLUMN tasks_completed INT NOT NULL DEFAULT 0"
307311
)
308312
) # create tasks completed column
313+
if "last_completion_date" not in [
314+
column["name"] for column in db.inspect(db.engine).get_columns("user")
315+
]: # check if last completion date column is not in user table
316+
db.session.execute(
317+
text(
318+
"ALTER TABLE user ADD COLUMN last_completion_date DATE NOT NULL DEFAULT CURRENT_DATE"
319+
)
320+
) # create last completion date column
321+
db.session.commit() # commit database changes
322+
if "daily_streak" not in [
323+
column["name"] for column in db.inspect(db.engine).get_columns("user")
324+
]: # check if tasks completed column is not in user table
325+
db.session.execute(
326+
text("ALTER TABLE user ADD COLUMN daily_streak INT NOT NULL DEFAULT 0")
327+
) # create tasks completed column
309328
if User.query.count() == 0: # if there is no users
310329
new_user = User(username="Player") # create new user
311330
db.session.add(new_user) # add new user to database
@@ -314,29 +333,19 @@ def init_db(): # initialize database
314333
column["name"] for column in db.inspect(db.engine).get_columns("task")
315334
]: # check if original due date column is not in task table
316335
db.session.execute(
317-
text("ALTER TABLE task ADD COLUMN original_due_date DATE")
336+
text(
337+
"ALTER TABLE task ADD COLUMN original_due_date DATE NOT NULL DEFAULT CURRENT_DATE"
338+
)
318339
) # create original due date column
319-
db.session.execute(
320-
text("UPDATE task SET original_due_date = ?"), (today)
321-
) # update existing rows
322-
db.session.commit() # commit database changes
323-
db.session.execute(
324-
text("ALTER TABLE task ALTER COLUMN original_due_date SET NOT NULL")
325-
) # set column to not null
326340
db.session.commit() # commit database changes
327341
if "due_date" not in [
328342
column["name"] for column in db.inspect(db.engine).get_columns("task")
329343
]: # check if due date column is not in task table
330344
db.session.execute(
331-
text("ALTER TABLE task ADD COLUMN due_date DATE")
345+
text(
346+
"ALTER TABLE task ADD COLUMN due_date DATE NOT NULL DEFAULT CURRENT_DATE"
347+
)
332348
) # create due date column
333-
db.session.execute(
334-
text("UPDATE task SET due_date = ?"), (today)
335-
) # update existing rows
336-
db.session.commit() # commit database changes
337-
db.session.execute(
338-
text("ALTER TABLE task ALTER COLUMN due_date SET NOT NULL")
339-
) # set column to not null
340349
db.session.commit() # commit database changes
341350
if "priority" not in [
342351
column["name"] for column in db.inspect(db.engine).get_columns("task")

0 commit comments

Comments
 (0)