-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathweekly_tasks.py
More file actions
177 lines (150 loc) · 5.63 KB
/
weekly_tasks.py
File metadata and controls
177 lines (150 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from datetime import date, datetime, time
from typing import Iterator, Optional
from app.internal.security.schema import CurrentUser
from app.database.models import User, Task, WeeklyTask
from sqlalchemy.orm.session import Session
def check_inputs(days: str, task_time: time, title: str) -> bool:
"""Checks inputs, used by the weekly_task_from_input function"""
return days and task_time and title
def weekly_task_from_input(
user: User,
title: Optional[str],
days: str,
content: Optional[str],
task_time: Optional[time],
is_important: bool,
weekly_task_id: int = 0,
) -> WeeklyTask:
"""This function is being used to make a Weekly Task model
from the inputs.
Args:
user (User): The user who wants to make or edit a Weekly Task.
title (str): Title of the Weekly Task.
days (str): Return days of the Weekly Task.
content (str): Content of the Weekly Task.
task_time (time): Return time of the Weekly Task.
is_important (bool): If the task is important.
weekly_task_id (int): The id of the weekly task, zero if not mentioned.
Returns:
WeeklyTask: the model WeeklyTask which the function managed to make.
"""
if isinstance(user, CurrentUser):
user_id = user.user_id
else:
user_id = user.id
weekly_task = WeeklyTask(
title=title,
content=content,
is_important=is_important,
user_id=user_id,
)
if weekly_task_id != 0:
weekly_task.id = weekly_task_id
inputs_ok = check_inputs(days, task_time, title)
if not inputs_ok:
return weekly_task
weekly_task.set_days(days)
weekly_task.task_time = task_time.strftime("%H:%M")
return weekly_task
def create_weekly_task(
weekly_task: WeeklyTask,
session: Session,
) -> bool:
"""This function is being used to add a Weekly Task to the user.
Args:
user (User): The user who wants to add the Weekly Task.
session (Session): The session to redirect to the database.
weekly_task (WeeklyTask): The Weekly Task that the user will add.
Returns:
bool: Shows if the weekly_task has been added to the db.
"""
inputs_ok = check_inputs(
weekly_task.days,
weekly_task.task_time,
weekly_task.title,
)
if not inputs_ok:
return False
session.add(weekly_task)
session.commit()
return True
def change_weekly_task(
user: User,
weekly_task: WeeklyTask,
session: Session,
) -> bool:
"""This function is being used to edit a Weekly Task the user have.
Args:
user (User): The user who wants to edit the Weekly Task.
session (Session): The session to redirect to the database.
weekly_task (WeeklyTask): The Weekly Task that the of the user,
with the edited values.
Returns:
bool: Shows if the weekly_task has been edited in the db.
"""
inputs_ok = check_inputs(
weekly_task.days,
weekly_task.task_time,
weekly_task.title,
)
if not inputs_ok:
return False
w_task_query = session.query(WeeklyTask)
old_weekly_task = w_task_query.filter_by(id=weekly_task.id).first()
if weekly_task.user_id != user.id:
return False
old_weekly_task.title = weekly_task.title
old_weekly_task.days = weekly_task.days
old_weekly_task.content = weekly_task.content
old_weekly_task.is_important = weekly_task.is_important
old_weekly_task.task_time = weekly_task.task_time
session.commit()
return True
def create_task(task: Task, user: User, session: Session) -> bool:
"""Make a task, used by the generate_tasks function"""
user_tasks_query = session.query(Task).filter_by(owner_id=user.id)
task_by_time = user_tasks_query.filter_by(time=task.time)
task_by_date_time = task_by_time.filter_by(date=task.date)
task_by_title_and_time = task_by_date_time.filter_by(title=task.title)
task_exist = task_by_title_and_time.first()
if task_exist:
return False
session.add(task)
session.commit()
return True
def get_datetime(day: str, task_time: str) -> datetime:
"""Getting the datetime of days in the current week,
used by the generate_tasks function"""
current_date = date.today()
current_week_num = current_date.strftime("%W")
current_year = current_date.strftime("%Y")
date_string = f"{day} {task_time} {current_week_num} {current_year}"
return datetime.strptime(date_string, "%a %H:%M %W %Y")
def generate_tasks(user: User, session: Session) -> Iterator[bool]:
"""Generates tasks for the week
based on all the weekly tasks the user have"""
for weekly_task in user.weekly_tasks:
task_time = weekly_task.task_time
days = weekly_task.get_days()
days_list = days.split(", ")
for day in days_list:
date_time = get_datetime(day, task_time)
task = Task(
title=weekly_task.title,
description=weekly_task.content,
is_done=False,
is_important=weekly_task.is_important,
date=date_time.date(),
time=date_time.time(),
owner_id=user.id,
)
yield create_task(task, user, session)
def remove_weekly_task(weekly_task_id: int, session: Session) -> bool:
"""Removes a weekly task from the db based on the weekly task id"""
weekly_task_query = session.query(WeeklyTask)
weekly_task = weekly_task_query.filter_by(id=weekly_task_id).first()
if not weekly_task:
return False
session.query(WeeklyTask).filter_by(id=weekly_task_id).delete()
session.commit()
return True