Fakultas Ilmu Komputer UI

Commit 3c24d673 authored by Muzaki Azami's avatar Muzaki Azami
Browse files

Pbi 10 profile dietku nutritionist

parent 349861ff
......@@ -11,7 +11,7 @@ pylint:
- pip install -r requirements.txt
when: on_success
script:
- pylint --load-plugins pylint_django --rcfile=./.pylintrc dietela_backend dietela_program dietela_quiz nutritionists authentication payment
- pylint --load-plugins pylint_django --rcfile=./.pylintrc dietela_backend dietela_program dietela_quiz nutritionists authentication payment profile_dietku
UnitTest:
image: python:3.6.5
......@@ -29,7 +29,7 @@ UnitTest:
- coverage erase
- SSLCONF="?sslmode=require"
- echo "$TEST_DATABASE_URL$SSLCONF"
- DATABASE_URL="$TEST_DATABASE_URL$SSLCONF" coverage run --branch --source=dietela_quiz,nutritionists,dietela_program,authentication,payment manage.py test --keepdb
- DATABASE_URL="$TEST_DATABASE_URL$SSLCONF" coverage run --branch --source=dietela_quiz,nutritionists,dietela_program,authentication,payment,profile_dietku manage.py test --keepdb
- coverage xml -i
- coverage report -m
artifacts:
......
# Generated by Django 3.1 on 2021-05-08 02:07
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('nutritionists', '0002_nutritionist_profile_picture'),
('authentication', '0002_customuser_name'),
]
operations = [
migrations.AddField(
model_name='customuser',
name='nutritionist',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='nutritionists.nutritionist'),
),
]
# Generated by Django 3.1 on 2021-05-08 02:20
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('nutritionists', '0002_nutritionist_profile_picture'),
('authentication', '0003_customuser_nutritionist'),
]
operations = [
migrations.AlterField(
model_name='customuser',
name='nutritionist',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='clients', to='nutritionists.nutritionist'),
),
]
......@@ -5,6 +5,7 @@ from django.utils.translation import gettext_lazy as _
from django.utils import timezone
from dietela_quiz.models import DietProfile
from nutritionists.models import Nutritionist
from .managers import CustomUserManager
class CustomUser(AbstractBaseUser, PermissionsMixin):
......@@ -20,6 +21,13 @@ class CustomUser(AbstractBaseUser, PermissionsMixin):
related_name="user",
blank=True, null=True
)
nutritionist = models.ForeignKey(
Nutritionist,
on_delete=models.CASCADE,
related_name="clients",
blank=True,
null=True
)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
......
......@@ -57,6 +57,7 @@ INSTALLED_APPS = [
'dietela_program',
'authentication',
'payment',
'profile_dietku',
# auth
'rest_framework.authtoken',
......
......@@ -8,4 +8,5 @@ urlpatterns = [
path('', include('dietela_program.urls')),
path('', include('payment.urls')),
path('', include('authentication.urls')),
path('', include('profile_dietku.urls')),
]
......@@ -7,3 +7,4 @@ class NutritionistSerializer(serializers.ModelSerializer):
class Meta:
model = Nutritionist
fields = "__all__"
\ No newline at end of file
#from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class ProfileDietkuConfig(AppConfig):
name = 'profile_dietku'
import json
from django.test import TestCase
from django.contrib.auth.models import Group
from rest_framework.test import APITestCase, APIClient
from rest_framework import status
from dietela_quiz.models import DietProfile
from authentication.models import CustomUser
from nutritionists.models import Nutritionist
from authentication.serializers import CustomUserDetailsSerializer
from django.db.models import Q
class GetProfilesTest(APITestCase):
client = APIClient()
@classmethod
def setUpTestData(cls):
cls.BASE_URL = "/profiles/"
cls.SPECIFIC_URL = "/profiles/?nutritionist=1"
cls.diet_profile = DietProfile.objects.create(
name="test",
email="test@test.com",
age=20,
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.nutritionist = 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='tes',
email='test@test.com',
password='abc',
diet_profile=cls.diet_profile,
nutritionist=cls.nutritionist)
cls.custom_user_2 = CustomUser.objects.create_user(name='tes', email='email2@gmail.com', password='abc')
cls.custom_user_3 = CustomUser.objects.create_user(
name='tesb',
email='test2@2test.com',
password='abc',
nutritionist=cls.nutritionist)
cls.g0 = Group.objects.get(name='client')
cls.g0.user_set.remove(cls.custom_user_3)
cls.g1 = Group.objects.create(name='nutritionist')
cls.g1.user_set.add(cls.custom_user_3)
def test_get_all_users(self):
response = self.client.get(self.BASE_URL)
users = CustomUser.objects.all()
serializer = CustomUserDetailsSerializer(users, many=True)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_user_belonging_to_a_nutritionist(self):
response = self.client.get(self.BASE_URL,secure=True)
data = {
'email': 'test2@2test.com',
'password': 'abc',
'role': 'nutritionist',
}
response = self.client.post('/auth/user-login/', data, format='json')
json_response = json.loads(response.content)
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + json_response['access_token'])
response = self.client.get(self.BASE_URL,secure=True)
users = CustomUser.objects.filter(Q(nutritionist=1) & ~Q(email='test2@2test.com'))
serializer = CustomUserDetailsSerializer(users, many=True)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_user_by_non_nutritionist(self):
response = self.client.get(self.BASE_URL,secure=True)
data = {
'email': 'email2@gmail.com',
'password': 'abc',
'role' : 'client',
}
response = self.client.post('/auth/user-login/', data, format='json')
json_response = json.loads(response.content)
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + json_response['access_token'])
response = self.client.get(self.BASE_URL,secure=True)
users = CustomUser.objects.all()
serializer = CustomUserDetailsSerializer(users, many=True)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
from rest_framework import routers
from django.urls import path
from .views import GetProfilesViewSet
router = routers.SimpleRouter()
urlpatterns = [
path('profiles/', GetProfilesViewSet.as_view(), name='get_profiles'),
]
urlpatterns += router.urls
from rest_framework import generics
from django.db.models import Q
from authentication.models import CustomUser
from authentication.serializers import CustomUserDetailsSerializer
class GetProfilesViewSet(generics.ListAPIView):
serializer_class = CustomUserDetailsSerializer
def get_queryset(self):
"""
Optionally restricts the returned purchases to a given user,
by filtering against a `username` query parameter in the URL.
"""
groups = self.request.user.groups.values_list('name',flat = True) # QuerySet Object
groups_list = list(groups) # QuerySet to `list`
queryset = CustomUser.objects.all()
if len(groups_list)==1:
if groups_list[0] == 'nutritionist':
nutritionist_id = self.request.user.nutritionist.id
queryset = queryset.filter(
Q(nutritionist=nutritionist_id) & ~Q(id=self.request.user.id))
return queryset
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