Fakultas Ilmu Komputer UI

Commit 637cf487 authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto
Browse files

Display last cart + create all_carts_are_expired attribute in user serializer

parent 56719bc2
...@@ -26,13 +26,21 @@ class UserAuthSerializer(serializers.ModelSerializer): ...@@ -26,13 +26,21 @@ class UserAuthSerializer(serializers.ModelSerializer):
result['transaction_status'] = None result['transaction_status'] = None
result['cart_id'] = None result['cart_id'] = None
result['all_carts_are_expired'] = None
if instance.cart.exists(): if instance.cart.exists():
active_carts = instance.cart.filter(program_end_date__gte=datetime.date.today()) result['transaction_status'] = instance.cart.last().transaction_status
if len(active_carts) > 0: result['cart_id'] = instance.cart.last().id
last_active_cart = active_carts.order_by('-id')[0]
result['transaction_status'] = last_active_cart.transaction_status all_carts_are_expired = True
result['cart_id'] = last_active_cart.id for cart in instance.cart.all():
if cart.program_end_date is None:
continue
if cart.program_end_date >= datetime.date.today():
all_carts_are_expired = False
break
result['all_carts_are_expired'] = all_carts_are_expired
result['nutritionist'] = None result['nutritionist'] = None
......
import json import json
import datetime
from unittest.mock import patch from unittest.mock import patch
from rest_framework import status from rest_framework import status
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
...@@ -99,7 +100,8 @@ class UserModelTests(APITestCase): ...@@ -99,7 +100,8 @@ class UserModelTests(APITestCase):
cls.cart = Cart.objects.create( cls.cart = Cart.objects.create(
program=cls.dietela_program, program=cls.dietela_program,
nutritionist=cls.nutritionist, nutritionist=cls.nutritionist,
user=cls.custom_user_1 user=cls.custom_user_1,
program_end_date=datetime.date(2021,6,28)
) )
def test_create_user(self): def test_create_user(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment