-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some minor bug fixes still a long way to go
- Loading branch information
1 parent
cc94bdd
commit 7c063ee
Showing
2,862 changed files
with
676,806 additions
and
89 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
from sqlalchemy.orm import Session | ||
from app.models import User | ||
from app.schemas import UserCreate, UserUpdate | ||
|
This file contains 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 |
---|---|---|
@@ -1,75 +1,117 @@ | ||
#app/main.py | ||
from fastapi import FastAPI, Depends, Request, Form, HTTPException | ||
from fastapi.responses import RedirectResponse, HTMLResponse | ||
from fastapi.templating import Jinja2Templates | ||
from sqlalchemy.orm import Session | ||
from app.auth import authenticate_user, get_current_user | ||
from app.auth import authenticate_user, get_current_user, get_user_id_from_cookie | ||
from app.crud import create_user | ||
from app.init_db import init_db | ||
import logging | ||
from app.dependencies import get_db | ||
from app.routers import user as user_router | ||
from app.routers import admin as admin_router | ||
from app.database import Base, engine | ||
from app.dependencies import get_db | ||
from fastapi.staticfiles import StaticFiles | ||
from contextlib import asynccontextmanager | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
import logging | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) | ||
|
||
app = FastAPI() | ||
# Example async context manager for lifespan | ||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
init_db() # Initialize the database connection or other startup tasks | ||
yield # This is where the app runs | ||
# Cleanup can be done here if necessary | ||
|
||
# Async context manager for database session | ||
@asynccontextmanager | ||
async def get_db_session(): | ||
db = next(get_db()) | ||
try: | ||
yield db | ||
finally: | ||
db.close() | ||
|
||
app = FastAPI(lifespan=lifespan) | ||
|
||
app.include_router(admin_router.router, prefix="/admin", tags=["admin"]) | ||
app.include_router(user_router.router, prefix="/admin", tags=["users"]) | ||
|
||
@app.middleware("http") | ||
async def add_user_to_request(request: Request, call_next): | ||
try: | ||
request.state.user = await get_current_user(request) | ||
except HTTPException: | ||
request.state.user = None | ||
# Middleware to add user information to the request | ||
class AddUserMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next): | ||
async with get_db_session() as db: | ||
try: | ||
user_id = get_user_id_from_cookie(request.cookies.get("user_id")) | ||
if user_id: | ||
request.state.user = await get_current_user(db=db, user_id=user_id) | ||
logging.info(f"User ID retrieved from cookie: {user_id}") | ||
if request.state.user: | ||
logging.info(f"User retrieved: {request.state.user.username}") | ||
else: | ||
request.state.user = None | ||
except HTTPException as e: | ||
logging.error(f"HTTPException in middleware: {e.detail}") | ||
request.state.user = None | ||
except Exception as e: | ||
logging.error(f"Error in middleware: {e}") | ||
request.state.user = None | ||
|
||
response = await call_next(request) | ||
return response | ||
response = await call_next(request) | ||
return response | ||
|
||
# Add middleware to the app | ||
app.add_middleware(AddUserMiddleware) | ||
|
||
app.mount("/static", StaticFiles(directory="app/static"), name="static") | ||
|
||
templates = Jinja2Templates(directory="app/templates") | ||
|
||
@app.on_event("startup") | ||
def on_startup(): | ||
init_db() | ||
|
||
Base.metadata.create_all(bind=engine) | ||
|
||
@app.get("/") | ||
async def read_root(): | ||
return {"message": "Welcome to the Admin Dashboard"} | ||
|
||
@app.get("/admin/") | ||
def login_page(request: Request): | ||
async def login_page(request: Request): | ||
return templates.TemplateResponse("login.html", {"request": request, "user": request.state.user}) | ||
|
||
@app.post("/login/") | ||
def login(request: Request, username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db)): | ||
async def login(request: Request, username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db)): | ||
user = authenticate_user(username, password, db) | ||
if not user: | ||
raise HTTPException(status_code=400, detail="Invalid credentials") | ||
logging.warning(f"Failed login attempt for user: {username}") | ||
return templates.TemplateResponse("login.html", { | ||
"request": request, "error": "Invalid credentials, please try again." | ||
}) | ||
|
||
# Set the user in cookie and redirect to the dashboard | ||
response = RedirectResponse(url="/dashboard/", status_code=302) | ||
response.set_cookie(key="username", value=user.username) | ||
response.set_cookie(key="user_id", value=str(user.id), httponly=True, secure=False, samesite="Lax") | ||
return response | ||
|
||
@app.get("/dashboard/") | ||
def dashboard(request: Request): | ||
async def dashboard(request: Request): | ||
if not request.state.user: | ||
return RedirectResponse(url="/admin/") | ||
return templates.TemplateResponse("dashboard.html", {"request": request}) | ||
|
||
# Log current user info | ||
logging.info(f"Current user: {request.state.user.username}") | ||
|
||
return templates.TemplateResponse("dashboard.html", { | ||
"request": request, | ||
"user": request.state.user # Pass user context | ||
}) | ||
|
||
@app.get("/logout/") | ||
def logout(): | ||
async def logout(): | ||
response = RedirectResponse(url="/logout-confirmation/") | ||
response.delete_cookie("username") | ||
response.delete_cookie("user_id") # Remove the user_id cookie | ||
return response | ||
|
||
@app.get("/logout-confirmation/", response_class=HTMLResponse) | ||
async def logout_confirmation(request: Request): | ||
return templates.TemplateResponse("logout_confirmation.html", {"request": request}) | ||
return templates.TemplateResponse("logout_confirmation.html", { | ||
"request": request, | ||
"user": request.state.user # Pass the user object to the template | ||
}) |
Binary file not shown.
This file contains 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 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 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 |
---|---|---|
@@ -1,11 +1,17 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block content %} | ||
<div class="container"> | ||
<h1 class="title">Welcome, {{ username }}</h1> | ||
<p class="subtitle">You are now logged in.</p> | ||
<a href="/logout" class="button is-danger">Logout</a> | ||
|
||
</div> | ||
{% if user %} | ||
<div class="container"> | ||
<h1 class="title">Welcome, {{ user.username }}</h1> <!-- Access username through user object --> | ||
<p class="subtitle">You are now logged in.</p> | ||
<a href="/logout" class="button is-danger">Logout</a> | ||
</div> | ||
{% else %} | ||
<div class="container"> | ||
<p>You have not managed to log in. Please click here to <a href="/admin/" class="button is-danger">Login</a>.</p> | ||
</div> | ||
{% endif %} | ||
|
||
{% endblock %} |
This file contains 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 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 |
---|---|---|
@@ -1,8 +1,16 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block content %} | ||
|
||
<div class="container"> | ||
<h1 class="title">You have been logged out</h1> | ||
<p>Thank you for visiting. Click <a href="/admin/">here</a> to log in again.</p> | ||
<h1 class="title">Logout Confirmation</h1> | ||
|
||
{% if user %} | ||
<p>Goodbye, {{ user.username }}! You have successfully logged out.</p> | ||
<a href="/admin/" class="button">Login Again</a> | ||
{% else %} | ||
<p>You are not logged in. Please click here to <a href="/admin/" class="button">Login</a>.</p> | ||
{% endif %} | ||
</div> | ||
|
||
{% endblock %} |
This file contains 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
Oops, something went wrong.