Skip to content

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

Open
wants to merge 43 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a459763
feat: added models Task and WeeklyTasks. added the weekly_tasks route…
issac211 Jan 24, 2021
75015bb
fix: made a connection between the HTML pages and the functions. dele…
issac211 Jan 26, 2021
cad6d10
fix: Arrange the code in all the files and add a description to the c…
issac211 Jan 26, 2021
a27bb08
feat: merging to local develop branch
issac211 Jan 26, 2021
30fd9a3
fix: Position the generate_tasks function on the weekly_tasks_manager…
issac211 Jan 27, 2021
f65155c
fix: Added a note in routers/weekly_tasks.py on the generate_tasks fu…
issac211 Jan 27, 2021
c4e1a1d
closed the session on the relevant functions
issac211 Jan 27, 2021
2e441a1
feat: added tests for all the functions and fixed a small thing
issac211 Jan 28, 2021
276943e
Merge branch 'develop' into feature/weekly-tasks
issac211 Jan 28, 2021
ecdd5fa
fix: Improved code visibility for flake8
issac211 Jan 29, 2021
f7bd688
fix: improved testing for max coverage
issac211 Jan 29, 2021
0c4fcaf
fix: improved code visibility
issac211 Jan 29, 2021
54e68b1
fix: improved testing code visibility
issac211 Jan 30, 2021
2b4ae86
fix: improved code for requested changes
issac211 Feb 1, 2021
f42fb9c
fix: improved code for requested changes
issac211 Feb 2, 2021
1c4c61c
Merge branch 'develop' into feature/weekly-tasks
issac211 Feb 2, 2021
52d6197
fix: improved code for requested changes
issac211 Feb 3, 2021
3eede59
fix: improved code for requested changes
issac211 Feb 4, 2021
8f54b10
Merge branch 'develop' into feature/weekly-tasks
issac211 Feb 4, 2021
2554f98
Merge branch 'develop' into feature/weekly-tasks
issac211 Feb 4, 2021
cb6836f
fix: improved code for requested changes
issac211 Feb 6, 2021
1855ce7
fix: minor change
issac211 Feb 7, 2021
564da0e
Merge branch 'develop' into feature/weekly-tasks
issac211 Feb 12, 2021
2485084
fix: fixed for flake8
issac211 Feb 12, 2021
4b3e3ee
fix: fixed imports
issac211 Feb 12, 2021
62f2023
fix: fixed a mistake
issac211 Feb 13, 2021
6a5151e
Merge branch 'develop' into feature/weekly-tasks
issac211 Feb 18, 2021
ce4bc71
fix: for flake8
issac211 Feb 18, 2021
bd8147c
fix: improved code for requested changes
issac211 Feb 21, 2021
f7cbce6
Merge branch 'develop' into feature/weekly-tasks
issac211 Feb 21, 2021
925e449
Merge branch 'develop' into feature/weekly-tasks
issac211 Feb 22, 2021
a6efda9
fix: pre-commit hooks
issac211 Feb 22, 2021
07502c0
fix: for change request
issac211 Feb 23, 2021
4cf90a2
fix: import name fix
issac211 Feb 23, 2021
9244bbb
Merge branch 'develop' into feature/weekly-tasks
issac211 Feb 23, 2021
af87259
fix: weekview, Not my model, but it failed and needed a little fix
issac211 Feb 23, 2021
b947751
Feature/geolocation (#193)
Eliory09 Feb 24, 2021
b4b0ede
Feature/notifications (#331)
IdanPelled Feb 24, 2021
a30cbd8
fix: for Changes requeste
issac211 Feb 24, 2021
3e2b357
local merge
issac211 Feb 24, 2021
d210459
Revert "local merge"
issac211 Feb 24, 2021
dab05ee
Arranging files
issac211 Feb 24, 2021
487fd66
Revert "Arranging files"
issac211 Feb 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}>'

Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to give more specific variable names.
is it the creation date, or maybe the deadline, it is hard to tell.

Copy link
Contributor Author

@issac211 issac211 Feb 24, 2021

Choose a reason for hiding this comment

The 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)
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")


class Quote(Base):
__tablename__ = "quotes"

Expand Down
171 changes: 171 additions & 0 deletions app/internal/weekly_tasks.py
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(
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 = (
user_weekly_task.title
for user_weekly_task in user.weekly_tasks
)
if weekly_task.title in user_titles:
return False
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
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


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
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from app.internal.quotes import daily_quotes, load_quotes
from app.routers import (
agenda, dayview, email, event, invitation, profile, search, telegram,
whatsapp
weekly_tasks, whatsapp
)
from app.telegram.bot import telegram_bot

Expand Down Expand Up @@ -41,6 +41,7 @@ def create_tables(engine, psql_environment):
app.include_router(dayview.router)
app.include_router(email.router)
app.include_router(invitation.router)
app.include_router(weekly_tasks.router)
app.include_router(whatsapp.router)
app.include_router(search.router)

Expand Down
Loading