Coverage for polar/models/organization_access_token.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 12:42 +0000

1from datetime import datetime 

2from uuid import UUID 

3 

4from sqlalchemy import CHAR, TIMESTAMP, ForeignKey, String, Text, Uuid 

5from sqlalchemy.orm import Mapped, declared_attr, mapped_column, relationship 

6 

7from polar.auth.scope import Scope, scope_to_set 

8from polar.kit.db.models.base import RecordModel 

9 

10from .organization import Organization 

11 

12 

13class OrganizationAccessToken(RecordModel): 

14 __tablename__ = "organization_access_tokens" 

15 

16 token: Mapped[str] = mapped_column(CHAR(64), unique=True, nullable=False) 

17 scope: Mapped[str] = mapped_column(Text, nullable=False) 

18 expires_at: Mapped[datetime | None] = mapped_column( 

19 TIMESTAMP(timezone=True), nullable=True, index=True 

20 ) 

21 comment: Mapped[str] = mapped_column(String, nullable=False) 

22 last_used_at: Mapped[datetime | None] = mapped_column( 

23 TIMESTAMP(timezone=True), nullable=True, default=None 

24 ) 

25 

26 organization_id: Mapped[UUID] = mapped_column( 

27 Uuid, ForeignKey("organizations.id"), nullable=False, index=True 

28 ) 

29 

30 @declared_attr 

31 def organization(cls) -> Mapped["Organization"]: 

32 return relationship(Organization, lazy="raise") 

33 

34 @property 

35 def scopes(self) -> set[Scope]: 

36 return scope_to_set(self.scope)