From 29a9adfb14337f40310718d57ae4fee742402be3 Mon Sep 17 00:00:00 2001 From: Ashutosh619-sudo Date: Wed, 27 Dec 2023 15:09:01 +0530 Subject: [PATCH 1/4] Ready View for making sure bamboohr Creds are valid --- apps/bamboohr/urls.py | 3 ++- apps/bamboohr/views.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/bamboohr/urls.py b/apps/bamboohr/urls.py index 2226006e..35d5ad42 100644 --- a/apps/bamboohr/urls.py +++ b/apps/bamboohr/urls.py @@ -1,11 +1,12 @@ from django.urls import path from .views import PostFolder, PostPackage, BambooHrConnection, BambooHrView, BambooHrConfigurationView, \ - DisconnectView, SyncEmployeesView + DisconnectView, SyncEmployeesView, BambooHrReadyView app_name = 'bamboohr' urlpatterns = [ + path('ready/', BambooHrReadyView.as_view(), name='ready'), path('', BambooHrView.as_view(), name='bamboohr'), path('packages/', PostPackage.as_view(), name='package'), path('folder/', PostFolder.as_view(), name='folder'), diff --git a/apps/bamboohr/views.py b/apps/bamboohr/views.py index 14707a4d..3f08b099 100644 --- a/apps/bamboohr/views.py +++ b/apps/bamboohr/views.py @@ -1,4 +1,5 @@ import logging +from bamboosdk.bamboohrsdk import BambooHrSDK from rest_framework.response import Response from rest_framework.views import status @@ -12,9 +13,38 @@ from apps.bamboohr.actions import disconnect_bamboohr, sync_employees from apps.names import BAMBOO_HR +from rest_framework.views import APIView + logger = logging.getLogger(__name__) logger.level = logging.INFO +class BambooHrReadyView(generics.ListAPIView): + + def get(self, request, *args, **kwargs): + try: + # bamboohr = BambooHr.objects.get(org_id__in=[kwargs['org_id']]) + bamboohrsdk = BambooHrSDK(api_token='864acbfb14b38f974439af49ec95bc02e52a45b9', sub_domain='baba') + response = bamboohrsdk.time_off.get() + + if response['timeOffTypes']: + return Response( + data = { + 'message': 'Ready' + }, + status=status.HTTP_200_OK + ) + else: + return Response( + data = { + 'message': 'Invalid token' + }, + status=status.HTTP_400_BAD_REQUEST + ) + except BambooHr.DoesNotExist: + return Response( + data={'message': 'Bamboo HR Details Not Found'}, + status=status.HTTP_404_NOT_FOUND + ) class BambooHrView(generics.ListAPIView): serializer_class = BambooHrSerializer From a03ae50943ce5319a004095401979af1a5b7a54f Mon Sep 17 00:00:00 2001 From: Ashutosh619-sudo Date: Wed, 27 Dec 2023 15:14:10 +0530 Subject: [PATCH 2/4] comment changes --- apps/bamboohr/urls.py | 2 +- apps/bamboohr/views.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/bamboohr/urls.py b/apps/bamboohr/urls.py index 35d5ad42..9b40fc4e 100644 --- a/apps/bamboohr/urls.py +++ b/apps/bamboohr/urls.py @@ -6,7 +6,7 @@ app_name = 'bamboohr' urlpatterns = [ - path('ready/', BambooHrReadyView.as_view(), name='ready'), + path('health_check/', BambooHrReadyView.as_view(), name='health-check'), path('', BambooHrView.as_view(), name='bamboohr'), path('packages/', PostPackage.as_view(), name='package'), path('folder/', PostFolder.as_view(), name='folder'), diff --git a/apps/bamboohr/views.py b/apps/bamboohr/views.py index 3f08b099..c72569dc 100644 --- a/apps/bamboohr/views.py +++ b/apps/bamboohr/views.py @@ -22,8 +22,8 @@ class BambooHrReadyView(generics.ListAPIView): def get(self, request, *args, **kwargs): try: - # bamboohr = BambooHr.objects.get(org_id__in=[kwargs['org_id']]) - bamboohrsdk = BambooHrSDK(api_token='864acbfb14b38f974439af49ec95bc02e52a45b9', sub_domain='baba') + bamboohr = BambooHr.objects.get(org_id__in=[kwargs['org_id']]) + bamboohrsdk = BambooHrSDK(api_token=bamboohr.api_token, sub_domain=bamboohr.sub_domain) response = bamboohrsdk.time_off.get() if response['timeOffTypes']: From a6d11228d4e3fbcfdac072749fc1bb0cff0f98e0 Mon Sep 17 00:00:00 2001 From: Ashutosh619-sudo Date: Wed, 27 Dec 2023 15:32:55 +0530 Subject: [PATCH 3/4] new field to check if creds have expire --- .../0006_bamboohr_is_credentials_expired.py | 18 ++++++++++++++++++ apps/bamboohr/models.py | 1 + apps/bamboohr/views.py | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 apps/bamboohr/migrations/0006_bamboohr_is_credentials_expired.py diff --git a/apps/bamboohr/migrations/0006_bamboohr_is_credentials_expired.py b/apps/bamboohr/migrations/0006_bamboohr_is_credentials_expired.py new file mode 100644 index 00000000..feb3182b --- /dev/null +++ b/apps/bamboohr/migrations/0006_bamboohr_is_credentials_expired.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.14 on 2023-12-27 10:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('bamboohr', '0005_bamboohr_employee_exported_at'), + ] + + operations = [ + migrations.AddField( + model_name='bamboohr', + name='is_credentials_expired', + field=models.BooleanField(default=False, help_text='BambooHr Credential Status'), + ), + ] diff --git a/apps/bamboohr/models.py b/apps/bamboohr/models.py index a0b57ca5..de284aa0 100644 --- a/apps/bamboohr/models.py +++ b/apps/bamboohr/models.py @@ -18,6 +18,7 @@ class BambooHr(models.Model): created_at = models.DateTimeField(auto_now_add=True, help_text='Created at datetime') updated_at = models.DateTimeField(auto_now=True, help_text='Updated at datetime') employee_exported_at = models.DateTimeField(auto_now_add=True, help_text='Employee exported to Fyle at datetime') + is_credentials_expired = models.BooleanField(default=False, help_text='BambooHr Credential Status') class Meta: db_table = 'bamboohr' diff --git a/apps/bamboohr/views.py b/apps/bamboohr/views.py index c72569dc..a6e467c6 100644 --- a/apps/bamboohr/views.py +++ b/apps/bamboohr/views.py @@ -22,7 +22,7 @@ class BambooHrReadyView(generics.ListAPIView): def get(self, request, *args, **kwargs): try: - bamboohr = BambooHr.objects.get(org_id__in=[kwargs['org_id']]) + bamboohr = BambooHr.objects.get(org_id__in=[kwargs['org_id']], is_credentials_expire=False) bamboohrsdk = BambooHrSDK(api_token=bamboohr.api_token, sub_domain=bamboohr.sub_domain) response = bamboohrsdk.time_off.get() @@ -34,6 +34,8 @@ def get(self, request, *args, **kwargs): status=status.HTTP_200_OK ) else: + bamboohr.is_credentials_expired = True + bamboohr.save() return Response( data = { 'message': 'Invalid token' From bbeda811d5419d7ead0c4e90454c09a2b9e4f3ff Mon Sep 17 00:00:00 2001 From: Ashutosh619-sudo Date: Wed, 27 Dec 2023 15:37:06 +0530 Subject: [PATCH 4/4] minor comment changes --- apps/bamboohr/urls.py | 4 ++-- apps/bamboohr/views.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/bamboohr/urls.py b/apps/bamboohr/urls.py index 9b40fc4e..5de8f5b4 100644 --- a/apps/bamboohr/urls.py +++ b/apps/bamboohr/urls.py @@ -1,12 +1,12 @@ from django.urls import path from .views import PostFolder, PostPackage, BambooHrConnection, BambooHrView, BambooHrConfigurationView, \ - DisconnectView, SyncEmployeesView, BambooHrReadyView + DisconnectView, SyncEmployeesView, HealthCheck app_name = 'bamboohr' urlpatterns = [ - path('health_check/', BambooHrReadyView.as_view(), name='health-check'), + path('health_check/', HealthCheck.as_view(), name='health-check'), path('', BambooHrView.as_view(), name='bamboohr'), path('packages/', PostPackage.as_view(), name='package'), path('folder/', PostFolder.as_view(), name='folder'), diff --git a/apps/bamboohr/views.py b/apps/bamboohr/views.py index a6e467c6..0af911d6 100644 --- a/apps/bamboohr/views.py +++ b/apps/bamboohr/views.py @@ -18,7 +18,7 @@ logger = logging.getLogger(__name__) logger.level = logging.INFO -class BambooHrReadyView(generics.ListAPIView): +class HealthCheck(generics.ListAPIView): def get(self, request, *args, **kwargs): try: