Skip to content

Commit

Permalink
created edit_task() for TasksRepository. Needs further unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan-Sell committed Nov 15, 2024
1 parent ab4165f commit 79a944c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 37 deletions.
9 changes: 4 additions & 5 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class TaskStatus(Enum):
COMPLETED = "Completed"


TASK_STATUS_LIST = [
"Not Started",
"In Progress",
"Completed"
]
TASK_STATUS_LIST = ["Not Started", "In Progress", "Completed"]


EDIT_TASK_STATUS_LIST = ["", "Not Started", "In Progress", "Completed"]
18 changes: 15 additions & 3 deletions src/forms/task_form.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField
from wtforms import StringField, SubmitField, SelectField, IntegerField
from wtforms.validators import DataRequired

from src.config import TASK_STATUS_LIST
from src.config import TASK_STATUS_LIST, EDIT_TASK_STATUS_LIST


class AddTaskForm(FlaskForm):
Expand All @@ -12,6 +12,18 @@ class AddTaskForm(FlaskForm):
status = SelectField(
"Status:",
choices=[(status, status) for status in TASK_STATUS_LIST],
validators=[DataRequired()]
validators=[DataRequired()],
)
submit = SubmitField("Create Task")


class EditTaskForm(FlaskForm):

id = IntegerField("Task ID: ", validators=[DataRequired()])
title = StringField("Title:")
description = StringField("Description:")
status = SelectField(
"Status:",
choices=[(status, status) for status in EDIT_TASK_STATUS_LIST],
)
submit = SubmitField("Edit Task")
50 changes: 30 additions & 20 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import Flask, flash, redirect, render_template, request, url_for
from flask_login import LoginManager, login_required, login_user

from src.forms.task_form import AddTaskForm
from src.forms.task_form import AddTaskForm, EditTaskForm
from src.forms.user_forms import RegisterForm, LogInForm
from src.models import Base, SessionLocal, Tasks, Users, engine
from src.repository.users_repository import UsersRepository
Expand Down Expand Up @@ -44,7 +44,6 @@ def login():
user_repo = UsersRepository(session)
user = user_repo.find_user_by_username(username)


if user is None:
session.close()
flash("That email does not exist. Please try again.", "danger")
Expand Down Expand Up @@ -77,9 +76,7 @@ def register():
user_repo = UsersRepository(session)

if user_repo.find_user_by_username(username) is not None:
flash(
"Username already exists. Please choose a different one.", "danger"
)
flash("Username already exists. Please choose a different one.", "danger")
session.close()
return redirect(url_for("register"))

Expand All @@ -104,31 +101,19 @@ def view_tasks(user_id):
@app.route("/add_task/<int:user_id>", methods=["GET", "POST"])
@login_required
def add_task(user_id):

# Print to confirm function entry
print("Entered add_task function")

# Print user_id to confirm the URL parameter is being received correctly
print(f"Received user_id: {user_id}")


form = AddTaskForm()

if form.validate_on_submit():

print("Form was submitted and validated successfully")


title = form.title.data
description = form.description.data
status = form.status.data
print(f"Form Data - Title: {title}, Description: {description}, Status: {status}")


session = SessionLocal()
task_repo = TasksRespository(session)

task = Tasks(title=title, description=description, status=status, user_id=user_id)
task = Tasks(
title=title, description=description, status=status, user_id=user_id
)
task_repo.add_task(task)
session.close()

Expand All @@ -138,5 +123,30 @@ def add_task(user_id):
return render_template("add_task.html", form=form, user_id=user_id)


@app.route("/edit_task/<int:user_id>", methods=["GET", "POST"])
@login_required
def edit_task(user_id):
form = EditTaskForm()

if form.validate_on_submit():
task_id = form.id.data
title = form.title.data
description = form.description.data
status = form.status.data

session = SessionLocal()
task_repo = TasksRespository(session)
tasks = task_repo.find_tasks_by_user(user_id)
all_ids = [task.id for task in tasks]

if task_id not in all_ids:
flash(
f"Task # {task_id} is not associated with this user. Enter another task ID.",
"danger",
)
session.close()
return redirect(url_for("edit_task", user_id=user_id))


if __name__ == "__main__":
app.run(debug=True, port=5001)
18 changes: 18 additions & 0 deletions src/repository/tasks_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,21 @@ def delete_task(self, task_id: int):
if task is not None:
self.db_session.delete(task)
self.db_session.commit()

def edit_task(self, task_id: str, title: Optional[str] = None, description: Optional[str] = None, status: Optional[str] = None) -> None:
task = self.find_task_by_id(task_id)

# Check if task exists
if task is not None:
if title is not None and title.strip() != "":
task.title = title

if description is not None and description.strip() != "":
task.description = description

if status is not None and status.strip() != "":
task.status = status

# if none of the variables are none, then commit changes
if title or description or status:
self.db_session.commit()
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,33 @@ def test_db():
task1 = Tasks(
title="Chill out, maxin'",
description="Relaxing after school",
status=TaskStatus.NOT_STARTED,
status="Not Started",
user_id=will.id,
)
task2 = Tasks(
title="Shoot some b-ball",
description="Basketball with friends",
status=TaskStatus.IN_PROGRESS,
status="In Progress",
user_id=will.id,
)
task3 = Tasks(
title="Catch a cab",
description="Take a cab to Bel-Air",
status=TaskStatus.COMPLETED,
status="Completed",
user_id=will.id,
)

# Carlton's tasks
task4 = Tasks(
title="Do the Carlton Dance",
description="Dance like no one's watching",
status=TaskStatus.COMPLETED,
status="Completed",
user_id=carlton.id,
)
task5 = Tasks(
title="Argue with Will",
description="Friendly banter with Will",
status=TaskStatus.NOT_STARTED,
status="Not Started",
user_id=carlton.id,
)

Expand Down
53 changes: 49 additions & 4 deletions tests/test_repository/test_tasks_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_find_tasks_by_user(tasks_repository):
assert len(tasks_found) == 2
assert tasks_found[0].title == "Do the Carlton Dance"
assert tasks_found[0].description == "Dance like no one's watching"
assert tasks_found[1].status == TaskStatus.NOT_STARTED
assert tasks_found[1].status == "Not Started"
assert tasks_found[1].user_id == 2


Expand All @@ -31,7 +31,7 @@ def test_find_tasks_by_id(tasks_repository):
# Assert
assert task_found.title == "Catch a cab"
assert task_found.description == "Take a cab to Bel-Air"
assert task_found.status == TaskStatus.COMPLETED
assert task_found.status == "Completed"
assert task_found.user_id == 1


Expand All @@ -40,7 +40,7 @@ def test_add_task(tasks_repository, test_db):
title = "Pick apples"
description = "Ride trailor to the apple orchard with Jen. Remember the baskets."
user_id = 42
new_task = Tasks(title=title, description=description, user_id=user_id)
new_task = Tasks(title=title, description=description, user_id=user_id, status="Not Started")

# Action
tasks_repository.add_task(new_task)
Expand All @@ -50,7 +50,7 @@ def test_add_task(tasks_repository, test_db):
assert len(tasks_in_db) == 6
assert tasks_in_db[5].title == title
assert tasks_in_db[5].description == description
assert tasks_in_db[5].status.value == "Not Started"
assert tasks_in_db[5].status == "Not Started"
assert tasks_in_db[5].user_id == user_id


Expand All @@ -64,3 +64,48 @@ def test_delete_tasks(tasks_repository, test_db):
# Confirm Carlton is now in index 2 b/c Will's last task was deleted
assert tasks_in_db[2].title == "Do the Carlton Dance"
assert tasks_in_db[2].user_id == 2


def test_edit_task_title(tasks_repository, test_db):
# Arrange
task = tasks_repository.find_task_by_id(1)
new_title = "Chillin' Out, Relaxin' All Cool"

# Action
tasks_repository.edit_task(task_id=task.id, title=new_title)

# Assert
updated_task = tasks_repository.find_task_by_id(task.id)
assert updated_task.title == "Chillin' Out, Relaxin' All Cool"
assert updated_task.description == "Relaxing after school"
assert updated_task.status == "Not Started"


def test_edit_task_description(tasks_repository, test_db):
# Arrange
task = tasks_repository.find_task_by_id(2)
new_description = "Playing basketball with DJ Jazzy Jeff"

# Action
tasks_repository.edit_task(task_id=task.id, description=new_description)

# Assert
updated_task = tasks_repository.find_task_by_id(task.id)
assert updated_task.title == "Shoot some b-ball"
assert updated_task.description == "Playing basketball with DJ Jazzy Jeff"
assert updated_task.status == "In Progress"


def test_edit_task_status(tasks_repository, test_db):
# Arrange
task = tasks_repository.find_task_by_id(3)
new_status = "Completed"

# Action
tasks_repository.edit_task(task_id=task.id, status=new_status)

# Assert
updated_task = tasks_repository.find_task_by_id(task.id)
assert updated_task.title == "Catch a cab"
assert updated_task.description == "Take a cab to Bel-Air"
assert updated_task.status == "Completed"

0 comments on commit 79a944c

Please sign in to comment.