-
Notifications
You must be signed in to change notification settings - Fork 1
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
Feature/tdbytes #96
Merged
Merged
Feature/tdbytes #96
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d93d0b9
:whale: remove obsolete compose version
otytlandsvik 3272855
:sparkles: add kiosk suggestion support
otytlandsvik 5a1ee91
:passport_control: add kiosk admin role
otytlandsvik cc4a4c1
:white_check_mark: add tests for kiosk suggestion endpoints
otytlandsvik 8edad90
:art: run autopep8
otytlandsvik 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 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,4 +1,3 @@ | ||
version: "3.5" | ||
name: backend | ||
services: | ||
tdctl_api: | ||
|
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,4 +1,3 @@ | ||
version: "3.5" | ||
services: | ||
tdctl_api: | ||
image: ghcr.io/td-org-uit-no/tdctl-api/tdctl-api:latest | ||
|
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 |
---|---|---|
|
@@ -2,46 +2,51 @@ | |
from fastapi import FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
|
||
from app.api import stats | ||
from app.api import kiosk, stats | ||
from .config import config | ||
|
||
from .api import members, auth, events, admin, mail, jobs | ||
from .db import setup_db | ||
|
||
|
||
def create_app(): | ||
|
||
app = FastAPI( | ||
title='TDCTL-API', | ||
title="TDCTL-API", | ||
version="0.1", | ||
description='''TDCTL-database API. | ||
Everything related to Tromsøstudentenes Dataforening''', | ||
contact={'name': 'td', 'email': '[email protected]'}, | ||
description="""TDCTL-database API. | ||
Everything related to Tromsøstudentenes Dataforening""", | ||
contact={"name": "td", "email": "[email protected]"}, | ||
docs_url="/", | ||
) | ||
|
||
# CORS Middleware | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=['http://localhost:3000', 'https://td-uit.no'], | ||
allow_origins=["http://localhost:3000", "https://td-uit.no"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
# Fetch config object | ||
env = os.getenv('API_ENV', 'default') | ||
env = os.getenv("API_ENV", "default") | ||
app.config = config[env] | ||
|
||
# Routers | ||
app.include_router(members.router, prefix="/api/member", tags=['members']) | ||
app.include_router(auth.router, prefix="/api/auth", tags=['auth']) | ||
app.include_router(events.router, prefix="/api/event", tags=['event']) | ||
app.include_router(admin.router, prefix="/api/admin", tags=['admin']) | ||
app.include_router(mail.router, prefix="/api/mail", tags=['mail']) | ||
app.include_router(jobs.router, prefix="/api/jobs", tags=['job']) | ||
app.include_router(members.router, prefix="/api/member", tags=["members"]) | ||
app.include_router(auth.router, prefix="/api/auth", tags=["auth"]) | ||
app.include_router(events.router, prefix="/api/event", tags=["event"]) | ||
app.include_router(admin.router, prefix="/api/admin", tags=["admin"]) | ||
app.include_router(mail.router, prefix="/api/mail", tags=["mail"]) | ||
app.include_router(jobs.router, prefix="/api/jobs", tags=["job"]) | ||
app.include_router(kiosk.router, prefix="/api/kiosk", tags=["kiosk"]) | ||
# only visible in development | ||
app.include_router(stats.router, prefix="/api/stats", tags=['stats'], include_in_schema=app.config.ENV!='production') | ||
app.include_router( | ||
stats.router, | ||
prefix="/api/stats", | ||
tags=["stats"], | ||
include_in_schema=app.config.ENV != "production", | ||
) | ||
|
||
setup_db(app) | ||
# Set tokens to expire at at "exp" | ||
|
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from datetime import datetime | ||
from uuid import UUID, uuid4 | ||
|
||
from fastapi import APIRouter, Depends, HTTPException, Request, Response | ||
|
||
from app.auth_helpers import authorize, authorize_admin, authorize_kiosk_admin | ||
from app.db import get_database | ||
|
||
from ..models import AccessTokenPayload, KioskSuggestionPayload, Role | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/suggestion") | ||
def add_suggestion( | ||
request: Request, | ||
newSuggestion: KioskSuggestionPayload, | ||
token: AccessTokenPayload = Depends(authorize), | ||
): | ||
db = get_database(request) | ||
|
||
member = db.members.find_one({"id": UUID(token.user_id)}) | ||
if member is None: | ||
raise HTTPException(404) | ||
|
||
id = uuid4() | ||
formatted_product = newSuggestion.product.lower().capitalize() | ||
|
||
suggestion = { | ||
"id": id, | ||
"product": formatted_product, | ||
"member": member, | ||
"timestamp": datetime.now(), | ||
} | ||
|
||
db.kioskSuggestions.insert_one(suggestion) | ||
|
||
return Response(status_code=201) | ||
|
||
|
||
@router.get("/suggestions") | ||
def get_suggestions( | ||
request: Request, token: AccessTokenPayload = Depends(authorize_kiosk_admin) | ||
): | ||
db = get_database(request) | ||
|
||
# Kiosk admin has access to list, but only admin should get member names | ||
isAdmin = token.role == Role.admin | ||
|
||
# Return all suggestions in collection | ||
suggestions = db.kioskSuggestions.aggregate([{"$sort": {"date": -1}}]) | ||
|
||
# Only return username to admins | ||
ret = [ | ||
{ | ||
"id": s["id"], | ||
"product": s["product"], | ||
"username": s["member"].get("realName", None) if isAdmin else "-", | ||
"timestamp": s["timestamp"], | ||
} | ||
for s in suggestions | ||
] | ||
|
||
if len(ret) == 0: | ||
raise HTTPException(404, "No kiosk suggestions found") | ||
|
||
return ret | ||
|
||
|
||
@router.delete("/suggestion/{id}") | ||
def delete_suggestion( | ||
request: Request, | ||
id: str, | ||
token: AccessTokenPayload = Depends(authorize_admin), | ||
): | ||
db = get_database(request) | ||
|
||
res = db.kioskSuggestions.find_one_and_delete({"id": UUID(id)}) | ||
|
||
if not res: | ||
raise HTTPException(404, "Suggestion not found") | ||
|
||
return Response(status_code=200) |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not relevant now, but we should maybe look to guard against users spamming suggestions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, many endpoints could use some rate limiting mechanism