Organization model that maps to the orgs table
| 148 | |
| 149 | |
| 150 | class OrgModel(BaseModel): |
| 151 | """Organization model that maps to the orgs table""" |
| 152 | |
| 153 | __tablename__ = "orgs" |
| 154 | __table_args__ = {"schema": "public"} |
| 155 | |
| 156 | id = model.Column(model.UUID, primary_key=True, default=uuid.uuid4) |
| 157 | name = model.Column(model.String, nullable=False) |
| 158 | prem_status = model.Column(model.Enum(PremStatus), default=PremStatus.free, nullable=False) |
| 159 | subscription_id = model.Column(model.String, nullable=True) |
| 160 | |
| 161 | users = orm.relationship("UserOrgModel", back_populates="org", cascade="all, delete-orphan") |
| 162 | projects = orm.relationship("ProjectModel", back_populates="org") |
| 163 | invites = orm.relationship("OrgInviteModel", back_populates="org", cascade="all, delete-orphan") |
| 164 | |
| 165 | def set_current_user(self, user_id: str | UUID) -> None: |
| 166 | """ |
| 167 | Set the current user for this organization. THis is the user performing |
| 168 | the request. |
| 169 | |
| 170 | This allows us to access `current_user_role` as a property later. |
| 171 | """ |
| 172 | user_org = self.get_user_membership(user_id) |
| 173 | self._current_user_role = user_org.role |
| 174 | |
| 175 | @property |
| 176 | def current_user_role(self) -> OrgRoles: |
| 177 | """Get the current user's role in this organization.""" |
| 178 | # this property helps us render this field in the response schema |
| 179 | if not hasattr(self, "_current_user_role"): |
| 180 | raise AttributeError("Current user role is not set. Call set_current_user() first.") |
| 181 | |
| 182 | return self._current_user_role |
| 183 | |
| 184 | @property |
| 185 | def is_freeplan(self) -> bool: |
| 186 | """Check if the organization is on a free plan.""" |
| 187 | return self.prem_status == PremStatus.free |
| 188 | |
| 189 | @property |
| 190 | def current_member_count(self) -> int: |
| 191 | """The number of users that are a member of this organization.""" |
| 192 | # Use optimized count if available |
| 193 | if hasattr(self, '_member_count'): |
| 194 | return self._member_count |
| 195 | # this includes the count of invites, too, so we can prevent the admin |
| 196 | # from over-inviting users |
| 197 | if not hasattr(self, 'users') or not hasattr(self, 'invites'): |
| 198 | raise AttributeError( |
| 199 | "Member count not available. Use get_by_id or get_all_for_user to load relationships." |
| 200 | ) |
| 201 | return len(self.users) + len(self.invites) |
| 202 | |
| 203 | @property |
| 204 | def max_member_count(self) -> int | None: |
| 205 | """The maximum number of users that can be a member of this organization.""" |
| 206 | return FREEPLAN_MAX_USERS if self.is_freeplan else None |
| 207 |
no outgoing calls
searching dependent graphs…