From a852e6758ca8feb94ceff0d7bc0cd7453637d140 Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Mon, 2 Oct 2023 15:25:43 -0300 Subject: [PATCH 1/8] revert changing code --- bothub/api/v2/zeroshot/permissions.py | 15 --------------- bothub/api/v2/zeroshot/views.py | 9 +++++---- 2 files changed, 5 insertions(+), 19 deletions(-) delete mode 100644 bothub/api/v2/zeroshot/permissions.py diff --git a/bothub/api/v2/zeroshot/permissions.py b/bothub/api/v2/zeroshot/permissions.py deleted file mode 100644 index 7d90b312..00000000 --- a/bothub/api/v2/zeroshot/permissions.py +++ /dev/null @@ -1,15 +0,0 @@ -from rest_framework import permissions - -from django.conf import settings - -class ZeroshotTokenPermission(permissions.BasePermission): - def has_permission(self, request, view): - token = request.META.get('HTTP_AUTHORIZATION') - - if token: - return token == f'Bearer {settings.FLOWS_TOKEN_ZEROSHOT}' - else: - return False - - def has_object_permission(self, request, view, obj): - return self.has_object_permission(request, view, obj) diff --git a/bothub/api/v2/zeroshot/views.py b/bothub/api/v2/zeroshot/views.py index 7be5b03e..28141c3d 100644 --- a/bothub/api/v2/zeroshot/views.py +++ b/bothub/api/v2/zeroshot/views.py @@ -14,8 +14,6 @@ Repository, ) -from bothub.api.v2.zeroshot.permissions import ZeroshotTokenPermission - logger = logging.getLogger(__name__) @@ -76,11 +74,14 @@ def get(self, request): class ZeroShotFastPredictAPIView(APIView): - permission_classes = [ZeroshotTokenPermission] - def post(self, request): data = request.data + auth = data.get("token") + + if auth != settings.FLOWS_TOKEN_ZEROSHOT: + return Response(status=401, data={"error": "You don't have permission to do this."}) + classes = {} for categorie in data.get("categories"): From 0039c4dd98275fe56d4057a6b629d6def43e0def Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Thu, 5 Oct 2023 16:03:48 -0300 Subject: [PATCH 2/8] adding token auth on header request --- bothub/api/v2/zeroshot/permissions.py | 15 +++++++++++++++ bothub/api/v2/zeroshot/views.py | 4 ++++ 2 files changed, 19 insertions(+) create mode 100644 bothub/api/v2/zeroshot/permissions.py diff --git a/bothub/api/v2/zeroshot/permissions.py b/bothub/api/v2/zeroshot/permissions.py new file mode 100644 index 00000000..7d90b312 --- /dev/null +++ b/bothub/api/v2/zeroshot/permissions.py @@ -0,0 +1,15 @@ +from rest_framework import permissions + +from django.conf import settings + +class ZeroshotTokenPermission(permissions.BasePermission): + def has_permission(self, request, view): + token = request.META.get('HTTP_AUTHORIZATION') + + if token: + return token == f'Bearer {settings.FLOWS_TOKEN_ZEROSHOT}' + else: + return False + + def has_object_permission(self, request, view, obj): + return self.has_object_permission(request, view, obj) diff --git a/bothub/api/v2/zeroshot/views.py b/bothub/api/v2/zeroshot/views.py index 28141c3d..ab9fd20a 100644 --- a/bothub/api/v2/zeroshot/views.py +++ b/bothub/api/v2/zeroshot/views.py @@ -14,6 +14,7 @@ Repository, ) +from bothub.api.v2.zeroshot.permissions import ZeroshotTokenPermission logger = logging.getLogger(__name__) @@ -74,6 +75,9 @@ def get(self, request): class ZeroShotFastPredictAPIView(APIView): + authentication_classes = [] + permission_classes = [ZeroshotTokenPermission] + def post(self, request): data = request.data From 9c07b4f8f874253e095d7786c27bbba00f438aa2 Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Thu, 5 Oct 2023 16:05:18 -0300 Subject: [PATCH 3/8] removing old token verification --- bothub/api/v2/zeroshot/views.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/bothub/api/v2/zeroshot/views.py b/bothub/api/v2/zeroshot/views.py index ab9fd20a..02d2ab18 100644 --- a/bothub/api/v2/zeroshot/views.py +++ b/bothub/api/v2/zeroshot/views.py @@ -79,12 +79,8 @@ class ZeroShotFastPredictAPIView(APIView): permission_classes = [ZeroshotTokenPermission] def post(self, request): - data = request.data - - auth = data.get("token") - if auth != settings.FLOWS_TOKEN_ZEROSHOT: - return Response(status=401, data={"error": "You don't have permission to do this."}) + data = request.data classes = {} From e28659d6f55a17157bf8f4db50b11fdf5972c613 Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Thu, 5 Oct 2023 16:11:53 -0300 Subject: [PATCH 4/8] return adjustments --- bothub/api/v2/zeroshot/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bothub/api/v2/zeroshot/views.py b/bothub/api/v2/zeroshot/views.py index 02d2ab18..2f72d645 100644 --- a/bothub/api/v2/zeroshot/views.py +++ b/bothub/api/v2/zeroshot/views.py @@ -101,7 +101,7 @@ def post(self, request): "Content-Type": "application/json; charset: utf-8", "Authorization": f"Bearer {settings.ZEROSHOT_TOKEN}", } - + response_nlp = None try: url = settings.ZEROSHOT_BASE_NLP_URL if len(settings.ZEROSHOT_SUFFIX) > 0: @@ -111,6 +111,7 @@ def post(self, request): url=url, json=body ) + return Response(status=response_nlp.status_code, data=response_nlp.json() if response_nlp.status_code == 200 else {"error": response_nlp.text}) except Exception as error: logger.error(f"[ - ] Zeroshot fast predict: {error}") - return Response(status=response_nlp.status_code, data=response_nlp.json() if response_nlp.status_code == 200 else {"error": response_nlp.text}) + return Response(status=response_nlp.status_code if response_nlp else 500, data={"error": error}) From 68158605b029257bd6772e5e70f15a978dac5c5c Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Thu, 5 Oct 2023 16:16:13 -0300 Subject: [PATCH 5/8] removing unused function --- bothub/api/v2/zeroshot/permissions.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/bothub/api/v2/zeroshot/permissions.py b/bothub/api/v2/zeroshot/permissions.py index 7d90b312..5440874b 100644 --- a/bothub/api/v2/zeroshot/permissions.py +++ b/bothub/api/v2/zeroshot/permissions.py @@ -10,6 +10,3 @@ def has_permission(self, request, view): return token == f'Bearer {settings.FLOWS_TOKEN_ZEROSHOT}' else: return False - - def has_object_permission(self, request, view, obj): - return self.has_object_permission(request, view, obj) From e8f6ca426b7ce11fb279417f7a4f10481acea171 Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Fri, 6 Oct 2023 09:43:41 -0300 Subject: [PATCH 6/8] udpate from 6.3.0 -> 6.4.0 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 374678ba..0847fb89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 6.4.0 +## Add + - New format to validate flows token on zeroshot + # 6.3.0 ## Add - New format with singleton to create rabbitmq connection From 50703eaff8e91df60d91869caa5126d4edbb1bed Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Fri, 6 Oct 2023 10:16:47 -0300 Subject: [PATCH 7/8] fix manifest path on build workflow --- .github/workflows/build-bothub-engine-push-tag-dockerhub.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-bothub-engine-push-tag-dockerhub.yaml b/.github/workflows/build-bothub-engine-push-tag-dockerhub.yaml index b5533da0..532cc0bb 100644 --- a/.github/workflows/build-bothub-engine-push-tag-dockerhub.yaml +++ b/.github/workflows/build-bothub-engine-push-tag-dockerhub.yaml @@ -33,7 +33,7 @@ jobs: echo "COMMIT_SHA=$GITHUB_SHA" | tee -a "${GITHUB_ENV}" echo "IMAGE_TAG=bothubit/bothub-engine:${TAG}" | tee -a "${GITHUB_ENV}" echo "IMAGE_SOURCE_URL=https://github.com/Ilhasoft/bothub-engine" | tee -a "${GITHUB_ENV}" - echo "MANIFESTS_REPOSITORY=Ilhasoft/kubernetes-manifests-artificial-intelligence" | tee -a "${GITHUB_ENV}" + echo "MANIFESTS_REPOSITORY=weni-ai/kubernetes-manifests-artificial-intelligence" | tee -a "${GITHUB_ENV}" echo "MANIFESTS_APPLICATION=engine" | tee -a "${GITHUB_ENV}" echo "MANIFESTS_PATCH_TARGET=deployment-engine.json" | tee -a "${GITHUB_ENV}" From 9c8c52787527ca24682d770f6ac83866acf0ef2c Mon Sep 17 00:00:00 2001 From: Alisson Date: Fri, 13 Oct 2023 16:38:12 -0300 Subject: [PATCH 8/8] add prefetch_related to filter --- bothub/common/models.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bothub/common/models.py b/bothub/common/models.py index c31f77a1..cf3dd7cb 100644 --- a/bothub/common/models.py +++ b/bothub/common/models.py @@ -8,7 +8,7 @@ from django.core.mail import send_mail from django.core.validators import RegexValidator, _lazy_re_compile from django.db import models -from django.db.models import Sum, Q, IntegerField, Case, When, Count +from django.db.models import Sum, Q, IntegerField, Case, When, Count, Prefetch from django.dispatch import receiver from django.shortcuts import get_object_or_404 from django.template.loader import render_to_string @@ -79,7 +79,10 @@ def supported_language(self, language): versions__repositoryversionlanguage__in=valid_updates, versions__repositoryversionlanguage__added__translations__language=language, ) - ) + ).prefetch_related( + 'versions', + Prefetch('versions', queryset=RepositoryVersion.objects.all(), to_attr='repositoryversionlanguage'), + Prefetch('versions', queryset=RepositoryVersion.objects.all(), to_attr='repositoryversionlanguage__added__translations')) def count_logs(self, start_date=None, end_date=None, user=None, *args, **kwargs): return self.annotate(