Fakultas Ilmu Komputer UI

Commit f18b2f8b authored by Ami's avatar Ami
Browse files

[GREEN] implementing export csv and utils

parent dd7a0c6a
Pipeline #76382 failed with stages
in 11 minutes and 23 seconds
......@@ -28,3 +28,41 @@ class DietQuestionnaireSerializer(serializers.ModelSerializer):
class Meta:
model = DietQuestionnaire
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):
class Meta:
model = QuizResult
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__"
......@@ -8,7 +8,6 @@ from authentication.models import CustomUser
from nutritionists.models import Nutritionist
from diet_questionnaire.models import DietQuestionnaire
from authentication.serializers import CustomUserDetailsSerializer
from .utilities import classifier
from django.db.models import Q
class GetProfilesAndCSVTest(APITestCase):
......@@ -222,377 +221,3 @@ class GetProfilesAndCSVTest(APITestCase):
def test_get_csv(self):
response = self.client.get('/exportcsv/')
self.assertEqual(response.status_code, status.HTTP_200_OK)
class ClassifierUtilitiesTest(TestCase):
def test_gender(self):
field_name = 'gender'
list_of_attributes = ['Laki-laki','Perempuan']
list_of_int_choices = [1,2]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_special_condition(self):
field_name = 'special_condition'
list_of_attributes = [
'Tidak ada',
'Hamil Trimester 1',
'Hamil Trimester 2',
'Hamil Trimester 3',
'Menyusui Bayi 0-6 Bulan',
'Menyusui Bayi 7-12 Bulan'
]
list_of_int_choices = [1,2,3,4,5,6]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_body_activity(self):
field_name = 'body_activity'
list_of_attributes = [
'Lebih banyak duduk dan tidak rutin olahraga',
'Banyak jalan: return banyak bergerak: return tetapi tidak rutin olahraga',
'Olahraga rutin 30-45 menit per hari',
'Olahraga rutin 45-60 menit per hari',
'Olahraga rutin lebih dari 60 menit per hari',
]
list_of_int_choices = [1,2,3,4,5]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_vegetables_in_one_day(self):
field_name = 'vegetables_in_one_day'
list_of_attributes = [
'0 porsi',
'1 porsi',
'2 porsi',
'3 porsi',
'Lebih dari 3 porsi'
]
list_of_int_choices = [1,2,3,4,5]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_fruits_in_one_day(self):
field_name = 'fruits_in_one_day'
list_of_attributes = [
'0 porsi',
'1 porsi',
'2 porsi',
'3 porsi',
'Lebih dari 3 porsi'
]
list_of_int_choices = [1,2,3,4,5]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_fried_food_in_one_day(self):
field_name = 'fried_food_in_one_day'
list_of_attributes = [
'0 potong',
'1 potong',
'2 potong',
'3 potong',
'Lebih dari 3 potong'
]
list_of_int_choices = [1,2,3,4,5]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_sweet_snacks_in_one_day(self):
field_name = 'sweet_snacks_in_one_day'
list_of_attributes = [
'0 potong',
'1 potong',
'2 potong',
'3 potong',
'Lebih dari 3 potong'
]
list_of_int_choices = [1,2,3,4,5]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_sweet_drinks_in_one_day(self):
field_name = 'sweet_drinks_in_one_day'
list_of_attributes = [
'0 gelas',
'1 gelas',
'2 gelas',
'3 gelas',
'Lebih dari 3 gelas'
]
list_of_int_choices = [1,2,3,4,5]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_packaged_food_in_one_day(self):
field_name = 'packaged_food_in_one_day'
list_of_attributes = [
'0 porsi',
'1 porsi',
'2 porsi',
'3 porsi',
'Lebih dari 3 porsi'
]
list_of_int_choices = [1,2,3,4,5]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_large_meal_in_one_day(self):
field_name = 'large_meal_in_one_day'
list_of_attributes = [
'1 kali',
'2 kali',
'3 kali',
'Lebih dari 3 kali'
]
list_of_int_choices = [1,2,3,4]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_snacks_in_one_day(self):
field_name = 'snacks_in_one_day'
list_of_attributes = [
'Tidak pernah',
'1 kali',
'2 kali',
'3 kali',
'Lebih dari 3 kali'
]
list_of_int_choices = [1,2,3,4,5]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_breakfast_type(self):
field_name = 'breakfast_type'
list_of_attributes = [
'Tidak pernah makan pagi',
'Makanan yang ringan saja',
'Makanan yang sampai mengenyangkan perut'
]
list_of_int_choices = [1,2,3]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_current_condition(self):
field_name = 'current_condition'
list_of_attributes = [
'Saya belum tertarik melakukan diet dan gaya hidup sehat dan tidak tahu kondisi gizi serta kesehatan saya',
'Saya belum tertarik melakukan diet dan gaya hidup sehat meskipun saya tahu kondisi gizi dan kesehatan saya',
'Saya mulai tertarik melakukan diet dan gaya hidup sehat meskipun saya tidak tahu kondisi gizi dan kesehatan saya',
'Saya mulai tertarik melakukan diet dan gaya hidup sehat dan tahu kondisi gizi dan kesehatan saya',
'Saya sudah tahu manfaat diet dan gaya hidup sehat namun belum tahu harus mulai menerapkannya dari mana',
'Saya sudah pernah merubah diet dan gaya hidup menjadi lebih sehat tetapi tidak bertahan lama (dilakukan kurang dari 6 bulan)',
'Saya sudah pernah berhasil menjalani diet dan gaya hidup sehat selama lebih dari 6 bulan dan ingin melakukannya lagi'
]
list_of_int_choices = [1,2,3,4,5,6,7]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_problem_to_solve(self):
field_name = 'problem_to_solve'
list_of_attributes = [
'Turun berat badan dengan target yang realistis',
'Naik berat badan dengan target yang realistis',
'Berat badan yang bertahan permanen: return menjaga agar berat badan tidak naik: return anti yo-yo diet',
'Butuh pengaturan gizi Ibu Hamil',
'Butuh pengaturan gizi Ibu Menyusui',
'Menyiapkan tubuh untuk mencapai kesuburan dan persiapan kehamilan',
'Mengatur gula darah tinggi (Pre-diabetes): return hiperlipidemia (kolesterol): return hipertensi (tekanan darah tinggi)',
'Capai pola makan sehat dan tetap enjoy menikmati makanan yang disukai',
'Transisi vegan',
'Butuh pengaturan gizi pada anak-anak atau remaja'
]
list_of_int_choices = [1,2,3,4,5,6,7,8,9,10]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_agree_to_all_statements_consent(self):
field_name = 'agree_to_all_statements_consent'
list_of_attributes = ['Ya','Tidak']
list_of_int_choices = [1,2]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_personal_data_consent(self):
field_name = 'personal_data_consent'
list_of_attributes = ['Ya','Tidak']
list_of_int_choices = [1,2]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_general_purpose(self):
field_name = 'general_purpose'
list_of_attributes = ['Mencapai Body Goals','Mencapai Hidup Sehat Jangka Panjang']
list_of_int_choices = [1,2]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_profession(self):
field_name = 'profession'
list_of_attributes = [
'Pegawai Swasta',
'PNS',
'Wirausaha',
'Self-Employed',
'Freelancer',
'Ibu Rumah Tangga',
'Mahasiswa',
'Pelajar (selain mahasiswa)',
'Lainnya'
]
list_of_int_choices = [1,2,3,4,5,6,7,8,9]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_last_education(self):
field_name = 'last_education'
list_of_attributes = [
'S2 atau S3',
'S1',
'SMA',
'SMP',
'SD',
'Lainnya'
]
list_of_int_choices = [1,2,3,4,5,6]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_meal_preference(self):
field_name = 'meal_preference'
list_of_attributes = [
'Tidak ada batasan',
'Halal',
'Vegetarian',
'Lacto-Ovo Vegetarian',
'Ovo-Vegetarian',
'Vegan'
]
list_of_int_choices = [1,2,3,4,5,6]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_dietary_change(self):
field_name = 'dietary_change'
list_of_attributes = ['Diet ketat','Dimulai dari perubahan bertahap saja dulu']
list_of_int_choices = [1,2]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_has_weigher(self):
field_name = 'has_weigher'
list_of_attributes = ['Ya','Tidak']
list_of_int_choices = [1,2]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_breakfast_frequency(self):
field_name = 'breakfast_frequency'
list_of_attributes = ['Selalu','Kadang-Kadang','Tidak pernah']
list_of_int_choices = [1,2]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_breakfast_meal_type(self):
field_name = 'breakfast_meal_type'
list_of_attributes = [
'Makanan yang ringan saja: return hanya untuk mengganjal perut sebelum beraktivitas (misalnya: roti isi: return cemilan tradisional seperti lontong: return gorengan: return atau kue-kue jajanan pasar)',
'Makanan yang mengenyangkan perut (nasi atau roti atau kentang dengan lauk-pauk)']
list_of_int_choices = [1,2]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_sweet_tea_consumption_frequency(self):
field_name = 'sweet_tea_consumption_frequency'
list_of_attributes = [
'Tidak pernah',
'Sangat jarang',
'Tidak lebih dari 3 gelas per bulan',
'1 gelas/minggu',
'2 gelas atau lebih/minggu',
'Hampir setiap hari',
'1 gelas (250 ml)/hari',
'2 gelas (500 ml)/hari',
'Lebih dari 2 gelas/hari'
]
list_of_int_choices = [1,2,3,4,5,6,7,8,9]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_coffee_consumption_frequency(self):
field_name = 'coffee_consumption_frequency'
list_of_attributes = [
'Tidak pernah',
'Sangat jarang',
'Tidak lebih dari 3 gelas per bulan',
'1 gelas/minggu',
'2 gelas atau lebih/minggu',
'Hampir setiap hari',
'1 gelas (250 ml)/hari',
'2 gelas (500 ml)/hari',
'Lebih dari 2 gelas/hari'
]
list_of_int_choices = [1,2,3,4,5,6,7,8,9]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_milk_consumption_frequency(self):
field_name = 'milk_consumption_frequency'
list_of_attributes = [
'Tidak pernah',
'Sangat jarang',
'Tidak lebih dari 3 gelas per bulan',
'1 gelas/minggu',
'2 gelas atau lebih/minggu',
'Hampir setiap hari',
'1 gelas (250 ml)/hari',
'2 gelas (500 ml)/hari',
'Lebih dari 2 gelas/hari'
]
list_of_int_choices = [1,2,3,4,5,6,7,8,9]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_other_drink_consumption_frequency(self):
field_name = 'other_drink_consumption_frequency'
list_of_attributes = [
'Tidak pernah',
'Sangat jarang',
'Tidak lebih dari 3 gelas per bulan',
'1 gelas/minggu',
'2 gelas atau lebih/minggu',
'Hampir setiap hari',
'1 gelas (250 ml)/hari',
'2 gelas (500 ml)/hari',
'Lebih dari 2 gelas/hari'
]
list_of_int_choices = [1,2,3,4,5,6,7,8,9]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_additional_sugar_in_a_day(self):
field_name = 'additional_sugar_in_a_day'
list_of_attributes = [
'Tidak pernah menggunakan gula tambahan',
'1 sendok teh',
'2 sendok teh',
'Lebih dari 2 sendok teh',
'1 sendok makan',
'2 sendok makan',
'Lebih dari 2 sendok makan'
]
list_of_int_choices = [1,2,3,4,5,6,7]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
def test_liquid_consumption_frequency(self):
field_name = 'liquid_consumption_frequency'
list_of_attributes = [
'8 gelas/hari atau 2 liter/hari',
'Kurang dari 8 gelas/hari atau kurang dari 2 liter/hari',
'Lebih dari 8 gelas/hari atau lebih dari 2 liter/hari'
]
list_of_int_choices = [1,2,3]
for i in range(len(list_of_int_choices)):
self.assertEqual(list_of_attributes[i],classifier(list_of_int_choices[i],field_name))
self.assertEqual(None,classifier(4,'a')) # For some reason coverage demands a case where classifier doesn't return annything
\ No newline at end of file
from rest_framework import routers
from django.urls import path
from .views import GetProfilesViewSet
from .views import GetProfilesViewSet, ExportCSVTimDietela
router = routers.SimpleRouter()
urlpatterns = [
path('profiles/', GetProfilesViewSet.as_view(), name='get_profiles'),
path('exportcsv/', ExportCSVTimDietela.as_view(), name='export_csv'),
]
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 import generics
from rest_framework.views import APIView
from django.db.models import Q
from django.http import HttpResponse
from authentication.models import CustomUser
from authentication.serializers import CustomUserDetailsSerializer
from dietela_quiz.models import DietProfile
from diet_questionnaire.models import DietQuestionnaire
from .utilities import dp_helper,dq_helper
class GetProfilesViewSet(generics.ListAPIView):
......@@ -22,3 +28,41 @@ class GetProfilesViewSet(generics.ListAPIView):
queryset = queryset.filter(
Q(nutritionist=nutritionist_id) & ~Q(id=self.request.user.id))
return queryset
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