Fakultas Ilmu Komputer UI

Commit 96412075 authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto
Browse files

Merge branch 'PBI-14-ProgressDietNutritionist' into 'staging'

Progress Diet Nutritionist

See merge request !74
parents 6ec0c18e 3ae3f4fe
Pipeline #79845 passed with stages
in 15 minutes and 23 seconds
from django.contrib import admin from django.contrib import admin
from .models import WeeklyReport from .models import WeeklyReport, WeeklyReportComment
admin.site.register(WeeklyReport) admin.site.register(WeeklyReport)
admin.site.register(WeeklyReportComment)
# Generated by Django 3.1 on 2021-05-30 14:01
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('diet_progress', '0004_auto_20210530_0818'),
]
operations = [
migrations.CreateModel(
name='WeeklyReportComment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('weight', models.TextField()),
('height', models.TextField()),
('waist_size', models.TextField()),
('changes_felt', models.TextField()),
('hunger_level', models.TextField()),
('fullness_level', models.TextField()),
('heavy_meal', models.TextField()),
('snacks', models.TextField()),
('average_consumption', models.TextField()),
('water_consumption', models.TextField()),
('physical_activity', models.TextField()),
('time_for_activity', models.TextField()),
('feeling_rating', models.TextField()),
('lesson_learned', models.TextField()),
('problem_faced_and_feedbacks', models.TextField()),
('weekly_report', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='weekly_report_comment', to='diet_progress.weeklyreport')),
],
),
]
...@@ -67,3 +67,30 @@ class WeeklyReport(models.Model): ...@@ -67,3 +67,30 @@ class WeeklyReport(models.Model):
choices = Rating.choices) choices = Rating.choices)
lesson_learned = models.TextField() lesson_learned = models.TextField()
problem_faced_and_feedbacks = models.TextField() problem_faced_and_feedbacks = models.TextField()
class WeeklyReportComment(models.Model):
weekly_report = models.OneToOneField(
WeeklyReport,
on_delete=models.CASCADE,
related_name='weekly_report_comment'
)
weight = models.TextField()
height = models.TextField()
waist_size = models.TextField()
changes_felt = models.TextField()
hunger_level = models.TextField()
fullness_level = models.TextField()
heavy_meal = models.TextField()
snacks = models.TextField()
average_consumption = models.TextField()
water_consumption = models.TextField()
physical_activity = models.TextField()
time_for_activity = models.TextField()
feeling_rating = models.TextField()
lesson_learned = models.TextField()
problem_faced_and_feedbacks = models.TextField()
def __str__(self):
return f"Weekly Report {self.weekly_report.id} Comment"
from rest_framework import serializers, fields from rest_framework import serializers, fields
from constants.model_choices import PhysicalActivity from constants.model_choices import PhysicalActivity
from .models import WeeklyReport from .models import WeeklyReport, WeeklyReportComment
class WeeklyReportSerializer(serializers.ModelSerializer): class WeeklyReportSerializer(serializers.ModelSerializer):
...@@ -10,3 +10,9 @@ class WeeklyReportSerializer(serializers.ModelSerializer): ...@@ -10,3 +10,9 @@ class WeeklyReportSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = WeeklyReport model = WeeklyReport
fields = "__all__" fields = "__all__"
class WeeklyReportCommentSerializer(serializers.ModelSerializer):
class Meta:
model = WeeklyReportComment
fields = "__all__"
...@@ -2,14 +2,13 @@ import json ...@@ -2,14 +2,13 @@ import json
import datetime import datetime
from unittest.mock import patch from unittest.mock import patch
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from django.test import TestCase from django.utils.encoding import force_text
from rest_framework import status from rest_framework import status
from rest_framework.response import Response from rest_framework.response import Response
from nutritionists.models import Nutritionist from nutritionists.models import Nutritionist
from authentication.models import CustomUser from authentication.models import CustomUser
from .models import WeeklyReport from .models import WeeklyReport, WeeklyReportComment
from .serializers import WeeklyReportSerializer from .serializers import WeeklyReportSerializer, WeeklyReportCommentSerializer
from .views import WeeklyReportViewSet
class WeeklyReportTest(APITestCase): class WeeklyReportTest(APITestCase):
...@@ -120,4 +119,170 @@ class WeeklyReportTest(APITestCase): ...@@ -120,4 +119,170 @@ class WeeklyReportTest(APITestCase):
reports = WeeklyReport.objects.all() reports = WeeklyReport.objects.all()
serializer = WeeklyReportSerializer(reports, many=True) serializer = WeeklyReportSerializer(reports, many=True)
self.assertEqual(response.data, serializer.data) self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
\ No newline at end of file
class WeeklyReportCommentTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.BASE_URL = "/progress/nutritionist_comment/"
cls.nutritionist_1 = Nutritionist.objects.create(
id=1,
full_name_and_degree="Test, S.Gz",
registration_certificate_no="1234567890",
university="Universitas Indonesia",
mastered_nutritional_problems="Manajemen berat badan, hipertensi",
handled_age_group="12-17 tahun (Remaja)",
another_practice_place="RSCM",
languages="Bahasa Indonesia, Bahasa Inggris",
)
cls.custom_user_1 = CustomUser.objects.create_user(
name='tes1',
email='email@email.com',
password='abc',
nutritionist=cls.nutritionist_1,
deadline = datetime.date.today(),
)
cls.custom_user_2 = CustomUser.objects.create_user(
name='tes2',
email='email2@email.com',
password='abc',
nutritionist=cls.nutritionist_1,
deadline = (datetime.date.today() + datetime.timedelta(days=3)),
)
cls.weekly_report_1 = WeeklyReport.objects.create(
nutritionist = cls.nutritionist_1,
client = cls.custom_user_1,
week_num = 1,
weight = 155,
height = 188,
waist_size = 100,
changes_felt = 1,
hunger_level = 1,
fullness_level = 1,
heavy_meal = 1,
snacks = 1,
sweet_beverages = 1,
fried_snacks = 1,
umami_snacks = 1,
sweet_snacks = 1,
fruits_portion = 1,
vegetables_portion = 1,
water_consumption = "aw",
physical_activity = [1,2],
physical_activity_other = "n",
time_for_activity = 1,
feeling_rating = 2,
lesson_learned = "a",
problem_faced_and_feedbacks = "x"
)
cls.weekly_report_2 = WeeklyReport.objects.create(
nutritionist = cls.nutritionist_1,
client = cls.custom_user_2,
week_num = 1,
weight = 155,
height = 188,
waist_size = 100,
changes_felt = 1,
hunger_level = 1,
fullness_level = 1,
heavy_meal = 1,
snacks = 1,
sweet_beverages = 1,
fried_snacks = 1,
umami_snacks = 1,
sweet_snacks = 1,
fruits_portion = 1,
vegetables_portion = 1,
water_consumption = "aw",
physical_activity = [1,2],
physical_activity_other = "n",
time_for_activity = 1,
feeling_rating = 2,
lesson_learned = "a",
problem_faced_and_feedbacks = "x"
)
cls.weekly_report_comment = WeeklyReportComment.objects.create(
weekly_report = cls.weekly_report_1,
weight = "bagus",
height = "ideal",
waist_size = "bagus",
changes_felt = "bagus",
hunger_level = "bagus",
fullness_level = "bagus",
heavy_meal = "bagus",
snacks = "bagus",
average_consumption = "bagus",
water_consumption = "bagus",
physical_activity = "bagus",
time_for_activity = "bagus",
feeling_rating = "bagus",
lesson_learned = "bagus",
problem_faced_and_feedbacks = "bagus",
)
def test_create_weekly_report_comment_succeed(self):
data = {
"weekly_report": self.weekly_report_2.id,
"weight": "bagus",
"height": "ideal",
"waist_size": "bagus",
"changes_felt": "bagus",
"hunger_level": "bagus",
"fullness_level": "bagus",
"heavy_meal": "bagus",
"snacks": "bagus",
"average_consumption": "bagus",
"water_consumption": "bagus",
"physical_activity": "bagus",
"time_for_activity": "bagus",
"feeling_rating": "bagus",
"lesson_learned": "bagus",
"problem_faced_and_feedbacks": "bagus",
}
response = self.client.post(self.BASE_URL, data, format='json')
json_response = force_text(response.content)
data['id'] = json.loads(response.content).get('id')
self.assertJSONEqual(json_response, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_create_weekly_report_comment_failed(self):
data = {}
response = self.client.post(self.BASE_URL, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_get_all_weekly_report_comment(self):
response = self.client.get(self.BASE_URL)
weekly_report_comments = WeeklyReportComment.objects.all()
serializer = WeeklyReportCommentSerializer(weekly_report_comments, many=True)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_single_weekly_report_comment_succeed(self):
url = f"{self.BASE_URL}{self.weekly_report_comment.pk}/"
response = self.client.get(url)
weekly_report_comment = WeeklyReportComment.objects.get(
pk=self.weekly_report_comment.pk)
serializer = WeeklyReportCommentSerializer(weekly_report_comment)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_single_weekly_report_comment_failed(self):
url = f"{self.BASE_URL}1303/"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_weekly_report_comment_string(self):
self.assertEqual(str(self.weekly_report_comment), f"Weekly Report {self.weekly_report_1.id} Comment")
from rest_framework import routers from rest_framework import routers
from .views import WeeklyReportViewSet from .views import WeeklyReportViewSet, WeeklyReportCommentViewSet
router = routers.SimpleRouter() router = routers.SimpleRouter()
router.register('progress/user_report', WeeklyReportViewSet) router.register('progress/user_report', WeeklyReportViewSet)
router.register('progress/nutritionist_comment', WeeklyReportCommentViewSet)
urlpatterns = [] urlpatterns = []
......
from rest_framework import viewsets, status from rest_framework import viewsets, status
from rest_framework.response import Response from rest_framework.response import Response
from authentication.utilities import get_appropriate_week_num from authentication.utilities import get_appropriate_week_num
from .models import WeeklyReport from .models import WeeklyReport, WeeklyReportComment
from .serializers import WeeklyReportSerializer from .serializers import WeeklyReportSerializer, WeeklyReportCommentSerializer
class WeeklyReportViewSet(viewsets.ModelViewSet): class WeeklyReportViewSet(viewsets.ModelViewSet):
serializer_class = WeeklyReportSerializer serializer_class = WeeklyReportSerializer
...@@ -23,3 +23,8 @@ class WeeklyReportViewSet(viewsets.ModelViewSet): ...@@ -23,3 +23,8 @@ class WeeklyReportViewSet(viewsets.ModelViewSet):
response = serializer.data response = serializer.data
return Response(response, status=status.HTTP_201_CREATED, headers=headers) return Response(response, status=status.HTTP_201_CREATED, headers=headers)
class WeeklyReportCommentViewSet(viewsets.ModelViewSet):
serializer_class = WeeklyReportCommentSerializer
queryset = WeeklyReportComment.objects.all()
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