Skip to content

Commit

Permalink
Merge pull request #45 from avantifellows/org
Browse files Browse the repository at this point in the history
Endpoint to onboard an organization
  • Loading branch information
sudheshna-donthineni authored Jun 6, 2022
2 parents eb11900 + 2c415e0 commit 8139624
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
18 changes: 17 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@


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

class Config:
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
schema_extra = {"example": {"name": "Avanti Fellows"}}


class Image(BaseModel):
Expand Down
41 changes: 37 additions & 4 deletions app/routers/organizations.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
from fastapi import APIRouter, status, HTTPException
from settings import Settings
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"])
settings = Settings()


@router.get("/check-auth-token/{api_key}", response_model=Organization)
def generate_random_string():
return "".join(
[
secrets.choice(string.ascii_letters + string.digits)
for _ in range(settings.random_string_length)
]
)


@router.post("/", response_model=OrganizationResponse)
async def create_organization(organization: Organization):
organization = jsonable_encoder(organization)

# create an API key
key = generate_random_string()

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


@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
5 changes: 5 additions & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pydantic import BaseSettings


class Settings(BaseSettings):
random_string_length: int = 20

0 comments on commit 8139624

Please sign in to comment.