Spaces:
Paused
Paused
Update app/db/schemas.py
Browse files- app/db/schemas.py +291 -257
app/db/schemas.py
CHANGED
|
@@ -1,258 +1,292 @@
|
|
| 1 |
-
from sqlalchemy.orm import validates
|
| 2 |
-
from sqlalchemy import event
|
| 3 |
-
from datetime import datetime
|
| 4 |
-
from typing import List, Optional, Dict
|
| 5 |
-
from pydantic import BaseModel, EmailStr, validator
|
| 6 |
-
from .models import User, Product, Order, Event, Notification
|
| 7 |
-
import re
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
class
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
class
|
| 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 |
-
class Config:
|
| 98 |
-
from_attributes = True
|
| 99 |
-
|
| 100 |
-
class
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
class
|
| 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 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
@validator('
|
| 185 |
-
def
|
| 186 |
-
if v is not None and
|
| 187 |
-
raise ValueError('
|
| 188 |
-
return v
|
| 189 |
-
|
| 190 |
-
class
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
@
|
| 237 |
-
def
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
Product.validate_price = validate_price
|
|
|
|
| 1 |
+
from sqlalchemy.orm import validates
|
| 2 |
+
from sqlalchemy import event
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from typing import List, Optional, Dict
|
| 5 |
+
from pydantic import BaseModel, EmailStr, validator
|
| 6 |
+
from .models import User, Product, Order, Event, Notification
|
| 7 |
+
import re
|
| 8 |
+
|
| 9 |
+
# Role schemas
|
| 10 |
+
class RoleBase(BaseModel):
|
| 11 |
+
name: str
|
| 12 |
+
description: str
|
| 13 |
+
permissions: List[str] = []
|
| 14 |
+
|
| 15 |
+
class RoleCreate(RoleBase):
|
| 16 |
+
pass
|
| 17 |
+
|
| 18 |
+
class RoleUpdate(RoleBase):
|
| 19 |
+
name: Optional[str] = None
|
| 20 |
+
description: Optional[str] = None
|
| 21 |
+
permissions: Optional[List[str]] = None
|
| 22 |
+
|
| 23 |
+
class RoleInDB(RoleBase):
|
| 24 |
+
id: int
|
| 25 |
+
created_at: datetime
|
| 26 |
+
updated_at: Optional[datetime] = None
|
| 27 |
+
|
| 28 |
+
class Config:
|
| 29 |
+
from_attributes = True
|
| 30 |
+
|
| 31 |
+
class BranchBase(BaseModel):
|
| 32 |
+
name: str
|
| 33 |
+
address: str
|
| 34 |
+
phone: str
|
| 35 |
+
email: EmailStr
|
| 36 |
+
is_active: bool = True
|
| 37 |
+
|
| 38 |
+
class BranchCreate(BranchBase):
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
class BranchInDB(BranchBase):
|
| 42 |
+
id: int
|
| 43 |
+
created_at: datetime
|
| 44 |
+
updated_at: Optional[datetime] = None
|
| 45 |
+
|
| 46 |
+
class Config:
|
| 47 |
+
from_attributes = True
|
| 48 |
+
|
| 49 |
+
# Update User schemas
|
| 50 |
+
class UserBase(BaseModel):
|
| 51 |
+
email: EmailStr
|
| 52 |
+
username: str
|
| 53 |
+
full_name: str
|
| 54 |
+
is_active: bool = True
|
| 55 |
+
is_superuser: bool = False
|
| 56 |
+
branch_id: Optional[int] = None
|
| 57 |
+
|
| 58 |
+
class UserCreate(UserBase):
|
| 59 |
+
password: str
|
| 60 |
+
role_ids: Optional[List[int]] = None # IDs of roles to assign
|
| 61 |
+
|
| 62 |
+
class UserUpdate(BaseModel):
|
| 63 |
+
email: Optional[EmailStr] = None
|
| 64 |
+
username: Optional[str] = None
|
| 65 |
+
full_name: Optional[str] = None
|
| 66 |
+
is_active: Optional[bool] = None
|
| 67 |
+
is_superuser: Optional[bool] = None
|
| 68 |
+
password: Optional[str] = None
|
| 69 |
+
branch_id: Optional[int] = None
|
| 70 |
+
role_ids: Optional[List[int]] = None
|
| 71 |
+
|
| 72 |
+
class UserInDB(UserBase):
|
| 73 |
+
id: int
|
| 74 |
+
created_at: datetime
|
| 75 |
+
roles: List[RoleInDB]
|
| 76 |
+
|
| 77 |
+
class Config:
|
| 78 |
+
from_attributes = True
|
| 79 |
+
|
| 80 |
+
class ProductBase(BaseModel):
|
| 81 |
+
name: str
|
| 82 |
+
description: str
|
| 83 |
+
price: float
|
| 84 |
+
category: str
|
| 85 |
+
inventory_count: int
|
| 86 |
+
seller_id: int
|
| 87 |
+
branch_id: int
|
| 88 |
+
|
| 89 |
+
class ProductCreate(ProductBase):
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
class ProductInDB(ProductBase):
|
| 93 |
+
id: int
|
| 94 |
+
created_at: datetime
|
| 95 |
+
updated_at: Optional[datetime] = None
|
| 96 |
+
|
| 97 |
+
class Config:
|
| 98 |
+
from_attributes = True
|
| 99 |
+
|
| 100 |
+
class OrderItemBase(BaseModel):
|
| 101 |
+
product_id: int
|
| 102 |
+
quantity: int
|
| 103 |
+
price: float
|
| 104 |
+
|
| 105 |
+
class OrderItemCreate(OrderItemBase):
|
| 106 |
+
pass
|
| 107 |
+
|
| 108 |
+
class OrderItemInDB(OrderItemBase):
|
| 109 |
+
id: int
|
| 110 |
+
order_id: int
|
| 111 |
+
|
| 112 |
+
class Config:
|
| 113 |
+
from_attributes = True
|
| 114 |
+
|
| 115 |
+
class OrderBase(BaseModel):
|
| 116 |
+
customer_id: int
|
| 117 |
+
branch_id: int
|
| 118 |
+
total_amount: float
|
| 119 |
+
status: str = "pending"
|
| 120 |
+
items: List[OrderItemCreate]
|
| 121 |
+
|
| 122 |
+
class OrderCreate(OrderBase):
|
| 123 |
+
pass
|
| 124 |
+
|
| 125 |
+
class OrderInDB(OrderBase):
|
| 126 |
+
id: int
|
| 127 |
+
created_at: datetime
|
| 128 |
+
updated_at: Optional[datetime] = None
|
| 129 |
+
items: List[OrderItemInDB]
|
| 130 |
+
|
| 131 |
+
class Config:
|
| 132 |
+
from_attributes = True
|
| 133 |
+
|
| 134 |
+
class NotificationBase(BaseModel):
|
| 135 |
+
user_id: int
|
| 136 |
+
title: str
|
| 137 |
+
message: str
|
| 138 |
+
type: str
|
| 139 |
+
data: Optional[dict] = None
|
| 140 |
+
read: bool = False
|
| 141 |
+
|
| 142 |
+
class NotificationCreate(NotificationBase):
|
| 143 |
+
pass
|
| 144 |
+
|
| 145 |
+
class NotificationInDB(NotificationBase):
|
| 146 |
+
id: int
|
| 147 |
+
created_at: datetime
|
| 148 |
+
|
| 149 |
+
class Config:
|
| 150 |
+
from_attributes = True
|
| 151 |
+
|
| 152 |
+
class EventBase(BaseModel):
|
| 153 |
+
title: str
|
| 154 |
+
description: str
|
| 155 |
+
start_time: datetime
|
| 156 |
+
end_time: datetime
|
| 157 |
+
is_all_day: bool = False
|
| 158 |
+
reminder_minutes: int = 30
|
| 159 |
+
|
| 160 |
+
@validator('end_time')
|
| 161 |
+
def end_time_after_start_time(cls, v, values):
|
| 162 |
+
if 'start_time' in values and v <= values['start_time']:
|
| 163 |
+
raise ValueError('end_time must be after start_time')
|
| 164 |
+
return v
|
| 165 |
+
|
| 166 |
+
@validator('reminder_minutes')
|
| 167 |
+
def valid_reminder_minutes(cls, v):
|
| 168 |
+
if v < 0:
|
| 169 |
+
raise ValueError('reminder_minutes cannot be negative')
|
| 170 |
+
return v
|
| 171 |
+
|
| 172 |
+
class EventCreate(EventBase):
|
| 173 |
+
attendees: List[str] = []
|
| 174 |
+
|
| 175 |
+
class EventUpdate(BaseModel):
|
| 176 |
+
title: Optional[str] = None
|
| 177 |
+
description: Optional[str] = None
|
| 178 |
+
start_time: Optional[datetime] = None
|
| 179 |
+
end_time: Optional[datetime] = None
|
| 180 |
+
is_all_day: Optional[bool] = None
|
| 181 |
+
reminder_minutes: Optional[int] = None
|
| 182 |
+
attendees: Optional[List[str]] = None
|
| 183 |
+
|
| 184 |
+
@validator('reminder_minutes')
|
| 185 |
+
def valid_reminder_minutes(cls, v):
|
| 186 |
+
if v is not None and v < 0:
|
| 187 |
+
raise ValueError('reminder_minutes cannot be negative')
|
| 188 |
+
return v
|
| 189 |
+
|
| 190 |
+
class EventInDB(EventBase):
|
| 191 |
+
id: int
|
| 192 |
+
user_id: int
|
| 193 |
+
attendees: List[str]
|
| 194 |
+
status: str
|
| 195 |
+
attendee_responses: Dict[str, str]
|
| 196 |
+
created_at: datetime
|
| 197 |
+
updated_at: Optional[datetime] = None
|
| 198 |
+
reminder_sent: bool = False
|
| 199 |
+
is_recurring: bool = False
|
| 200 |
+
recurrence_group: Optional[str] = None
|
| 201 |
+
parent_event_id: Optional[int] = None
|
| 202 |
+
sequence_number: Optional[int] = None
|
| 203 |
+
|
| 204 |
+
class Config:
|
| 205 |
+
from_attributes = True
|
| 206 |
+
|
| 207 |
+
class RecurringEventCreate(EventCreate):
|
| 208 |
+
recurrence_pattern: str
|
| 209 |
+
recurrence_end_date: Optional[datetime] = None
|
| 210 |
+
|
| 211 |
+
@validator('recurrence_pattern')
|
| 212 |
+
def valid_recurrence_pattern(cls, v):
|
| 213 |
+
valid_patterns = ['daily', 'weekly', 'monthly', 'yearly']
|
| 214 |
+
if v not in valid_patterns:
|
| 215 |
+
raise ValueError(f'recurrence_pattern must be one of: {", ".join(valid_patterns)}')
|
| 216 |
+
return v
|
| 217 |
+
|
| 218 |
+
@validator('recurrence_end_date')
|
| 219 |
+
def end_date_after_start_time(cls, v, values):
|
| 220 |
+
if v is not None and 'start_time' in values and v <= values['start_time']:
|
| 221 |
+
raise ValueError('recurrence_end_date must be after start_time')
|
| 222 |
+
return v
|
| 223 |
+
|
| 224 |
+
class RecurringEventUpdate(EventUpdate):
|
| 225 |
+
recurrence_pattern: Optional[str] = None
|
| 226 |
+
recurrence_end_date: Optional[datetime] = None
|
| 227 |
+
|
| 228 |
+
@validator('recurrence_pattern')
|
| 229 |
+
def valid_recurrence_pattern(cls, v):
|
| 230 |
+
if v is not None:
|
| 231 |
+
valid_patterns = ['daily', 'weekly', 'monthly', 'yearly']
|
| 232 |
+
if v not in valid_patterns:
|
| 233 |
+
raise ValueError(f'recurrence_pattern must be one of: {", ".join(valid_patterns)}')
|
| 234 |
+
return v
|
| 235 |
+
|
| 236 |
+
@validates('email')
|
| 237 |
+
def validate_email(self, key, email):
|
| 238 |
+
if not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', email):
|
| 239 |
+
raise ValueError('Invalid email address')
|
| 240 |
+
return email
|
| 241 |
+
|
| 242 |
+
@validates('username')
|
| 243 |
+
def validate_username(self, key, username):
|
| 244 |
+
if len(username) < 3:
|
| 245 |
+
raise ValueError('Username must be at least 3 characters long')
|
| 246 |
+
return username
|
| 247 |
+
|
| 248 |
+
@validates('inventory_count')
|
| 249 |
+
def validate_inventory(self, key, count):
|
| 250 |
+
if count < 0:
|
| 251 |
+
raise ValueError('Inventory count cannot be negative')
|
| 252 |
+
return count
|
| 253 |
+
|
| 254 |
+
@validates('price')
|
| 255 |
+
def validate_price(self, key, price):
|
| 256 |
+
if price < 0:
|
| 257 |
+
raise ValueError('Price cannot be negative')
|
| 258 |
+
return price
|
| 259 |
+
|
| 260 |
+
# Event listeners for automatic timestamps
|
| 261 |
+
@event.listens_for(Product, 'before_insert')
|
| 262 |
+
def set_created_at(mapper, connection, target):
|
| 263 |
+
target.created_at = datetime.utcnow()
|
| 264 |
+
target.updated_at = datetime.utcnow()
|
| 265 |
+
|
| 266 |
+
@event.listens_for(Product, 'before_update')
|
| 267 |
+
def set_updated_at(mapper, connection, target):
|
| 268 |
+
target.updated_at = datetime.utcnow()
|
| 269 |
+
|
| 270 |
+
@event.listens_for(Order, 'before_insert')
|
| 271 |
+
def set_order_created_at(mapper, connection, target):
|
| 272 |
+
target.created_at = datetime.utcnow()
|
| 273 |
+
target.updated_at = datetime.utcnow()
|
| 274 |
+
|
| 275 |
+
@event.listens_for(Order, 'before_update')
|
| 276 |
+
def set_order_updated_at(mapper, connection, target):
|
| 277 |
+
target.updated_at = datetime.utcnow()
|
| 278 |
+
|
| 279 |
+
@event.listens_for(Event, 'before_insert')
|
| 280 |
+
def set_event_created_at(mapper, connection, target):
|
| 281 |
+
target.created_at = datetime.utcnow()
|
| 282 |
+
target.updated_at = datetime.utcnow()
|
| 283 |
+
|
| 284 |
+
@event.listens_for(Event, 'before_update')
|
| 285 |
+
def set_event_updated_at(mapper, connection, target):
|
| 286 |
+
target.updated_at = datetime.utcnow()
|
| 287 |
+
|
| 288 |
+
# Add validators to models
|
| 289 |
+
User.validate_email = validate_email
|
| 290 |
+
User.validate_username = validate_username
|
| 291 |
+
Product.validate_inventory = validate_inventory
|
| 292 |
Product.validate_price = validate_price
|