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

Initializes default project on new organization creation #2818

Merged
merged 5 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 1 addition & 16 deletions src/dispatch/database/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

from dispatch import config
from dispatch.organization.models import Organization
from dispatch.project import service as project_service
from dispatch.project.models import ProjectCreate
from dispatch.search import fulltext
from dispatch.search.fulltext import (
sync_trigger,
Expand Down Expand Up @@ -92,8 +90,8 @@ def init_database(engine):

def init_schema(*, engine, organization: Organization):
"""Initializes a new schema."""

schema_name = f"{DISPATCH_ORGANIZATION_SCHEMA_PREFIX}_{organization.slug}"

if not engine.dialect.has_schema(engine, schema_name):
with engine.connect() as connection:
connection.execute(CreateSchema(schema_name))
Expand Down Expand Up @@ -125,19 +123,6 @@ def init_schema(*, engine, organization: Organization):

organization = db_session.merge(organization)
db_session.add(organization)

# create any required default values in schema here
#
#
project_service.get_or_create(
db_session=db_session,
project_in=ProjectCreate(
name="default",
default=True,
description="Default Dispatch project.",
organization=organization,
),
)
db_session.commit()
return organization

Expand Down
31 changes: 23 additions & 8 deletions src/dispatch/organization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError

from dispatch.database.core import get_db
from dispatch.database.service import common_parameters, search_filter_sort_paginate

from dispatch.enums import UserRoles
from dispatch.auth.models import DispatchUser
from dispatch.auth.service import get_current_user

from dispatch.auth.permissions import (
OrganizationOwnerPermission,
PermissionsDependency,
)
from dispatch.auth.service import get_current_user
from dispatch.database.core import get_db
from dispatch.database.service import common_parameters, search_filter_sort_paginate
from dispatch.enums import UserRoles
from dispatch.exceptions import ExistsError
from dispatch.models import PrimaryKey
from dispatch.project import flows as project_flows
from dispatch.project import service as project_service
from dispatch.project.models import ProjectCreate

from .models import (
OrganizationCreate,
Expand Down Expand Up @@ -54,14 +55,28 @@ def create_organization(
detail=[{"msg": "An organization with this name already exists."}],
)

# create organization
# we create the organization
organization = create(db_session=db_session, organization_in=organization_in)

# add creator as organization owner
# we add the creator as organization owner
add_user(
db_session=db_session, organization=organization, user=current_user, role=UserRoles.owner
)

# we create the default project
project_in = ProjectCreate(
name="default",
default=True,
description="Default Dispatch project.",
organization=organization,
)
project = project_service.create(db_session=db_session, project_in=project_in)

# we initialize the default project
project_flows.project_init_flow(
project_id=project.id, organization_slug=organization.slug, db_session=db_session
)

return organization


Expand Down
3 changes: 2 additions & 1 deletion src/dispatch/project/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@


@background_task
def project_create_flow(*, organization_slug: str, project_id: int, db_session=None):
def project_init_flow(*, project_id: int, organization_slug: str, db_session=None):
"""Initializes a new project with default settings."""
project = get(db_session=db_session, project_id=project_id)

# Add all plugins in disabled mode
Expand Down
4 changes: 2 additions & 2 deletions src/dispatch/project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from dispatch.exceptions import ExistsError
from dispatch.models import OrganizationSlug, PrimaryKey

from .flows import project_create_flow
from .flows import project_init_flow
from .models import (
ProjectCreate,
ProjectRead,
Expand Down Expand Up @@ -56,7 +56,7 @@ def create_project(

project = create(db_session=db_session, project_in=project_in)
background_tasks.add_task(
project_create_flow, project_id=project.id, organization_slug=organization
project_init_flow, project_id=project.id, organization_slug=organization
)
return project

Expand Down