links.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from typing import Optional
  2. from pydantic import ConfigDict
  3. from sqlmodel import (
  4. Column,
  5. Field,
  6. ForeignKey,
  7. Integer,
  8. SQLModel,
  9. UniqueConstraint,
  10. )
  11. from gpustack.mixins import BaseModelMixin
  12. class ModelInstanceModelFileLink(SQLModel, table=True):
  13. model_instance_id: int | None = Field(
  14. default=None,
  15. sa_column=Column(
  16. Integer,
  17. ForeignKey("model_instances.id", ondelete="CASCADE"),
  18. primary_key=True,
  19. ),
  20. )
  21. model_file_id: int | None = Field(
  22. default=None,
  23. sa_column=Column(
  24. Integer,
  25. ForeignKey("model_files.id", ondelete="RESTRICT"),
  26. primary_key=True,
  27. ),
  28. )
  29. model_config = ConfigDict(protected_namespaces=())
  30. class ModelInstanceDraftModelFileLink(SQLModel, table=True):
  31. model_instance_id: int = Field(
  32. default=None,
  33. sa_column=Column(
  34. Integer,
  35. ForeignKey("model_instances.id", ondelete="CASCADE"),
  36. primary_key=True,
  37. ),
  38. )
  39. model_file_id: int = Field(
  40. default=None,
  41. sa_column=Column(
  42. Integer,
  43. ForeignKey("model_files.id", ondelete="RESTRICT"),
  44. primary_key=True,
  45. ),
  46. )
  47. model_config = ConfigDict(protected_namespaces=())
  48. class ModelRoutePrincipalLinkBase(SQLModel):
  49. route_id: int = Field(
  50. sa_column=Column(
  51. Integer,
  52. ForeignKey("model_routes.id", ondelete="CASCADE"),
  53. nullable=False,
  54. index=True,
  55. ),
  56. )
  57. principal_id: int = Field(
  58. sa_column=Column(
  59. Integer,
  60. ForeignKey("principals.id", ondelete="CASCADE"),
  61. nullable=False,
  62. ),
  63. )
  64. class ModelRoutePrincipalLink(ModelRoutePrincipalLinkBase, BaseModelMixin, table=True):
  65. """Per-route principal grants for ``ALLOWED_USERS`` and
  66. ``ALLOWED_PRINCIPALS`` access policies.
  67. Each row references a single ``principals`` row — kind (USER / ORG
  68. / GROUP) is read from the joined principals row at evaluation
  69. time. This collapses the previous polymorphic three-FK design and
  70. matches the unified principal model used everywhere else.
  71. """
  72. __tablename__ = 'model_route_principals'
  73. __table_args__ = (
  74. UniqueConstraint('route_id', 'principal_id', name='uix_route_principal'),
  75. )
  76. id: Optional[int] = Field(default=None, primary_key=True)