Add a method to refresh Product stock

This commit is contained in:
Nsukami Di Kiesse Patrick 2022-11-14 12:21:00 +00:00
parent 0a36ba886d
commit 66dc01ee00
2 changed files with 14 additions and 0 deletions

View File

@ -2,6 +2,18 @@ from django.db import models
class Product(models.Model):
# https://git.disroot.org/nsukami/interview/src/branch/feat-1/src/modules/inventory/tests.py#L59
description = models.CharField(max_length=200)
unit_price = models.FloatField()
stock = models.IntegerField()
# https://git.disroot.org/nsukami/interview/src/branch/feat-1/src/modules/orders/tests.py#L111
# a product stock should be refreshed after an order is made
def refresh_from_db(self):
from modules.orders.models import OrderItem
# self.stock = self.stock - sum((oi.quantity for oi in self.orderitem_set.all()))
self.stock = self.stock - sum(
oi.quantity for oi in OrderItem.objects.filter(id=self.id)
)
self.save()

View File

@ -9,6 +9,8 @@ class ProductSerializer(serializers.ModelSerializer):
fields = "__all__"
# https://git.disroot.org/nsukami/interview/src/branch/feat-1/src/modules/inventory/tests.py#L17
# a product can be created, updated, listed but not deleted
class ProductsViewSet(
mixins.CreateModelMixin,
mixins.ListModelMixin,