Skip to content

Commit

Permalink
fix(organization): disallow creation of org with existing slug
Browse files Browse the repository at this point in the history
  • Loading branch information
whitdog47 committed Jan 16, 2025
1 parent 26d39f8 commit 6431560
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/dispatch/organization/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fastapi import APIRouter, Depends, HTTPException, status
from slugify import slugify
from pydantic.error_wrappers import ErrorWrapper, ValidationError

from sqlalchemy.exc import IntegrityError
Expand All @@ -23,7 +24,7 @@
OrganizationUpdate,
OrganizationPagination,
)
from .service import create, get, get_by_name, update, add_user
from .service import create, get, get_by_name, get_by_slug, update, add_user


router = APIRouter()
Expand All @@ -45,6 +46,11 @@ def create_organization(
current_user: CurrentUser,
):
"""Create a new organization."""
if not organization_in.name:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=[{"msg": "An organization name is required."}],
)
organization = get_by_name(db_session=db_session, name=organization_in.name)
if organization:
raise HTTPException(
Expand All @@ -56,7 +62,12 @@ def create_organization(
status_code=status.HTTP_409_CONFLICT,
detail=[{"msg": "An organization with this id already exists."}],
)

slug = slugify(organization_in.name, separator="_")
if get_by_slug(db_session=db_session, slug=slug):
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=[{"msg": "An organization with this slug already exists."}],
)
# we create the organization
organization = create(db_session=db_session, organization_in=organization_in)

Expand Down

0 comments on commit 6431560

Please sign in to comment.