Fakultas Ilmu Komputer UI

Commit 221de8b7 authored by Muzaki Azami's avatar Muzaki Azami
Browse files

Merge branch 'PBI-7-Checkout' into 'staging'

Pbi 7 checkout

See merge request !38
parents ffad3485 27de646d
Pipeline #71342 passed with stages
in 9 minutes and 41 seconds
......@@ -444,7 +444,7 @@ class FormulaTest(TestCase):
client_type_1 = get_program_recommendation(3, 6, [1]) # Score 22-30
client_type_2 = get_program_recommendation(4, 4, [1]) # Score 31-69
client_type_3 = get_program_recommendation(3, 1, [1]) # Score 70-79
client_type_4 = get_program_recommendation(3, 1, [2]) # Score 80-99
client_type_4 = get_program_recommendation(3, 1, [11,4]) # Score 80-99
client_type_5 = get_program_recommendation(7, 1, [8]) # Score 100-119
client_type_6 = get_program_recommendation(3, 7, [1]) # Score 120-150
client_type_7 = get_program_recommendation(3, 10, [3]) # Score 151-159
......
# Generated by Django 3.1 on 2021-04-21 15:43
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('payment', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='cart',
name='client',
),
]
from django.db import models
from authentication.models import CustomUser
from dietela_program.models import DietelaProgram
from nutritionists.models import Nutritionist
class Cart(models.Model):
client = models.ForeignKey(
CustomUser,
on_delete=models.CASCADE
)
program = models.ForeignKey(
DietelaProgram,
on_delete=models.CASCADE
......@@ -19,4 +14,4 @@ class Cart(models.Model):
)
def __str__(self):
return f"{self.client} - {self.program.unique_code} - {self.nutritionist}"
return f"{self.program.unique_code} - {self.nutritionist}"
from rest_framework import serializers
from dietela_program.serializers import DietelaProgramSerializer
from nutritionists.serializers import NutritionistSerializer
from authentication.serializers import CustomUserDetailsSerializer
from .models import Cart
class CartSerializer(serializers.ModelSerializer):
client = CustomUserDetailsSerializer()
program = DietelaProgramSerializer()
nutritionist = NutritionistSerializer()
class Meta:
model = Cart
fields = "__all__"
def to_representation(self, instance):
result = super().to_representation(instance)
result['program'] = DietelaProgramSerializer(instance.program).data
result['nutritionist'] = NutritionistSerializer(instance.nutritionist).data
return result
import json
from rest_framework.test import APITestCase
from rest_framework import status
from .models import Cart
......@@ -14,34 +15,6 @@ class CartTests(APITestCase):
def setUpTestData(cls):
cls.BASE_URL = "/payment/cart/"
cls.diet_profile_1 = DietProfile.objects.create(
name="test",
email="test@test.com",
age=22,
weight=70,
height=173,
gender=1,
special_condition=2,
body_activity=2,
vegetables_in_one_day=2,
fruits_in_one_day=2,
fried_food_in_one_day=2,
sweet_snacks_in_one_day=2,
sweet_drinks_in_one_day=2,
packaged_food_in_one_day=2,
large_meal_in_one_day=2,
snacks_in_one_day=2,
breakfast_type=2,
current_condition=2,
problem_to_solve=2,
health_problem=[1])
cls.user_1 = CustomUser.objects.create_user(
email='test@test.com',
password='tes',
diet_profile=cls.diet_profile_1,
)
cls.dietela_program_1 = DietelaProgram.objects.create(
unique_code="PRG1",
name="Program 1",
......@@ -55,6 +28,7 @@ class CartTests(APITestCase):
)
cls.nutritionist_1 = Nutritionist.objects.create(
id=1,
full_name_and_degree="Test, S.Gz",
registration_certificate_no="1234567890",
university="Universitas Indonesia",
......@@ -65,13 +39,11 @@ class CartTests(APITestCase):
)
cls.cart_1 = Cart.objects.create(
client = cls.user_1,
program = cls.dietela_program_1,
nutritionist = cls.nutritionist_1,
)
cls.cart_2 = Cart.objects.create(
client = cls.user_1,
program = cls.dietela_program_2,
nutritionist = cls.nutritionist_1,
)
......@@ -102,5 +74,17 @@ class CartTests(APITestCase):
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_cart_string(self):
self.assertEqual(str(self.cart_1), 'test@test.com - PRG1 - Test, S.Gz')
self.assertEqual(str(self.cart_1), 'PRG1 - Test, S.Gz')
def test_post_cart_api(self):
url = f"{self.BASE_URL}"
data = {
'program': 'PRG1',
'nutritionist': 1,
}
response = self.client.post(
url, data, format='json')
self.assertEqual(json.loads(response.content).get('program').get('unique_code'), 'PRG1')
self.assertEqual(Cart.objects.count(), 3)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
from rest_framework import viewsets
from rest_framework import viewsets, status
from rest_framework.response import Response
from dietela_program.serializers import DietelaProgramSerializer
from dietela_program.models import DietelaProgram
from .serializers import CartSerializer
from .models import Cart
class CartViewSet(viewsets.ModelViewSet):
serializer_class = CartSerializer
queryset = Cart.objects.all()
def create(self, request, *args, **kwargs):
program_unique_id = request.data['program']
program = DietelaProgram.objects.get(unique_code=program_unique_id)
request.data['program']=program.id
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
program = DietelaProgram.objects.get(unique_code=program_unique_id)
response = serializer.data
response['program'] = DietelaProgramSerializer(program).data
return Response(response, status=status.HTTP_201_CREATED, headers=headers)
Supports Markdown
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