-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelos.py
More file actions
71 lines (55 loc) · 2.79 KB
/
modelos.py
File metadata and controls
71 lines (55 loc) · 2.79 KB
1
2
3
4
5
6
7
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
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
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from datetime import datetime
# Inicializamos la base de datos
bd = SQLAlchemy()
# Modelos de bases de datos
class Usuario(UserMixin, bd.Model):
"""Usuarios registrados en la tienda (clientes y administradores)."""
__tablename__ = "usuarios"
id = bd.Column(bd.Integer, primary_key=True)
nombre_usuario = bd.Column(bd.String(80), unique=True, nullable=False)
correo = bd.Column(bd.String(200), unique=True, nullable=False)
contraseña = bd.Column(bd.String(200), nullable=False)
es_admin = bd.Column(bd.Boolean, default=False) # Define si el usuario es administrador
pedidos = bd.relationship("Pedido", back_populates="usuario")
class Categoria(bd.Model):
"""Categorías de productos (por ejemplo: Placas, Memorias, etc.)."""
__tablename__ = "categorias"
id = bd.Column(bd.Integer, primary_key=True)
nombre = bd.Column(bd.String(100), nullable=False, unique=True)
productos = bd.relationship("Producto", back_populates="categoria")
class Producto(bd.Model):
"""Productos individuales del catálogo."""
__tablename__ = "productos"
id = bd.Column(bd.Integer, primary_key=True)
codigo = bd.Column(bd.String(50), unique=True, nullable=False)
nombre = bd.Column(bd.String(200), nullable=False)
descripcion = bd.Column(bd.Text)
precio = bd.Column(bd.Numeric(precision=10, scale=2), nullable=False)
stock = bd.Column(bd.Integer, nullable=False, default=0)
imagen = bd.Column(bd.String(300))
categoria_id = bd.Column(bd.Integer, bd.ForeignKey("categorias.id"))
categoria = bd.relationship("Categoria", back_populates="productos")
class Pedido(bd.Model):
"""Pedido o compra realizada por un usuario."""
__tablename__ = "pedidos"
id = bd.Column(bd.Integer, primary_key=True)
fecha_creacion = bd.Column(bd.DateTime, default=datetime.utcnow)
estado = bd.Column(bd.String(50), default="Pendiente")
total = bd.Column(bd.Float, nullable=False)
# Usuario que realizó la compra
usuario_id = bd.Column(bd.Integer, bd.ForeignKey("usuarios.id"))
usuario = bd.relationship("Usuario", back_populates="pedidos")
# Relación con los productos comprados
items = bd.relationship("ItemPedido", back_populates="pedido", cascade="all, delete-orphan")
class ItemPedido(bd.Model):
"""Ítem individual dentro de un pedido."""
__tablename__ = "items_pedido"
id = bd.Column(bd.Integer, primary_key=True)
producto_id = bd.Column(bd.Integer, bd.ForeignKey("productos.id"))
pedido_id = bd.Column(bd.Integer, bd.ForeignKey("pedidos.id"))
cantidad = bd.Column(bd.Integer, nullable=False)
precio = bd.Column(bd.Float, nullable=False)
producto = bd.relationship("Producto")
pedido = bd.relationship("Pedido", back_populates="items")