Corin1998 commited on
Commit
e9258c1
·
verified ·
1 Parent(s): 476331c

Update app/models.py

Browse files
Files changed (1) hide show
  1. app/models.py +34 -20
app/models.py CHANGED
@@ -1,67 +1,74 @@
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)
@@ -70,8 +77,15 @@ class UsageEvent(Base):
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"),)
@@ -82,13 +96,13 @@ class AuditLog(Base):
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)
 
1
+ from sqlalchemy import 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
+ password_hash: Mapped[str] = mapped_column(String(255), 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
+ __tablename__ = "permissions"
35
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
36
+ name: Mapped[str] = mapped_column(String(100), unique=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("permissions.id"), nullable=False)
44
+ __table_args__ = (UniqueConstraint("role_id", "permission_id", name="uq_role_perm"),)
45
+
46
+ class RoleBinding(Base):
47
+ __tablename__ = "role_bindings"
48
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
49
+ user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
50
  role_id: Mapped[int] = mapped_column(ForeignKey("roles.id"), nullable=False)
51
+ __table_args__ = (UniqueConstraint("user_id", "role_id", name="uq_user_role"),)
 
52
 
53
  user = relationship("User", back_populates="role_bindings")
54
 
55
  class Plan(Base):
56
+ __tablename__ = "plans"
57
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
58
  name: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
59
+ monthly_quota: Mapped[int] = mapped_column(Integer, default=1000)
60
  features: Mapped[dict] = mapped_column(JSON, default=dict)
61
 
62
  class Subscription(Base):
63
  __tablename__ = "subscriptions"
64
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
65
+ tenant_id: Mapped[int] = mapped_column(ForeignKey("tenants.id"), nullable=False, index=True)
66
  plan_id: Mapped[int] = mapped_column(ForeignKey("plans.id"), nullable=False)
67
  active: Mapped[bool] = mapped_column(Boolean, default=True)
68
  started_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
69
 
70
  class UsageEvent(Base):
71
+ __tablename__ = "usage_events"
72
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
73
  tenant_id: Mapped[int] = mapped_column(Integer, index=True)
74
  user_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)
 
77
  created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
78
 
79
  class App(Base):
80
+ __tablename__ = "apps"
81
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
82
+ name: Mapped[str] = mapped_column(String(100), unique=True, nullable=False)
83
+ description: Mapped[str] = mapped_column(String(255), default="")
84
+ callback_url: Mapped[str] = mapped_column(String(512), default="")
85
+
86
+ class InstalledApp(Base):
87
+ __tablename__ = "installed_apps"
88
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
89
  tenant_id: Mapped[int] = mapped_column(Integer, index=True)
90
  app_id: Mapped[int] = mapped_column(Integer, index=True)
91
  __table_args__ = (UniqueConstraint("tenant_id", "app_id", name="uq_tenant_app"),)
 
96
  tenant_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)
97
  user_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)
98
  path: Mapped[str] = mapped_column(String(512))
99
+ method: Mapped[str] = mapped_column(String(10))
100
  status_code: Mapped[int] = mapped_column(Integer)
101
  meta: Mapped[dict] = mapped_column(JSON, default=dict)
102
  created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
103
 
104
  class ErrorLog(Base):
105
+ __tablename__ = "error_logs"
106
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
107
  tenant_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)
108
  user_id: Mapped[int] = mapped_column(Integer, index=True, nullable=True)