Spaces:
Paused
Paused
Update app/db/models.py
Browse files- 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 |
-
#
|
| 17 |
-
class
|
| 18 |
-
__tablename__ = "
|
| 19 |
-
|
| 20 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 21 |
-
name: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
email: Mapped[str] = mapped_column(String
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
#
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|