Fred808 commited on
Commit
aebea36
·
verified ·
1 Parent(s): 0fd4e25

Update app/db/models.py

Browse files
Files changed (1) hide show
  1. app/db/models.py +170 -154
app/db/models.py CHANGED
@@ -1,155 +1,171 @@
1
- from sqlalchemy import Column, Integer, String, Boolean, DateTime, Float, ForeignKey, ARRAY, JSON, Table
2
- from sqlalchemy.orm import relationship, mapped_column, Mapped
3
- from sqlalchemy.dialects.postgresql import JSONB
4
- from datetime import datetime
5
- from typing import List, Optional
6
- from .database import Base
7
-
8
- # Association tables for many-to-many relationships
9
- user_roles = Table(
10
- 'user_roles',
11
- Base.metadata,
12
- Column('user_id', Integer, ForeignKey('users.id')),
13
- Column('role_id', Integer, ForeignKey('roles.id'))
14
- )
15
-
16
- # Add Branch model
17
- class Branch(Base):
18
- __tablename__ = "branches"
19
-
20
- id: Mapped[int] = mapped_column(primary_key=True)
21
- name: Mapped[str] = mapped_column(String, unique=True, index=True)
22
- address: Mapped[str] = mapped_column(String)
23
- phone: Mapped[str] = mapped_column(String)
24
- email: Mapped[str] = mapped_column(String)
25
- is_active: Mapped[bool] = mapped_column(Boolean, default=True)
26
- created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
27
- updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
28
-
29
- # Relationships
30
- users = relationship("User", back_populates="branch")
31
- products = relationship("Product", back_populates="branch")
32
- orders = relationship("Order", back_populates="branch")
33
-
34
- class User(Base):
35
- __tablename__ = "users"
36
-
37
- id: Mapped[int] = mapped_column(primary_key=True)
38
- email: Mapped[str] = mapped_column(String, unique=True, index=True)
39
- username: Mapped[str] = mapped_column(String, unique=True, index=True)
40
- full_name: Mapped[str] = mapped_column(String)
41
- hashed_password: Mapped[str] = mapped_column(String)
42
- is_active: Mapped[bool] = mapped_column(Boolean, default=True)
43
- is_superuser: Mapped[bool] = mapped_column(Boolean, default=False)
44
- roles: Mapped[List[str]] = mapped_column(ARRAY(String), default=list)
45
- branch_id: Mapped[Optional[int]] = mapped_column(ForeignKey("branches.id"))
46
- created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
47
-
48
- # Relationships
49
- branch = relationship("Branch", back_populates="users")
50
- products = relationship("Product", back_populates="seller")
51
- orders = relationship("Order", back_populates="customer")
52
- notifications = relationship("Notification", back_populates="user")
53
-
54
- class Product(Base):
55
- __tablename__ = "products"
56
-
57
- id: Mapped[int] = mapped_column(primary_key=True)
58
- name: Mapped[str] = mapped_column(String, index=True)
59
- description: Mapped[str] = mapped_column(String)
60
- price: Mapped[float] = mapped_column(Float)
61
- category: Mapped[str] = mapped_column(String, index=True)
62
- inventory_count: Mapped[int] = mapped_column(Integer)
63
- seller_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
64
- branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
65
- created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
66
- updated_at: Mapped[datetime] = mapped_column(
67
- DateTime,
68
- default=datetime.utcnow,
69
- onupdate=datetime.utcnow
70
- )
71
-
72
- # Relationships
73
- seller = relationship("User", back_populates="products")
74
- branch = relationship("Branch", back_populates="products")
75
- order_items = relationship("OrderItem", back_populates="product")
76
-
77
- class Order(Base):
78
- __tablename__ = "orders"
79
-
80
- id: Mapped[int] = mapped_column(primary_key=True)
81
- customer_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
82
- branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
83
- total_amount: Mapped[float] = mapped_column(Float)
84
- status: Mapped[str] = mapped_column(String)
85
- created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
86
- updated_at: Mapped[datetime] = mapped_column(
87
- DateTime,
88
- default=datetime.utcnow,
89
- onupdate=datetime.utcnow
90
- )
91
-
92
- # Relationships
93
- customer = relationship("User", back_populates="orders")
94
- branch = relationship("Branch", back_populates="orders")
95
- items = relationship("OrderItem", back_populates="order", cascade="all, delete-orphan")
96
-
97
- class OrderItem(Base):
98
- __tablename__ = "order_items"
99
-
100
- id: Mapped[int] = mapped_column(primary_key=True)
101
- order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"))
102
- product_id: Mapped[int] = mapped_column(ForeignKey("products.id"))
103
- quantity: Mapped[int] = mapped_column(Integer)
104
- price: Mapped[float] = mapped_column(Float)
105
-
106
- # Relationships
107
- order = relationship("Order", back_populates="items")
108
- product = relationship("Product", back_populates="order_items")
109
-
110
- class Notification(Base):
111
- __tablename__ = "notifications"
112
-
113
- id: Mapped[int] = mapped_column(primary_key=True)
114
- user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
115
- title: Mapped[str] = mapped_column(String)
116
- message: Mapped[str] = mapped_column(String)
117
- type: Mapped[str] = mapped_column(String)
118
- data: Mapped[Optional[dict]] = mapped_column(JSONB)
119
- created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
120
- read: Mapped[bool] = mapped_column(Boolean, default=False)
121
-
122
- # Relationship
123
- user = relationship("User", back_populates="notifications")
124
-
125
- class Event(Base):
126
- __tablename__ = "events"
127
-
128
- id: Mapped[int] = mapped_column(primary_key=True)
129
- user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
130
- title: Mapped[str] = mapped_column(String)
131
- description: Mapped[str] = mapped_column(String)
132
- start_time: Mapped[datetime] = mapped_column(DateTime)
133
- end_time: Mapped[datetime] = mapped_column(DateTime)
134
- attendees: Mapped[List[str]] = mapped_column(ARRAY(String), default=list)
135
- is_all_day: Mapped[bool] = mapped_column(Boolean, default=False)
136
- reminder_minutes: Mapped[int] = mapped_column(Integer)
137
- status: Mapped[str] = mapped_column(String)
138
- attendee_responses: Mapped[dict] = mapped_column(JSONB, default=dict)
139
- created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
140
- updated_at: Mapped[datetime] = mapped_column(
141
- DateTime,
142
- default=datetime.utcnow,
143
- onupdate=datetime.utcnow
144
- )
145
- # Fields for recurring events
146
- is_recurring: Mapped[bool] = mapped_column(Boolean, default=False)
147
- recurrence_pattern: Mapped[Optional[str]] = mapped_column(String)
148
- recurrence_group: Mapped[Optional[str]] = mapped_column(String)
149
- recurrence_end_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
150
- parent_event_id: Mapped[Optional[int]] = mapped_column(Integer)
151
- sequence_number: Mapped[Optional[int]] = mapped_column(Integer)
152
- reminder_sent: Mapped[bool] = mapped_column(Boolean, default=False)
153
-
154
- # Relationship
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  user = relationship("User")
 
1
+ from sqlalchemy import Column, Integer, String, Boolean, DateTime, Float, ForeignKey, ARRAY, JSON, Table
2
+ from sqlalchemy.orm import relationship, mapped_column, Mapped
3
+ from sqlalchemy.dialects.postgresql import JSONB
4
+ from datetime import datetime
5
+ from typing import List, Optional
6
+ from .database import Base
7
+
8
+ # Association tables for many-to-many relationships
9
+ user_roles = Table(
10
+ 'user_roles',
11
+ Base.metadata,
12
+ Column('user_id', Integer, ForeignKey('users.id')),
13
+ Column('role_id', Integer, ForeignKey('roles.id'))
14
+ )
15
+
16
+ # Role model
17
+ class Role(Base):
18
+ __tablename__ = "roles"
19
+
20
+ id: Mapped[int] = mapped_column(primary_key=True)
21
+ name: Mapped[str] = mapped_column(String, unique=True, index=True)
22
+ description: Mapped[str] = mapped_column(String)
23
+ permissions: Mapped[List[str]] = mapped_column(ARRAY(String), default=list)
24
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
25
+ updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
26
+
27
+ # Relationships
28
+ users = relationship("User", secondary=user_roles, back_populates="roles")
29
+
30
+ # Branch model
31
+ class Branch(Base):
32
+ __tablename__ = "branches"
33
+
34
+ id: Mapped[int] = mapped_column(primary_key=True)
35
+ name: Mapped[str] = mapped_column(String, unique=True, index=True)
36
+ address: Mapped[str] = mapped_column(String)
37
+ phone: Mapped[str] = mapped_column(String)
38
+ email: Mapped[str] = mapped_column(String)
39
+ is_active: Mapped[bool] = mapped_column(Boolean, default=True)
40
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
41
+ updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
42
+
43
+ # Relationships
44
+ users = relationship("User", back_populates="branch")
45
+ products = relationship("Product", back_populates="branch")
46
+ orders = relationship("Order", back_populates="branch")
47
+
48
+ class User(Base):
49
+ __tablename__ = "users"
50
+
51
+ id: Mapped[int] = mapped_column(primary_key=True)
52
+ email: Mapped[str] = mapped_column(String, unique=True, index=True)
53
+ username: Mapped[str] = mapped_column(String, unique=True, index=True)
54
+ full_name: Mapped[str] = mapped_column(String)
55
+ hashed_password: Mapped[str] = mapped_column(String)
56
+ is_active: Mapped[bool] = mapped_column(Boolean, default=True)
57
+ is_superuser: Mapped[bool] = mapped_column(Boolean, default=False)
58
+ branch_id: Mapped[Optional[int]] = mapped_column(ForeignKey("branches.id"))
59
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
60
+
61
+ # Update roles relationship to use many-to-many
62
+ roles = relationship("Role", secondary=user_roles, back_populates="users")
63
+
64
+ # Other relationships
65
+ branch = relationship("Branch", back_populates="users")
66
+ products = relationship("Product", back_populates="seller")
67
+ orders = relationship("Order", back_populates="customer")
68
+ notifications = relationship("Notification", back_populates="user")
69
+
70
+ class Product(Base):
71
+ __tablename__ = "products"
72
+
73
+ id: Mapped[int] = mapped_column(primary_key=True)
74
+ name: Mapped[str] = mapped_column(String, index=True)
75
+ description: Mapped[str] = mapped_column(String)
76
+ price: Mapped[float] = mapped_column(Float)
77
+ category: Mapped[str] = mapped_column(String, index=True)
78
+ inventory_count: Mapped[int] = mapped_column(Integer)
79
+ seller_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
80
+ branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
81
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
82
+ updated_at: Mapped[datetime] = mapped_column(
83
+ DateTime,
84
+ default=datetime.utcnow,
85
+ onupdate=datetime.utcnow
86
+ )
87
+
88
+ # Relationships
89
+ seller = relationship("User", back_populates="products")
90
+ branch = relationship("Branch", back_populates="products")
91
+ order_items = relationship("OrderItem", back_populates="product")
92
+
93
+ class Order(Base):
94
+ __tablename__ = "orders"
95
+
96
+ id: Mapped[int] = mapped_column(primary_key=True)
97
+ customer_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
98
+ branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
99
+ total_amount: Mapped[float] = mapped_column(Float)
100
+ status: Mapped[str] = mapped_column(String)
101
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
102
+ updated_at: Mapped[datetime] = mapped_column(
103
+ DateTime,
104
+ default=datetime.utcnow,
105
+ onupdate=datetime.utcnow
106
+ )
107
+
108
+ # Relationships
109
+ customer = relationship("User", back_populates="orders")
110
+ branch = relationship("Branch", back_populates="orders")
111
+ items = relationship("OrderItem", back_populates="order", cascade="all, delete-orphan")
112
+
113
+ class OrderItem(Base):
114
+ __tablename__ = "order_items"
115
+
116
+ id: Mapped[int] = mapped_column(primary_key=True)
117
+ order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"))
118
+ product_id: Mapped[int] = mapped_column(ForeignKey("products.id"))
119
+ quantity: Mapped[int] = mapped_column(Integer)
120
+ price: Mapped[float] = mapped_column(Float)
121
+
122
+ # Relationships
123
+ order = relationship("Order", back_populates="items")
124
+ product = relationship("Product", back_populates="order_items")
125
+
126
+ class Notification(Base):
127
+ __tablename__ = "notifications"
128
+
129
+ id: Mapped[int] = mapped_column(primary_key=True)
130
+ user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
131
+ title: Mapped[str] = mapped_column(String)
132
+ message: Mapped[str] = mapped_column(String)
133
+ type: Mapped[str] = mapped_column(String)
134
+ data: Mapped[Optional[dict]] = mapped_column(JSONB)
135
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
136
+ read: Mapped[bool] = mapped_column(Boolean, default=False)
137
+
138
+ # Relationship
139
+ user = relationship("User", back_populates="notifications")
140
+
141
+ class Event(Base):
142
+ __tablename__ = "events"
143
+
144
+ id: Mapped[int] = mapped_column(primary_key=True)
145
+ user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
146
+ title: Mapped[str] = mapped_column(String)
147
+ description: Mapped[str] = mapped_column(String)
148
+ start_time: Mapped[datetime] = mapped_column(DateTime)
149
+ end_time: Mapped[datetime] = mapped_column(DateTime)
150
+ attendees: Mapped[List[str]] = mapped_column(ARRAY(String), default=list)
151
+ is_all_day: Mapped[bool] = mapped_column(Boolean, default=False)
152
+ reminder_minutes: Mapped[int] = mapped_column(Integer)
153
+ status: Mapped[str] = mapped_column(String)
154
+ attendee_responses: Mapped[dict] = mapped_column(JSONB, default=dict)
155
+ created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
156
+ updated_at: Mapped[datetime] = mapped_column(
157
+ DateTime,
158
+ default=datetime.utcnow,
159
+ onupdate=datetime.utcnow
160
+ )
161
+ # Fields for recurring events
162
+ is_recurring: Mapped[bool] = mapped_column(Boolean, default=False)
163
+ recurrence_pattern: Mapped[Optional[str]] = mapped_column(String)
164
+ recurrence_group: Mapped[Optional[str]] = mapped_column(String)
165
+ recurrence_end_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
166
+ parent_event_id: Mapped[Optional[int]] = mapped_column(Integer)
167
+ sequence_number: Mapped[Optional[int]] = mapped_column(Integer)
168
+ reminder_sent: Mapped[bool] = mapped_column(Boolean, default=False)
169
+
170
+ # Relationship
171
  user = relationship("User")