Fakultas Ilmu Komputer UI

Commit d98d6e80 authored by Muzaki Azami's avatar Muzaki Azami
Browse files

Adding cart to weekly report

parent 53a74602
# Generated by Django 3.1 on 2021-06-28 12:37
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('payment', '0001_initial'),
('diet_progress', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='weeklyreport',
name='cart',
field=models.ForeignKey(default=3, on_delete=django.db.models.deletion.CASCADE, related_name='weekly_report', to='payment.cart'),
preserve_default=False,
),
]
# Generated by Django 3.1 on 2021-06-28 12:37
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('diet_progress', '0002_weeklyreport_cart'),
]
operations = [
migrations.RemoveField(
model_name='weeklyreport',
name='cart',
),
]
# Generated by Django 3.1 on 2021-06-28 13:39
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('payment', '0001_initial'),
('diet_progress', '0003_remove_weeklyreport_cart'),
]
operations = [
migrations.AddField(
model_name='weeklyreport',
name='cart',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='weekly_report', to='payment.cart'),
),
]
from django.db import models from django.db import models
from multiselectfield import MultiSelectField from multiselectfield import MultiSelectField
from nutritionists.models import Nutritionist from nutritionists.models import Nutritionist
from payment.models import Cart
from authentication.models import CustomUser from authentication.models import CustomUser
from constants.model_choices import( from constants.model_choices import(
LargeMealInOneDay, LargeMealInOneDay,
...@@ -24,6 +25,12 @@ class WeeklyReport(models.Model): ...@@ -24,6 +25,12 @@ class WeeklyReport(models.Model):
on_delete=models.CASCADE, on_delete=models.CASCADE,
related_name='weekly_report' related_name='weekly_report'
) )
cart = models.ForeignKey(
Cart,
on_delete=models.CASCADE,
related_name='weekly_report',
null=True
)
week_num = models.PositiveIntegerField() week_num = models.PositiveIntegerField()
......
...@@ -80,9 +80,17 @@ class WeeklyReportTest(APITestCase): ...@@ -80,9 +80,17 @@ class WeeklyReportTest(APITestCase):
program_end_date = datetime.date.today() + datetime.timedelta(days=13), program_end_date = datetime.date.today() + datetime.timedelta(days=13),
) )
cls.cart_3 = Cart.objects.create(
user = cls.custom_user_3,
program = cls.dietela_program_1,
nutritionist = cls.nutritionist_1,
program_end_date = datetime.date.today() + datetime.timedelta(days=15),
)
cls.weekly_report_1 = WeeklyReport.objects.create( cls.weekly_report_1 = WeeklyReport.objects.create(
client = cls.custom_user_2, client = cls.custom_user_2,
nutritionist = cls.nutritionist_1, nutritionist = cls.nutritionist_1,
cart = cls.cart_2,
week_num = 1, week_num = 1,
weight= 155, weight= 155,
height= 188, height= 188,
......
...@@ -4,6 +4,7 @@ from rest_framework.response import Response ...@@ -4,6 +4,7 @@ from rest_framework.response import Response
from django.db.models import Q from django.db.models import Q
from authentication.models import CustomUser from authentication.models import CustomUser
from authentication.utilities import get_appropriate_week_num from authentication.utilities import get_appropriate_week_num
from payment.models import Cart
from .models import WeeklyReport, WeeklyReportComment from .models import WeeklyReport, WeeklyReportComment
from .serializers import WeeklyReportSerializer, WeeklyReportCommentSerializer from .serializers import WeeklyReportSerializer, WeeklyReportCommentSerializer
...@@ -21,8 +22,12 @@ class WeeklyReportViewSet(viewsets.ModelViewSet): ...@@ -21,8 +22,12 @@ class WeeklyReportViewSet(viewsets.ModelViewSet):
queryset = WeeklyReport.objects.all() queryset = WeeklyReport.objects.all()
user_id = self.request.query_params.get('client_id') user_id = self.request.query_params.get('client_id')
if user_id is not None: if user_id is not None:
cart = Cart.objects.filter(user=user_id).order_by('-program_end_date')
cart = cart[0]
queryset = queryset.filter(Q(client=user_id)) queryset = queryset.filter(Q(client=user_id))
queryset = queryset.filter(Q(cart=cart.id))
if len(groups_list)==1: if len(groups_list)==1:
if groups_list[0] == 'nutritionist': if groups_list[0] == 'nutritionist':
...@@ -37,11 +42,13 @@ class WeeklyReportViewSet(viewsets.ModelViewSet): ...@@ -37,11 +42,13 @@ class WeeklyReportViewSet(viewsets.ModelViewSet):
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
deadline = self.request.user.deadline deadline = self.request.user.deadline
week_number = get_appropriate_week_num(deadline) week_number = get_appropriate_week_num(deadline)
cart = Cart.objects.filter(user=self.request.user).order_by('-program_end_date')
cart = cart[0]
request.data['week_num'] = week_number request.data['week_num'] = week_number
request.data['nutritionist'] = self.request.user.nutritionist.id request.data['nutritionist'] = self.request.user.nutritionist.id
request.data['client'] = self.request.user.id request.data['client'] = self.request.user.id
request.data['cart'] = cart.id
serializer = self.get_serializer(data=request.data) serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
self.perform_create(serializer) self.perform_create(serializer)
......
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