Django_rest_api/backend/ecommerce/models.py
devfzn e3b750bd83
Creación modelos Item y Order para app Ecommerce
Clase Item con atributos stock y precio; metodos amount,
manage_stock, check_stock, place_order.
Clase Order con atributos user, item, quantity.
Registro en panel de administración.
Serializadores de Item y Order + excepción APIException del framework.
2023-03-30 15:24:14 -03:00

86 lines
2.2 KiB
Python

from django.db import models
from django.contrib.auth.models import User
from utils.model_abstracts import Model
from django_extensions.db.models import (
TimeStampedModel,
ActivatorModel,
TitleSlugDescriptionModel
)
class Item(TimeStampedModel, ActivatorModel, TitleSlugDescriptionModel, Model):
"""
ecommerce.Item
Almacena una entrada de item para la tienda
"""
class Meta:
verbose_name = 'Item'
verbose_name_plural = 'Items'
ordering = ["id"]
def __str__(self):
return self.title
stock = models.IntegerField(default=1)
price = models.IntegerField(default=0)
def amount(self):
"""
Converte el precio de centavos a libras
"""
amount = float(self.price / 100)
return amount
def manage_stock(self, qty):
"""
Reduce stock según `qty`
"""
new_stock = self.stock - int(qty)
self.stock = new_stock
self.save()
def check_stock(self, qty):
"""
Comprueba si la cantidad en order excede el stock
"""
if int(qty) > self.stock:
return False
return True
def place_order(self, user, qty):
"""
Realiza un order (pedido)
"""
if self.check_stock(qty):
order = Order.objects.create(
item = self,
quantity = qty,
user= user)
self.manage_stock(qty)
return order
else:
return None
class Order(TimeStampedModel, ActivatorModel, Model):
"""
ecommerce.Order
Almacena una entrada de order, relacionada con :model:`ecommerce.Item` y
:model:`auth.User`.
"""
class Meta:
verbose_name = 'Order'
verbose_name_plural = 'Orders'
ordering = ["id"]
user = models.ForeignKey( User, on_delete=models.CASCADE,
null=True, blank=True )
item = models.ForeignKey( Item,
null=True,
blank=True,
on_delete=models.CASCADE )
quantity = models.IntegerField(default=0)
def __str__(self):
return f'{self.user.username} - {self.item.title}'