-
Notifications
You must be signed in to change notification settings - Fork 52
Feature/todo list #285
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
Merged
yammesicka
merged 26 commits into
PythonFreeCourse:develop
from
liaarbel:Feature/todo-list
Feb 26, 2021
Merged
Feature/todo list #285
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
b385f73
created templates of forms and the list, started with database models…
liaarbel 73d9f37
created to do list functions, templates an routers
liaarbel 892f168
added create, edit and delete functions. little changes with the fronted
liaarbel f97a148
added if tasks done and added tests
liaarbel 7e1a2da
fixed test of creating task
liaarbel 568c8d6
changed test_if_task_has_done data parameters
liaarbel 4b1855f
merged
liaarbel b1442a4
fixed tests - if task has done, and fixed models examples
liaarbel 916db78
merged
liaarbel 36ed683
added newline
liaarbel 3e7c4a3
removed imports and changed parameters name
liaarbel 28f9f95
added newline and changed parameter name
liaarbel c1a902b
deleted spaces
liaarbel 8891aca
changed data parameter name
liaarbel b7287f1
moved js to new file
liaarbel 4d7e29d
merged
liaarbel b4134d1
changed modals names, change functions in js from jquery, reformatted…
liaarbel bf946d7
merged
liaarbel ab20f1b
add ','
liaarbel aaa25aa
splited modal to each template, first try changing from jQuery, chang…
liaarbel 39b12f9
splited modal to each template
liaarbel 8aa8efd
merged
liaarbel d5c15d6
changed js from jQuery, fixed edit task test and added attributes to …
liaarbel 9bf753e
removed todo, fixed typing, checked about owner in delete_task() and …
liaarbel cc67d8e
removed commented code, changed url to url_for, removed console.log a…
liaarbel b37f6a0
fixed dayview momentary change
liaarbel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from datetime import date, time | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
from app.database.models import Task | ||
from app.internal.utils import create_model | ||
|
||
|
||
def create_task( | ||
db: Session, | ||
title: str, | ||
description: str, | ||
date_str: date, | ||
time_str: time, | ||
owner_id: int, | ||
is_important: bool, | ||
) -> Task: | ||
"""Creates and saves a new task.""" | ||
task = create_model( | ||
db, | ||
Task, | ||
title=title, | ||
description=description, | ||
date=date_str, | ||
time=time_str, | ||
owner_id=owner_id, | ||
is_important=is_important, | ||
is_done=False, | ||
) | ||
return task | ||
|
||
|
||
def by_id(db: Session, task_id: int) -> Task: | ||
task = db.query(Task).filter_by(id=task_id).one() | ||
return task |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
from datetime import datetime | ||
|
||
from fastapi import APIRouter, Depends, Form, status | ||
from fastapi.encoders import jsonable_encoder | ||
from fastapi.responses import JSONResponse, RedirectResponse | ||
from sqlalchemy.exc import SQLAlchemyError | ||
from sqlalchemy.orm import Session | ||
from starlette.requests import Request | ||
|
||
from app.config import templates | ||
from app.dependencies import get_db | ||
from app.internal.todo_list import by_id, create_task | ||
from app.internal.utils import get_current_user | ||
|
||
router = APIRouter( | ||
prefix="/task", | ||
tags=["task"], | ||
responses={status.HTTP_404_NOT_FOUND: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@router.post("/delete") | ||
def delete_task( | ||
request: Request, | ||
task_id: int = Form(...), | ||
db: Session = Depends(get_db), | ||
) -> RedirectResponse: | ||
user = get_current_user(db) | ||
task = by_id(db, task_id) | ||
if task.owner_id != user.id: | ||
return templates.TemplateResponse( | ||
"calendar_day_view.html", | ||
{"task_id": task_id}, | ||
status_code=status.HTTP_403_FORBIDDEN, | ||
) | ||
|
||
date_str = task.date.strftime('%Y-%m-%d') | ||
try: | ||
# Delete task | ||
db.delete(task) | ||
|
||
db.commit() | ||
|
||
except (SQLAlchemyError, TypeError): | ||
return templates.TemplateResponse( | ||
"calendar_day_view.html", | ||
{"task_id": task_id}, | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
) | ||
return RedirectResponse( | ||
request.url_for("dayview", date=date_str), | ||
status_code=status.HTTP_302_FOUND, | ||
) | ||
|
||
|
||
@router.post("/add") | ||
async def add_task( | ||
request: Request, | ||
title: str = Form(...), | ||
description: str = Form(...), | ||
date_str: str = Form(...), | ||
time_str: str = Form(...), | ||
is_important: bool = Form(False), | ||
session: Session = Depends(get_db), | ||
) -> RedirectResponse: | ||
user = get_current_user(session) | ||
create_task( | ||
session, | ||
title, | ||
description, | ||
datetime.strptime(date_str, '%Y-%m-%d').date(), | ||
datetime.strptime(time_str, '%H:%M').time(), | ||
user.id, | ||
is_important, | ||
) | ||
return RedirectResponse( | ||
request.url_for("dayview", date=date_str), | ||
status_code=status.HTTP_303_SEE_OTHER, | ||
) | ||
|
||
|
||
@router.post("/edit") | ||
async def edit_task( | ||
request: Request, | ||
task_id: int = Form(...), | ||
title: str = Form(...), | ||
description: str = Form(...), | ||
date_str: str = Form(...), | ||
time_str: str = Form(...), | ||
is_important: bool = Form(False), | ||
session: Session = Depends(get_db), | ||
) -> RedirectResponse: | ||
task = by_id(session, task_id) | ||
task.title = title | ||
task.description = description | ||
task.date = datetime.strptime(date_str, '%Y-%m-%d').date() | ||
task.time = datetime.strptime(time_str, '%H:%M:%S').time() | ||
task.is_important = is_important | ||
session.commit() | ||
return RedirectResponse( | ||
request.url_for("dayview", date=date_str), | ||
status_code=status.HTTP_303_SEE_OTHER, | ||
) | ||
|
||
|
||
@router.post("/done/{task_id}") | ||
async def set_task_done( | ||
request: Request, | ||
task_id: int, | ||
session: Session = Depends(get_db), | ||
) -> RedirectResponse: | ||
task = by_id(session, task_id) | ||
task.is_done = True | ||
session.commit() | ||
return RedirectResponse( | ||
request.url_for("dayview", date=task.date.strftime('%Y-%m-%d')), | ||
status_code=status.HTTP_303_SEE_OTHER, | ||
) | ||
|
||
|
||
@router.post("/undone/{task_id}") | ||
async def set_task_undone( | ||
request: Request, | ||
task_id: int, | ||
session: Session = Depends(get_db), | ||
) -> RedirectResponse: | ||
task = by_id(session, task_id) | ||
task.is_done = False | ||
session.commit() | ||
return RedirectResponse( | ||
request.url_for("dayview", date=task.date.strftime('%Y-%m-%d')), | ||
status_code=status.HTTP_303_SEE_OTHER, | ||
) | ||
|
||
|
||
@router.get("/{task_id}") | ||
async def get_task( | ||
task_id: int, | ||
session: Session = Depends(get_db), | ||
) -> JSONResponse: | ||
task = by_id(session, task_id) | ||
data = jsonable_encoder(task) | ||
return JSONResponse(content=data) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.