diff --git a/src/config.py b/src/config.py index f96e828..1f762e9 100644 --- a/src/config.py +++ b/src/config.py @@ -12,8 +12,7 @@ class TaskStatus(Enum): COMPLETED = "Completed" -TASK_STATUS_LIST = [ - "Not Started", - "In Progress", - "Completed" -] \ No newline at end of file +TASK_STATUS_LIST = ["Not Started", "In Progress", "Completed"] + + +EDIT_TASK_STATUS_LIST = ["", "Not Started", "In Progress", "Completed"] diff --git a/src/forms/task_form.py b/src/forms/task_form.py index c1ebcef..ae27dd0 100644 --- a/src/forms/task_form.py +++ b/src/forms/task_form.py @@ -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): @@ -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") diff --git a/src/main.py b/src/main.py index 2155fac..bb7f212 100644 --- a/src/main.py +++ b/src/main.py @@ -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 @@ -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") @@ -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")) @@ -104,31 +101,19 @@ def view_tasks(user_id): @app.route("/add_task/", 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() @@ -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/", 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) diff --git a/src/repository/tasks_repository.py b/src/repository/tasks_repository.py index 3a13e33..7762b46 100644 --- a/src/repository/tasks_repository.py +++ b/src/repository/tasks_repository.py @@ -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() diff --git a/tests/conftest.py b/tests/conftest.py index 13931ec..dc19bb4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -37,19 +37,19 @@ 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, ) @@ -57,13 +57,13 @@ def test_db(): 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, ) diff --git a/tests/test_repository/test_tasks_repository.py b/tests/test_repository/test_tasks_repository.py index bcf5ab4..b894ec5 100644 --- a/tests/test_repository/test_tasks_repository.py +++ b/tests/test_repository/test_tasks_repository.py @@ -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 @@ -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 @@ -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) @@ -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 @@ -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" \ No newline at end of file