mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
Merge pull request #305 from learnhouse/feat/get-orgs-with-config
feat: get config with orgs
This commit is contained in:
commit
283e5bc189
7 changed files with 85 additions and 7 deletions
14
apps/api/migrations/orgconfigs/v1_1.py
Normal file
14
apps/api/migrations/orgconfigs/v1_1.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
def migrate_to_v1_1(v1_config):
|
||||||
|
# Start by copying the existing configuration
|
||||||
|
v1_1_config = v1_config.copy()
|
||||||
|
|
||||||
|
# Update the config version
|
||||||
|
v1_1_config["config_version"] = "1.1"
|
||||||
|
|
||||||
|
# Add the new 'cloud' object at the end
|
||||||
|
v1_1_config['cloud'] = {
|
||||||
|
"plan": "free",
|
||||||
|
"custom_domain": False
|
||||||
|
}
|
||||||
|
|
||||||
|
return v1_1_config
|
||||||
|
|
@ -83,12 +83,18 @@ class OrgGeneralConfig(BaseModel):
|
||||||
color: str = "normal"
|
color: str = "normal"
|
||||||
watermark: bool = True
|
watermark: bool = True
|
||||||
|
|
||||||
|
# Cloud
|
||||||
|
class OrgCloudConfig(BaseModel):
|
||||||
|
plan: Literal["free", "standard", "pro"] = "free"
|
||||||
|
custom_domain: bool = False
|
||||||
|
|
||||||
|
|
||||||
# Main Config
|
# Main Config
|
||||||
class OrganizationConfigBase(BaseModel):
|
class OrganizationConfigBase(BaseModel):
|
||||||
config_version: str = "1.0"
|
config_version: str = "1.1"
|
||||||
general: OrgGeneralConfig
|
general: OrgGeneralConfig
|
||||||
features: OrgFeatureConfig
|
features: OrgFeatureConfig
|
||||||
|
cloud: OrgCloudConfig
|
||||||
|
|
||||||
|
|
||||||
class OrganizationConfig(SQLModel, table=True):
|
class OrganizationConfig(SQLModel, table=True):
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,10 @@ class Organization(OrganizationBase, table=True):
|
||||||
creation_date: str = ""
|
creation_date: str = ""
|
||||||
update_date: str = ""
|
update_date: str = ""
|
||||||
|
|
||||||
|
class OrganizationWithConfig(BaseModel):
|
||||||
|
org: Organization
|
||||||
|
config: OrganizationConfig
|
||||||
|
|
||||||
|
|
||||||
class OrganizationUpdate(OrganizationBase):
|
class OrganizationUpdate(OrganizationBase):
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ from fastapi import APIRouter, Depends
|
||||||
from sqlmodel import Session, select
|
from sqlmodel import Session, select
|
||||||
from config.config import get_learnhouse_config
|
from config.config import get_learnhouse_config
|
||||||
from migrations.orgconfigs.v0tov1 import migrate_v0_to_v1
|
from migrations.orgconfigs.v0tov1 import migrate_v0_to_v1
|
||||||
|
from migrations.orgconfigs.v1_1 import migrate_to_v1_1
|
||||||
from src.core.events.database import get_db_session
|
from src.core.events.database import get_db_session
|
||||||
from src.db.organization_config import OrganizationConfig
|
from src.db.organization_config import OrganizationConfig
|
||||||
|
|
||||||
|
|
@ -32,3 +33,22 @@ async def migrate(
|
||||||
db_session.commit()
|
db_session.commit()
|
||||||
|
|
||||||
return {"message": "Migration successful"}
|
return {"message": "Migration successful"}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/migrate_orgconfig_v1_to_v1.1")
|
||||||
|
async def migratev1_1(
|
||||||
|
db_session: Session = Depends(get_db_session),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Migrate organization config from v0 to v1
|
||||||
|
"""
|
||||||
|
statement = select(OrganizationConfig)
|
||||||
|
result = db_session.exec(statement)
|
||||||
|
|
||||||
|
for orgConfig in result:
|
||||||
|
orgConfig.config = migrate_to_v1_1(orgConfig.config)
|
||||||
|
|
||||||
|
db_session.add(orgConfig)
|
||||||
|
db_session.commit()
|
||||||
|
|
||||||
|
return {"message": "Migration successful"}
|
||||||
|
|
@ -20,7 +20,6 @@ from src.services.orgs.users import (
|
||||||
from src.db.organization_config import OrganizationConfigBase
|
from src.db.organization_config import OrganizationConfigBase
|
||||||
from src.db.users import PublicUser
|
from src.db.users import PublicUser
|
||||||
from src.db.organizations import (
|
from src.db.organizations import (
|
||||||
Organization,
|
|
||||||
OrganizationCreate,
|
OrganizationCreate,
|
||||||
OrganizationRead,
|
OrganizationRead,
|
||||||
OrganizationUpdate,
|
OrganizationUpdate,
|
||||||
|
|
@ -321,7 +320,7 @@ async def api_user_orgs(
|
||||||
limit: int,
|
limit: int,
|
||||||
current_user: PublicUser = Depends(get_current_user),
|
current_user: PublicUser = Depends(get_current_user),
|
||||||
db_session: Session = Depends(get_db_session),
|
db_session: Session = Depends(get_db_session),
|
||||||
) -> List[Organization]:
|
) -> List[OrganizationRead]:
|
||||||
"""
|
"""
|
||||||
Get orgs by page and limit by current user
|
Get orgs by page and limit by current user
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ from src.db.organization_config import (
|
||||||
CourseOrgConfig,
|
CourseOrgConfig,
|
||||||
DiscussionOrgConfig,
|
DiscussionOrgConfig,
|
||||||
MemberOrgConfig,
|
MemberOrgConfig,
|
||||||
|
OrgCloudConfig,
|
||||||
OrgFeatureConfig,
|
OrgFeatureConfig,
|
||||||
OrgGeneralConfig,
|
OrgGeneralConfig,
|
||||||
OrganizationConfig,
|
OrganizationConfig,
|
||||||
|
|
@ -350,6 +351,10 @@ def install_create_organization(org_object: OrganizationCreate, db_session: Sess
|
||||||
collaboration=CollaborationOrgConfig(enabled=True, limit=0),
|
collaboration=CollaborationOrgConfig(enabled=True, limit=0),
|
||||||
api=APIOrgConfig(enabled=True, limit=0),
|
api=APIOrgConfig(enabled=True, limit=0),
|
||||||
),
|
),
|
||||||
|
cloud=OrgCloudConfig(
|
||||||
|
plan='free',
|
||||||
|
custom_domain=False
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
org_config = json.loads(org_config.json())
|
org_config = json.loads(org_config.json())
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ from src.db.organization_config import (
|
||||||
CourseOrgConfig,
|
CourseOrgConfig,
|
||||||
DiscussionOrgConfig,
|
DiscussionOrgConfig,
|
||||||
MemberOrgConfig,
|
MemberOrgConfig,
|
||||||
|
OrgCloudConfig,
|
||||||
OrgFeatureConfig,
|
OrgFeatureConfig,
|
||||||
OrgGeneralConfig,
|
OrgGeneralConfig,
|
||||||
OrganizationConfig,
|
OrganizationConfig,
|
||||||
|
|
@ -178,6 +179,10 @@ async def create_org(
|
||||||
collaboration=CollaborationOrgConfig(enabled=True, limit=0),
|
collaboration=CollaborationOrgConfig(enabled=True, limit=0),
|
||||||
api=APIOrgConfig(enabled=True, limit=0),
|
api=APIOrgConfig(enabled=True, limit=0),
|
||||||
),
|
),
|
||||||
|
cloud=OrgCloudConfig(
|
||||||
|
plan='free',
|
||||||
|
custom_domain=False
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
org_config = json.loads(org_config.json())
|
org_config = json.loads(org_config.json())
|
||||||
|
|
@ -209,6 +214,7 @@ async def create_org(
|
||||||
|
|
||||||
return org
|
return org
|
||||||
|
|
||||||
|
|
||||||
async def create_org_with_config(
|
async def create_org_with_config(
|
||||||
request: Request,
|
request: Request,
|
||||||
org_object: OrganizationCreate,
|
org_object: OrganizationCreate,
|
||||||
|
|
@ -338,6 +344,7 @@ async def update_org(
|
||||||
|
|
||||||
return org
|
return org
|
||||||
|
|
||||||
|
|
||||||
async def update_org_with_config_no_auth(
|
async def update_org_with_config_no_auth(
|
||||||
request: Request,
|
request: Request,
|
||||||
orgconfig: OrganizationConfigBase,
|
orgconfig: OrganizationConfigBase,
|
||||||
|
|
@ -462,20 +469,43 @@ async def get_orgs_by_user(
|
||||||
user_id: str,
|
user_id: str,
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
limit: int = 10,
|
limit: int = 10,
|
||||||
) -> list[Organization]:
|
) -> list[OrganizationRead]:
|
||||||
|
|
||||||
statement = (
|
statement = (
|
||||||
select(Organization)
|
select(Organization)
|
||||||
.join(UserOrganization)
|
.join(UserOrganization)
|
||||||
.where(
|
.where(
|
||||||
Organization.id == UserOrganization.org_id,
|
|
||||||
UserOrganization.user_id == user_id,
|
UserOrganization.user_id == user_id,
|
||||||
|
UserOrganization.role_id == 1, # Only where the user is admin
|
||||||
)
|
)
|
||||||
|
.offset((page - 1) * limit)
|
||||||
|
.limit(limit)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get organizations where the user is an admin
|
||||||
|
result = db_session.exec(statement)
|
||||||
|
orgs = result.all()
|
||||||
|
|
||||||
|
orgsWithConfig = []
|
||||||
|
|
||||||
|
for org in orgs:
|
||||||
|
|
||||||
|
# Get org config
|
||||||
|
statement = select(OrganizationConfig).where(
|
||||||
|
OrganizationConfig.org_id == org.id
|
||||||
)
|
)
|
||||||
result = db_session.exec(statement)
|
result = db_session.exec(statement)
|
||||||
|
|
||||||
orgs = result.all()
|
org_config = result.first()
|
||||||
|
|
||||||
|
config = OrganizationConfig.model_validate(org_config) if org_config else {}
|
||||||
|
|
||||||
|
org = OrganizationRead(**org.model_dump(), config=config)
|
||||||
|
|
||||||
|
orgsWithConfig.append(org)
|
||||||
|
|
||||||
|
return orgsWithConfig
|
||||||
|
|
||||||
return orgs #type:ignore
|
|
||||||
|
|
||||||
# Config related
|
# Config related
|
||||||
async def update_org_signup_mechanism(
|
async def update_org_signup_mechanism(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue