Fakultas Ilmu Komputer UI

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

Pbi 12 profile dietku tim dietela

parent f53e05f6
# Generated by Django 3.1 on 2021-05-11 08:10
from django.db import migrations
import multiselectfield.db.fields
class Migration(migrations.Migration):
dependencies = [
('diet_questionnaire', '0007_auto_20210511_0104'),
]
operations = [
migrations.AlterField(
model_name='dietquestionnaire',
name='physical_activity',
field=multiselectfield.db.fields.MultiSelectField(blank=True, choices=[(1, 'Hampir tidak pernah olahraga'), (2, 'Jogging'), (3, 'Senam aerobic, zumba, yoga, dan sejenisnya'), (4, 'Sepak bola/Futsal'), (5, 'Renang'), (6, 'Basket'), (7, 'Bulutangkis'), (8, 'Voli'), (9, 'Lainnya')], max_length=17, null=True),
),
]
# Generated by Django 3.1 on 2021-05-11 09:23
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('diet_questionnaire', '0008_auto_20210511_1510'),
]
operations = [
migrations.AlterField(
model_name='dietquestionnaire',
name='user',
field=models.OneToOneField(blank=True, max_length=200, on_delete=django.db.models.deletion.CASCADE, related_name='diet_questionnaire', to=settings.AUTH_USER_MODEL),
),
]
# Generated by Django 3.1 on 2021-05-11 09:23
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('diet_questionnaire', '0009_auto_20210511_1623'),
]
operations = [
migrations.AlterField(
model_name='dietquestionnaire',
name='user',
field=models.OneToOneField(blank=True, on_delete=django.db.models.deletion.CASCADE, related_name='diet_questionnaire', to=settings.AUTH_USER_MODEL),
),
]
...@@ -37,3 +37,41 @@ class DietQuestionnaireSerializer(serializers.ModelSerializer): ...@@ -37,3 +37,41 @@ class DietQuestionnaireSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = DietQuestionnaire model = DietQuestionnaire
fields = "__all__" fields = "__all__"
class DietQuestionnaireChoicesSerializer(serializers.ModelSerializer):
agree_to_all_statements_consent_display = serializers.CharField(source=\
'get_agree_to_all_statements_consent_display')
personal_data_consent_display = serializers.CharField(source=\
'get_personal_data_consent_display')
general_purpose_display = serializers.CharField(source=\
'get_general_purpose_display')
profession_display = serializers.CharField(source=\
'get_profession_display')
last_education_display = serializers.CharField(source=\
'get_last_education_display')
meal_preference_display = serializers.CharField(source=\
'get_meal_preference_display')
dietary_change_display = serializers.CharField(source=\
'get_dietary_change_display')
has_weigher_display = serializers.CharField(source=\
'get_has_weigher_display')
breakfast_frequency_display = serializers.CharField(source=\
'get_breakfast_frequency_display')
breakfast_meal_type_display = serializers.CharField(source=\
'get_breakfast_meal_type_display')
sweet_tea_consumption_frequency_display = serializers.CharField(source=\
'get_sweet_tea_consumption_frequency_display')
coffee_consumption_frequency_display = serializers.CharField(source=\
'get_coffee_consumption_frequency_display')
milk_consumption_frequency_display = serializers.CharField(source=\
'get_milk_consumption_frequency_display')
other_drink_consumption_frequency_display = serializers.CharField(source=\
'get_other_drink_consumption_frequency_display')
additional_sugar_in_a_day_display = serializers.CharField(source=\
'get_additional_sugar_in_a_day_display')
liquid_consumption_frequency_display = serializers.CharField(source=\
'get_liquid_consumption_frequency_display')
class Meta:
model = DietQuestionnaire
fields = "__all__"
...@@ -16,3 +16,39 @@ class QuizResultSerializer(serializers.ModelSerializer): ...@@ -16,3 +16,39 @@ class QuizResultSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = QuizResult model = QuizResult
fields = "__all__" fields = "__all__"
class DietProfileChoicesSerializer(serializers.ModelSerializer):
gender_display = serializers.CharField(source=\
'get_gender_display')
special_condition_display = serializers.CharField(source=\
'get_special_condition_display')
body_activity_display = serializers.CharField(source=\
'get_body_activity_display')
vegetables_in_one_day_display = serializers.CharField(source=\
'get_vegetables_in_one_day_display')
fruits_in_one_day_display = serializers.CharField(source=\
'get_fruits_in_one_day_display')
fried_food_in_one_day_display = serializers.CharField(source=\
'get_fried_food_in_one_day_display')
sweet_snacks_in_one_day_display = serializers.CharField(source=\
'get_sweet_snacks_in_one_day_display')
sweet_drinks_in_one_day_display = serializers.CharField(source=\
'get_sweet_drinks_in_one_day_display')
packaged_food_in_one_day_display = serializers.CharField(source=\
'get_packaged_food_in_one_day_display')
large_meal_in_one_day_display = serializers.CharField(source=\
'get_large_meal_in_one_day_display')
snacks_in_one_day_display = serializers.CharField(source=\
'get_snacks_in_one_day_display')
breakfast_type_display = serializers.CharField(source=\
'get_breakfast_type_display')
current_condition_display = serializers.CharField(source=\
'get_current_condition_display')
problem_to_solve_display = serializers.CharField(source=\
'get_problem_to_solve_display')
health_problem_display = serializers.CharField(source=\
'get_health_problem_display')
class Meta:
model = DietProfile
fields = "__all__"
...@@ -13,7 +13,7 @@ from django.db.models import Q ...@@ -13,7 +13,7 @@ from django.db.models import Q
from .models import DietRecommendation from .models import DietRecommendation
class GetProfilesTest(APITestCase): class GetProfilesAndCSVTest(APITestCase):
client = APIClient() client = APIClient()
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
...@@ -43,8 +43,30 @@ class GetProfilesTest(APITestCase): ...@@ -43,8 +43,30 @@ class GetProfilesTest(APITestCase):
cls.diet_profile_2 = DietProfile.objects.create( cls.diet_profile_2 = DietProfile.objects.create(
name="test2", name="test2",
email="test2@test.com", email="test2@test3.com",
age=20, age=22,
weight=61,
height=172,
gender=2,
special_condition=1,
body_activity=1,
vegetables_in_one_day=1,
fruits_in_one_day=1,
fried_food_in_one_day=1,
sweet_snacks_in_one_day=1,
sweet_drinks_in_one_day=1,
packaged_food_in_one_day=1,
large_meal_in_one_day=1,
snacks_in_one_day=1,
breakfast_type=1,
current_condition=1,
problem_to_solve=1,
health_problem=[2, 3])
cls.diet_profile_3 = DietProfile.objects.create(
name="test22",
email="test22@test3.com",
age=22,
weight=61, weight=61,
height=172, height=172,
gender=2, gender=2,
...@@ -124,6 +146,13 @@ class GetProfilesTest(APITestCase): ...@@ -124,6 +146,13 @@ class GetProfilesTest(APITestCase):
cls.g1 = Group.objects.create(name='nutritionist') cls.g1 = Group.objects.create(name='nutritionist')
cls.g1.user_set.add(cls.custom_user_3) cls.g1.user_set.add(cls.custom_user_3)
cls.custom_user_4 = CustomUser.objects.create_user(
name='tes',
email='tesst@test.com',
password='abc',
diet_profile=cls.diet_profile_3,
nutritionist=cls.nutritionist)
def test_get_all_users(self): def test_get_all_users(self):
response = self.client.get(self.BASE_URL) response = self.client.get(self.BASE_URL)
...@@ -199,6 +228,10 @@ class GetProfilesTest(APITestCase): ...@@ -199,6 +228,10 @@ class GetProfilesTest(APITestCase):
}) })
self.assertEqual(response.data, expected_response) self.assertEqual(response.data, expected_response)
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_csv(self):
response = self.client.get('/exportcsv/')
self.assertEqual(response.status_code, status.HTTP_200_OK)
class DietRecommendationTests(APITestCase): class DietRecommendationTests(APITestCase):
......
from rest_framework import routers from rest_framework import routers
from django.urls import path from django.urls import path
from .views import GetProfilesViewSet, DietRecommendationViewSet from .views import GetProfilesViewSet, ExportCSVTimDietela, DietRecommendationViewSet
router = routers.SimpleRouter() router = routers.SimpleRouter()
router.register('diet-recommendation', DietRecommendationViewSet) router.register('diet-recommendation', DietRecommendationViewSet)
urlpatterns = [ urlpatterns = [
path('profiles/', GetProfilesViewSet.as_view(), name='get_profiles'), path('profiles/', GetProfilesViewSet.as_view(), name='get_profiles'),
path('exportcsv/', ExportCSVTimDietela.as_view(), name='export_csv'),
] ]
urlpatterns += router.urls urlpatterns += router.urls
from dietela_quiz.serializers import DietProfileChoicesSerializer
from diet_questionnaire.serializers import DietQuestionnaireChoicesSerializer
def diet_profile_classifier(obj,field_name):
return DietProfileChoicesSerializer(obj).data[f'{field_name}_display']
def diet_questionnaire_classifier(obj,field_name):
return DietQuestionnaireChoicesSerializer(obj).data[f'{field_name}_display']
def dp_helper(obj,obj_fields):
col_list = []
num_list = ['id','weight','height','age','waist_size']
for field in obj_fields:
if field.name not in ['id','user','finished_steps']:
attribute = getattr(obj,field.name)
if (isinstance(attribute,int)) and (field.name not in num_list):
col_list.append(diet_profile_classifier(obj,field.name))
else:
col_list.append(str(attribute))
return col_list
def dq_helper(obj,obj_fields):
col_list = []
num_list = ['id','weight','height','age','waist_size']
for field in obj_fields:
if field.name not in ['id','user','finished_steps']:
attribute = getattr(obj,field.name)
if (isinstance(attribute,int)) and (field.name not in num_list):
col_list.append(diet_questionnaire_classifier(obj,field.name))
else:
col_list.append(str(attribute))
return col_list
import csv
from rest_framework.views import APIView
from rest_framework import generics, viewsets, status from rest_framework import generics, viewsets, status
from rest_framework.response import Response from rest_framework.response import Response
from django.db.models import Q from django.db.models import Q
from django.http import HttpResponse
from authentication.models import CustomUser from authentication.models import CustomUser
from authentication.serializers import CustomUserDetailsSerializer from authentication.serializers import CustomUserDetailsSerializer
from dietela_quiz.models import DietProfile
from diet_questionnaire.models import DietQuestionnaire
from .utilities import dp_helper,dq_helper
from .serializers import DietRecommendationSerializer from .serializers import DietRecommendationSerializer
from .models import DietRecommendation from .models import DietRecommendation
...@@ -52,3 +58,41 @@ class DietRecommendationViewSet(viewsets.ModelViewSet): ...@@ -52,3 +58,41 @@ class DietRecommendationViewSet(viewsets.ModelViewSet):
serializer = DietRecommendationSerializer(instances, many=True) serializer = DietRecommendationSerializer(instances, many=True)
return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.data, status=status.HTTP_200_OK)
class ExportCSVTimDietela(APIView):
def get(self, request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="export.csv"'
diet_profile_fields = DietProfile._meta.fields
diet_questionnaire_fields = DietQuestionnaire._meta.fields
field_list = []
for field in diet_profile_fields:
if field.name not in ['id','user','finished_steps']:
field_list.append(field.name)
for field in diet_questionnaire_fields:
if field.name not in ['id','user','finished_steps']:
field_list.append(field.name)
writer = csv.writer(response)
writer.writerow(field_list)
for client in CustomUser.objects.all():
client_diet_profile = client.diet_profile
client_diet_questionnaire = DietQuestionnaire.objects.filter(user=client.id)
col_list = []
if client_diet_profile is not None:
diet_profile_id = client_diet_profile.id
client_diet_profile = DietProfile.objects.get(id=diet_profile_id)
col_list.extend(dp_helper(client_diet_profile,diet_profile_fields))
if client_diet_questionnaire:
client_diet_questionnaire = DietQuestionnaire.objects.get(user=client.id)
col_list.extend(dq_helper(client_diet_questionnaire,diet_questionnaire_fields))
writer.writerow(col_list)
return response
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