-
Notifications
You must be signed in to change notification settings - Fork 52
Feature/weekly tasks #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Feature/weekly tasks #146
Changes from 17 commits
a459763
75015bb
cad6d10
a27bb08
30fd9a3
f65155c
c4e1a1d
2e441a1
276943e
ecdd5fa
f7bd688
0c4fcaf
54e68b1
2b4ae86
f42fb9c
1c4c61c
52d6197
3eede59
8f54b10
2554f98
cb6836f
1855ce7
564da0e
2485084
4b3e3ee
62f2023
6a5151e
ce4bc71
bd8147c
f7cbce6
925e449
a6efda9
07502c0
4cf90a2
9244bbb
af87259
b947751
b4b0ede
a30cbd8
3e2b357
d210459
dab05ee
487fd66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,12 @@ class User(Base): | |
|
||
events = relationship("UserEvent", back_populates="participants") | ||
|
||
weekly_tasks = relationship( | ||
"WeeklyTask", cascade="all, delete", back_populates="owner") | ||
|
||
tasks = relationship( | ||
"Task", cascade="all, delete", back_populates="owner") | ||
|
||
def __repr__(self): | ||
return f'<User {self.id}>' | ||
|
||
|
@@ -110,6 +116,34 @@ def __repr__(self): | |
) | ||
|
||
|
||
class Task(Base): | ||
__tablename__ = "tasks" | ||
|
||
id = Column(Integer, primary_key=True, index=True) | ||
title = Column(String) | ||
content = Column(String) | ||
is_done = Column(Boolean, nullable=False) | ||
is_important = Column(Boolean, nullable=False) | ||
date_time = Column(DateTime, nullable=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to give more specific variable names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should not mess with this model, its not mine |
||
owner_id = Column(Integer, ForeignKey("users.id")) | ||
|
||
owner = relationship("User", back_populates="tasks") | ||
|
||
|
||
class WeeklyTask(Base): | ||
__tablename__ = "weekly_task" | ||
|
||
id = Column(Integer, primary_key=True, index=True) | ||
title = Column(String) | ||
days = Column(String, nullable=False) | ||
issac211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
content = Column(String) | ||
is_important = Column(Boolean, nullable=False) | ||
the_time = Column(String, nullable=False) | ||
owner_id = Column(Integer, ForeignKey("users.id")) | ||
|
||
owner = relationship("User", back_populates="weekly_tasks") | ||
issac211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class Quote(Base): | ||
__tablename__ = "quotes" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
from datetime import date, datetime, time | ||
from typing import Iterator | ||
|
||
from app.database.models import User, Task, WeeklyTask | ||
from sqlalchemy.orm.session import Session | ||
|
||
|
||
def check_inputs(days: str, the_time: time, title: str) -> bool: | ||
"""Checks inputs, used by the get_w_t_from_input function""" | ||
return days and the_time and title | ||
|
||
|
||
def get_w_t_from_input( | ||
issac211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
user: User, | ||
title: str, days: str, | ||
content: str, the_time: 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. | ||
the_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. | ||
""" | ||
weekly_task = WeeklyTask( | ||
title=title, | ||
content=content, | ||
is_important=is_important, | ||
owner_id=user.id | ||
) | ||
|
||
if weekly_task_id != 0: | ||
weekly_task.id = weekly_task_id | ||
|
||
inputs_ok = check_inputs(days, the_time, title) | ||
if not inputs_ok: | ||
return weekly_task | ||
weekly_task.days = days | ||
weekly_task.the_time = the_time.strftime("%H:%M") | ||
return weekly_task | ||
|
||
|
||
def create_weekly_task( | ||
user: User, session: Session, | ||
weekly_task: WeeklyTask | ||
) -> 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. | ||
""" | ||
if weekly_task.days is None or weekly_task.the_time is None: | ||
return False | ||
user_titles = ( | ||
issac211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
user_weekly_task.title | ||
for user_weekly_task in user.weekly_tasks | ||
) | ||
if weekly_task.title in user_titles: | ||
return False | ||
issac211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
session.add(weekly_task) | ||
session.commit() | ||
return True | ||
|
||
|
||
def change_weekly_task( | ||
user: User, session: Session, | ||
weekly_task: WeeklyTask | ||
) -> 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. | ||
""" | ||
if weekly_task.days is None or weekly_task.the_time is None: | ||
return False | ||
w_task_query = session.query(WeeklyTask) | ||
old_weekly_task = w_task_query.filter_by(id=weekly_task.id).first() | ||
|
||
user_titles = ( | ||
user_weekly_task.title for user_weekly_task in user.weekly_tasks | ||
if user_weekly_task.title != old_weekly_task.title | ||
) | ||
|
||
if weekly_task.title in user_titles: | ||
return False | ||
|
||
if weekly_task.owner_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.the_time = weekly_task.the_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(date_time=task.date_time) | ||
task_by_title_and_time = task_by_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, the_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} {the_time} {current_week_num} {current_year}" | ||
return datetime.strptime(date_string, "%a %H:%M %W %Y") | ||
|
||
|
||
def generate_tasks(session: Session, user: User) -> Iterator[bool]: | ||
"""Generates tasks for the week | ||
based on all the weekly tasks the user have""" | ||
for weekly_task in user.weekly_tasks: | ||
the_time = weekly_task.the_time | ||
issac211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
days = weekly_task.days.split(", ") | ||
for day in days: | ||
date_time = get_datetime(day, the_time) | ||
task = Task( | ||
title=weekly_task.title, | ||
content=weekly_task.content, | ||
is_done=False, | ||
is_important=weekly_task.is_important, | ||
date_time=date_time, | ||
owner_id=user.id | ||
) | ||
yield create_task(task, user, session) | ||
yield False | ||
issac211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def remove_weekly_task(weekly_task_id: int, session: Session) -> bool: | ||
issac211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""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 |
Uh oh!
There was an error while loading. Please reload this page.