Skip to content

Commit 6f140c9

Browse files
committed
Add docstrings
1 parent bfdb729 commit 6f140c9

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

app.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
A task list app written in Flask with levels and experience points (XP).
3+
"""
4+
15
import calendar
26
from datetime import datetime, timedelta, date
37
import math
@@ -20,6 +24,10 @@
2024

2125

2226
class User(db.Model):
27+
"""
28+
A user model with information to store the level and experience points (XP).
29+
"""
30+
2331
id: int = db.Column(
2432
db.Integer, primary_key=True, unique=True, nullable=False
2533
) # user id
@@ -63,6 +71,10 @@ class User(db.Model):
6371
) # user last task completion ID
6472

6573
def add_xp(self, amount: float) -> None: # add XP
74+
"""
75+
Add XP (experience points) to the user.
76+
amount - the amount to add XP.
77+
"""
6678
self.xp += amount # add XP by amount
6779
self.total_xp += amount # add total XP by amount
6880
flash(
@@ -72,6 +84,9 @@ def add_xp(self, amount: float) -> None: # add XP
7284
self.check_level_up() # check if user has leveled up
7385

7486
def check_level_up(self) -> None: # check if user has leveled up
87+
"""
88+
Check if the user has leveled up.
89+
"""
7590
while (
7691
self.xp >= self.xp_required
7792
): # if user XP is greater than or equal to XP required
@@ -87,6 +102,10 @@ def check_level_up(self) -> None: # check if user has leveled up
87102

88103

89104
class Task(db.Model):
105+
"""
106+
A task model with information to store the name, due date, priority, difficulty, repeat interval, repeat often, and completion status.
107+
"""
108+
90109
id: int = db.Column(
91110
db.Integer, primary_key=True, unique=True, nullable=False
92111
) # task id
@@ -180,6 +199,9 @@ def short_numeric_filter(
180199

181200
@app.route("/")
182201
def index() -> str: # get index page template
202+
"""
203+
Return the index page with tasks, users and today's date.
204+
"""
183205
tasks: list = Task.query.order_by(
184206
Task.due_date
185207
).all() # get the list of tasks sorted by due date
@@ -194,6 +216,9 @@ def index() -> str: # get index page template
194216

195217
@app.route("/add", methods=["POST"])
196218
def add_task() -> Response: # add the task to the task list
219+
"""
220+
Add a new task to the task list.
221+
"""
197222
name: str = request.form.get("name") # get name from request form
198223
due_date: str = request.form.get("due_date") # get due date
199224
priority = int(request.form.get("priority")) # get priority
@@ -222,6 +247,10 @@ def add_task() -> Response: # add the task to the task list
222247

223248
@app.route("/complete_task/<int:task_id>")
224249
def complete_task(task_id) -> Response: # complete task from task id
250+
"""
251+
Complete the task with the given task ID.
252+
task_id - the ID of the task to complete.
253+
"""
225254
task: Union[Task, None] = Task.query.get(task_id) # get task by task id
226255
if task is not None: # if task exists
227256
due_multiplier: float = 1.0 # set default due multiplier to 1
@@ -369,6 +398,10 @@ def complete_task(task_id) -> Response: # complete task from task id
369398

370399
@app.route("/delete_task/<int:task_id>")
371400
def delete_task(task_id) -> Response: # delete task from task id
401+
"""
402+
Delete the task based on the task ID.
403+
task_id - the ID of the task to delete.
404+
"""
372405
task: Union[Task, None] = Task.query.get(task_id) # get task by task id
373406
if task is not None: # if task exists
374407
db.session.delete(task) # delete task from task list
@@ -379,6 +412,13 @@ def delete_task(task_id) -> Response: # delete task from task id
379412
def calculate_next_recurring_event(
380413
original_date: date, times_completed: int, repeat_interval: int, repeat_often: int
381414
) -> date: # calculate the next recurring event date
415+
"""
416+
Calculate the next recurring event date based on the original date, times completed, repeat interval, and repeat often.
417+
original_date - the original date of the recurring event.
418+
times_completed - the number of times the recurring event has been completed.
419+
repeat_interval - the interval at which the recurring event repeats.
420+
repeat_often - the frequency at which the recurring event repeats.
421+
"""
382422
if repeat_often == 1: # if task repeat often is daily
383423
return original_date + timedelta(
384424
days=repeat_interval * times_completed
@@ -418,6 +458,9 @@ def calculate_next_recurring_event(
418458

419459

420460
def init_db() -> None: # initialize database
461+
"""
462+
Initialize the user and task database.
463+
"""
421464
with app.app_context():
422465
db.create_all() # create tables if they don't exist
423466
if "tasks_completed" not in [

0 commit comments

Comments
 (0)