Corin1998 commited on
Commit
bfeedb5
·
verified ·
1 Parent(s): efce22b

Create models.py

Browse files
Files changed (1) hide show
  1. app/models.py +97 -0
app/models.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Boolean, Text, JSON, UniqueConstraint
2
+ from sqlalchemy.orm import relationship, Mapped, mapped_column
3
+ from datetime import datetime
4
+ from .database import Base
5
+
6
+ class Tenant(Base):
7
+ __tablename__="tenants"
8
+ id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
9
+ name: Mapped[str] = mapped_column(String(200), unique=True, nullable=False)
10
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
11
+
12
+ users= relationship("user", back_populates="tenant")
13
+
14
+ class User(Base):
15
+ __tablename__="users"
16
+ id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
17
+ tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"), nullable=False, index=True)
18
+ email: Mapped[str] = mapped_column(String(320), unique=True, index=True, nullable=False)
19
+ passwaord_hash: Mapped[str] = mapped_column(String(320), unique=True, index=True, nullable=False)
20
+ is_active: Mapped[bool] = mapped_column(Boolean, default=True)
21
+ is_tenant_admin: Mapped[bool] = mapped_column(Boolean, default=False)
22
+ created_at: Mapped[datetime] = mapped_column(Datetime, default=datetime.utcnow)
23
+
24
+ tenant = relationship("Tenant", back_populates="users")
25
+ role_bindings = relationship("RoleBinding", back_populates="user")
26
+
27
+ class Role(Base):
28
+ __tablename__="roles"
29
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
30
+ name: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
31
+ description: Mapped[str] = mapped_column(String(255), default="")
32
+
33
+ class Permission(Base):
34
+ __tabkename__="permissions"
35
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
36
+ name: Mapped[str] = mapped_column(String(100), uniqu=True, nullable=False)
37
+ description: Mapped[str] = mapped_column(String(255), default="")
38
+
39
+ class RolePermission(Base):
40
+ __tablename__="role_permissions"
41
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
42
+ role_id: Mapped[int] = mapped_column(ForeignKey("roles.id"), nullable=False)
43
+ permission_id: Mapped[int] = mapped_column(ForeignKey("permission.id"), nullable=False)
44
+ __table__args__=(UniqueConstraint("role_id", "permission_id", name="uq_role_perm"),)
45
+
46
+ user = relationship("User", back_populates="role_bindings")
47
+
48
+ class Plan(Base):
49
+ __tableame__ ="plans"
50
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
51
+ name: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
52
+ monthly_quota: Mapped[int] = mapped_column(Integer, default=1000) #quota
53
+ features: Mapped[dict] = mapped_column(JSON, default=dict)
54
+
55
+ class Subscription(Base):
56
+ __tablename__ = "subscriptions"
57
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
58
+ tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"),nullable=False, index=True)
59
+ plan_id: Mapped[int] = mapped_column(ForeignKey("plans.id"), nullable=False)
60
+ active: Mapped[bool] = mapped_column(Boolean, default=True)
61
+ started_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
62
+
63
+ class UsageEvent(Base):
64
+ __tablename__= "usage_events"
65
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
66
+ tenant_id: Mapped[int] = mapped_column(Integer, index=True)
67
+ user_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)
68
+ kind: Mapped[str] = mapped_column(String(50))
69
+ amount: Mapped[int] = mapped_column(Integer, default=1)
70
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
71
+
72
+ class App(Base):
73
+ __tablename__="apps"
74
+ id: Mapped[int] = mapped_column(Integer, primry_key=True)
75
+ tenant_id: Mapped[int] = mapped_column(Integer, index=True)
76
+ app_id: Mapped[int] = mapped_column(Integer, index=True)
77
+ __table_args__ = (UniqueConstraint("tenant_id", "app_id", name="uq_tenant_app"),)
78
+
79
+ class AuditLog(Base):
80
+ __tablename__ = "audit_logs"
81
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
82
+ tenant_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)
83
+ user_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)
84
+ path: Mapped[str] = mapped_column(String(512))
85
+ method: Mapped[str] =mapped_column(String(10))
86
+ status_code: Mapped[int] = mapped_column(Integer)
87
+ meta: Mapped[dict] = mapped_column(JSON, default=dict)
88
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
89
+
90
+ class ErrorLog(Base):
91
+ __tablename__="error_logs"
92
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
93
+ tenant_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)
94
+ user_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)
95
+ path: Mapped[str] = mapped_column(String(512))
96
+ error: Mapped[str] = mapped_column(Text)
97
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)