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 6 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
19 changes: 18 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@


class Organization(BaseModel):
org_name: str
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
name: str

class Config:
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
allow_population_by_field_name = True
arbitrary_types_allowed = True
json_encoders = {ObjectId: str}
schema_extra = {"example": {"name": "Avanti Fellows"}}


class OrganizationResponse(Organization):
name: str

class Config:
allow_population_by_field_name = True
arbitrary_types_allowed = True
json_encoders = {ObjectId: str}
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
schema_extra = {"example": {"name": "Avanti Fellows"}}


class Image(BaseModel):
Expand Down
50 changes: 46 additions & 4 deletions app/routers/organizations.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
from fastapi import APIRouter, status, HTTPException
from database import client
from models import Organization
from models import Organization, OrganizationResponse
import secrets
import string
from fastapi.encoders import jsonable_encoder

router = APIRouter()
router = APIRouter(prefix="/organizations", tags=["Organizations"])


@router.get("/check-auth-token/{api_key}", response_model=Organization)
@router.post("/", response_model=OrganizationResponse)
async def create_organization(org: Organization):
org = jsonable_encoder(org)
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
key = "".join(
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
[secrets.choice(string.ascii_letters + string.digits) for _ in range(20)]
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved
)
number_of_loops = 3

while number_of_loops > 0:
sudheshna-donthineni marked this conversation as resolved.
Show resolved Hide resolved

# check if API key exists
if (client.quiz.organization.find_one({"key": key})) is None:
org["key"] = key
new_org = client.quiz.organization.insert_one(org)
created_org = client.quiz.organization.find_one(
{"_id": new_org.inserted_id}
)
return created_org

else:
key = "".join(
[
secrets.choice(string.ascii_letters + string.digits)
for _ in range(20)
]
)
number_of_loops -= 1

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("/authenticate/{api_key}", response_model=OrganizationResponse)
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(
{"key": api_key},
)
) is not None:
return org

Expand Down