Skip to content
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

Endpoint to onboard an organization #45

Merged
merged 8 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@


class Organization(BaseModel):
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
org_name: str
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved

class Config:
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
json_encoders = {ObjectId: str}


class Image(BaseModel):
url: str
Expand Down
30 changes: 29 additions & 1 deletion app/routers/organizations.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
from fastapi import APIRouter, status, HTTPException
from database import client
from models import Organization
from fastapi.responses import JSONResponse
import string
import random

router = APIRouter()
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved


@router.post("/create-org/{org_name}")
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
async def create_organization(org_name: str):
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
if org_name is not None:
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
# create an API key
char_set = string.ascii_letters + string.punctuation
urand = random.SystemRandom()
org_key = "".join([urand.choice(char_set) for _ in range(20)])
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved

# check if API key exists
if (client.quiz.organization.find_one({"org_key": org_key})) is None:
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
org_data = {"org_name": org_name, "org_key": org_key}
client.quiz.organization.insert_one(org_data)
return JSONResponse(status_code=status.HTTP_201_CREATED, content=org_name)
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved

raise HTTPException(
status_code=500,
detail="Duplicate API key. Please try again.",
)
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved


@router.get("/check-auth-token/{api_key}", response_model=Organization)
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
async def check_auth_token(api_key: str):

if (
org := client.quiz.organization.find_one({"org_key": api_key}, {"org_name": 1})
org := client.quiz.organization.find_one(
{"org_key": api_key},
{
"org_name": 1,
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
},
)
) is not None:
return org

Expand Down