55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
"""Organization model."""
|
|
from app.extensions import db
|
|
from app.models.base import BaseModel
|
|
|
|
|
|
class Organization(BaseModel):
|
|
"""Organization model representing a tenant/workspace."""
|
|
|
|
__tablename__ = "organizations"
|
|
|
|
name = db.Column(db.String(255), nullable=False)
|
|
slug = db.Column(db.String(255), unique=True, nullable=False, index=True)
|
|
description = db.Column(db.Text, nullable=True)
|
|
logo_url = db.Column(db.String(512), nullable=True)
|
|
is_active = db.Column(db.Boolean, default=True, nullable=False)
|
|
|
|
# Settings (stored as JSON)
|
|
settings = db.Column(db.JSON, nullable=True, default=dict)
|
|
|
|
# Relationships
|
|
members = db.relationship(
|
|
"OrganizationMember", back_populates="organization", cascade="all, delete-orphan"
|
|
)
|
|
oidc_clients = db.relationship(
|
|
"OIDCClient", back_populates="organization", cascade="all, delete-orphan"
|
|
)
|
|
|
|
def __repr__(self):
|
|
"""String representation of Organization."""
|
|
return f"<Organization {self.name}>"
|
|
|
|
def get_member_count(self):
|
|
"""Get the count of active members in the organization."""
|
|
return len([m for m in self.members if m.deleted_at is None])
|
|
|
|
def get_owner(self):
|
|
"""Get the owner of the organization."""
|
|
from app.utils.constants import OrganizationRole
|
|
|
|
for member in self.members:
|
|
if member.role == OrganizationRole.OWNER and member.deleted_at is None:
|
|
return member.user
|
|
return None
|
|
|
|
def is_member(self, user_id):
|
|
"""Check if a user is a member of the organization."""
|
|
from app.models.organization_member import OrganizationMember
|
|
|
|
return (
|
|
OrganizationMember.query.filter_by(
|
|
user_id=user_id, organization_id=self.id, deleted_at=None
|
|
).first()
|
|
is not None
|
|
)
|