├── README.md └── to_do_app ├── __pycache__ ├── main.cpython-311.pyc ├── orm.cpython-311.pyc ├── models.cpython-311.pyc └── database.cpython-311.pyc ├── requirements.txt ├── models.py ├── orm.py ├── main.py └── templates └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # ToDo-FastAPI -------------------------------------------------------------------------------- /to_do_app/__pycache__/main.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matinggff/ToDo-FastAPI/HEAD/to_do_app/__pycache__/main.cpython-311.pyc -------------------------------------------------------------------------------- /to_do_app/__pycache__/orm.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matinggff/ToDo-FastAPI/HEAD/to_do_app/__pycache__/orm.cpython-311.pyc -------------------------------------------------------------------------------- /to_do_app/__pycache__/models.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matinggff/ToDo-FastAPI/HEAD/to_do_app/__pycache__/models.cpython-311.pyc -------------------------------------------------------------------------------- /to_do_app/__pycache__/database.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matinggff/ToDo-FastAPI/HEAD/to_do_app/__pycache__/database.cpython-311.pyc -------------------------------------------------------------------------------- /to_do_app/requirements.txt: -------------------------------------------------------------------------------- 1 | annotated-types==0.5.0 2 | anyio==3.7.1 3 | click==8.1.7 4 | colorama==0.4.6 5 | fastapi==0.103.0 6 | greenlet==2.0.2 7 | h11==0.14.0 8 | idna==3.4 9 | Jinja2==3.1.2 10 | MarkupSafe==2.1.3 11 | pydantic==2.3.0 12 | pydantic_core==2.6.3 13 | python-multipart==0.0.6 14 | sniffio==1.3.0 15 | SQLAlchemy==2.0.20 16 | starlette==0.27.0 17 | typing_extensions==4.7.1 18 | uvicorn==0.23.2 19 | -------------------------------------------------------------------------------- /to_do_app/models.py: -------------------------------------------------------------------------------- 1 | from database import Base 2 | from sqlalchemy.types import String, Boolean, Integer, DateTime 3 | from sqlalchemy import Column 4 | 5 | class Todo(Base): 6 | __tablename__ = 'todo' 7 | id = Column(Integer, primary_key=True, index=True) 8 | title = Column(String) 9 | complete =Column(Boolean, default=False) 10 | created = Column(Integer) 11 | deadline = Column(Integer) 12 | left_time = Column(Integer, default=0) -------------------------------------------------------------------------------- /to_do_app/orm.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy.orm import Session 2 | from models import Todo 3 | from datetime import datetime 4 | 5 | def add_todo(title:str, db:Session): 6 | current_time_stamp = datetime.now() 7 | convert_to_stamp = int(round(current_time_stamp.timestamp())) 8 | new_todo = Todo( 9 | title = title, 10 | created = convert_to_stamp, 11 | deadline = convert_to_stamp + 10800 12 | 13 | ) 14 | db.add(new_todo) 15 | db.commit() 16 | db.refresh(new_todo) 17 | return new_todo 18 | 19 | def update_todo(todo_id:int, db:Session): 20 | todo = db.query(Todo).filter(Todo.id == todo_id).first() 21 | todo.complete = not todo.complete 22 | db.commit() 23 | 24 | def delete_todo(todo_id:int, db:Session): 25 | todo = db.query(Todo).filter(Todo.id == todo_id).first() 26 | db.delete(todo) 27 | db.commit() -------------------------------------------------------------------------------- /to_do_app/main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI,Request ,Depends, status, Form 2 | from fastapi.templating import Jinja2Templates 3 | from database import engine, get_db 4 | import models 5 | from starlette. responses import RedirectResponse 6 | from sqlalchemy.orm import Session 7 | from models import Todo 8 | import orm 9 | from datetime import datetime 10 | 11 | app = FastAPI() 12 | 13 | models.Base.metadata.create_all(bind=engine) 14 | 15 | templates = Jinja2Templates("templates") 16 | 17 | @app.get("/") 18 | def home(request: Request, db: Session = Depends(get_db)): 19 | todos = db.query(Todo).all() 20 | return templates.TemplateResponse("index.html", {"request": request, "todo_list": todos}) 21 | 22 | @app.post("/add") 23 | def add(request: Request, title:str = Form(...), db: Session = Depends(get_db)): 24 | orm.add_todo(title, db) 25 | 26 | url = app.url_path_for("home") #def name 27 | return RedirectResponse(url, status_code=status.HTTP_303_SEE_OTHER) 28 | 29 | @app.get("/update/{todo_id}") 30 | def update(request: Request, todo_id: int, db: Session = Depends(get_db)): 31 | orm.update_todo(todo_id, db) 32 | url = app.url_path_for("home") 33 | return RedirectResponse(url, status_code=status.HTTP_302_FOUND) 34 | 35 | @app.get("/delete/{todo_id}") 36 | def delete(request: Request, todo_id: int, db: Session = Depends(get_db)): 37 | orm.delete_todo(todo_id, db) 38 | url = app.url_path_for("home") 39 | return RedirectResponse(url, status_code=status.HTTP_302_FOUND) 40 | 41 | @app.get("/time/{todo_id}") 42 | def time_left(request: Request, todo_id: int, db: Session = Depends(get_db)): 43 | todo = db.query(Todo).filter(Todo.id == todo_id).first() 44 | current_time = datetime.now() 45 | convert_to_stamp = int(round(current_time.timestamp())) 46 | todo_left = todo.deadline - convert_to_stamp 47 | todo.left_time = todo_left 48 | db.commit() 49 | url = app.url_path_for("home") 50 | return RedirectResponse(url, status_code=status.HTTP_302_FOUND) 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /to_do_app/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 || No. | 34 |Todo item | 35 |Time | 36 |Status | 37 |Actions | 38 | 39 |||
|---|---|---|---|---|---|---|
| {{todo.id }} | 45 |{{ todo.title }} | 46 | {% if todo.left_time <= 0 %} 47 |0 s | 48 | {% else %} 49 |{{ todo.left_time }}S | 50 | {% endif %} 51 |
52 | {% if todo.complete == False %}
53 | In progress
54 | {% else %}
55 | Completed
56 | {% endif %}
57 | |
58 | 59 | Update 60 | Delete 61 | | 62 |63 | Reload 64 | | 65 |