Skip to content

Commit

Permalink
Merge branch 'fix/request_nlp_train_add_json_dumps' into develop_connect
Browse files Browse the repository at this point in the history
  • Loading branch information
mldzs committed Apr 15, 2021
2 parents 3b851ca + b15a518 commit f173ab7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 40 deletions.
4 changes: 2 additions & 2 deletions bothub/api/v2/nlp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class RepositoryAuthorizationParseViewSet(mixins.RetrieveModelMixin, GenericView
queryset = RepositoryAuthorization.objects
serializer_class = NLPSerializer
permission_classes = [AllowAny]
authentication_classes = [NLPAuthentication]
authentication_classes = []

def retrieve(self, request, *args, **kwargs):
check_auth(request)
Expand Down Expand Up @@ -573,7 +573,7 @@ class RepositoryUpdateInterpretersViewSet(
queryset = RepositoryVersionLanguage.objects
serializer_class = NLPSerializer
permission_classes = [AllowAny]
authentication_classes = [NLPAuthentication]
authentication_classes = []

def retrieve(self, request, *args, **kwargs):
check_auth(request)
Expand Down
16 changes: 3 additions & 13 deletions bothub/authentication/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class NLPAuthentication(TokenAuthentication):
model = RepositoryAuthorization

def authenticate(self, request):
from bothub.api.v2.nlp.views import RepositoryAuthorizationParseViewSet
from bothub.api.v2.nlp.views import RepositoryUpdateInterpretersViewSet

auth = get_authorization_header(request).split()

if not auth or auth[0].lower() != self.keyword.lower().encode():
Expand All @@ -64,20 +61,13 @@ def authenticate(self, request):
)
raise exceptions.AuthenticationFailed(msg)

if isinstance(
request.parser_context.get("view"), RepositoryAuthorizationParseViewSet
) or isinstance(
request.parser_context.get("view"), RepositoryUpdateInterpretersViewSet
):
return self.authenticate_credentials(token, validation=False)

return self.authenticate_credentials(token, validation=True)
return self.authenticate_credentials(token)

def authenticate_credentials(self, key, **kwargs):
def authenticate_credentials(self, key):
model = self.get_model()
try:
authorization = model.objects.get(uuid=key)
if not authorization.can_translate and kwargs.get("validation"):
if not authorization.can_translate:
raise exceptions.PermissionDenied()

return (authorization.user, authorization)
Expand Down
33 changes: 9 additions & 24 deletions bothub/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,31 +431,16 @@ def validate_if_can_run_automatic_evaluate(self, language: str) -> None:

def request_nlp_train(self, user_authorization, data):
try: # pragma: no cover
if data.get("repository_version"):
r = requests.post( # pragma: no cover
"{}train/".format(
self.nlp_server
if self.nlp_server
else settings.BOTHUB_NLP_BASE_URL
),
data={"repository_version": data.get("repository_version")},
headers={
"Authorization": "Bearer {}".format(user_authorization.uuid)
},
)
else:
r = requests.post( # pragma: no cover
"{}train/".format(
self.nlp_server
if self.nlp_server
else settings.BOTHUB_NLP_BASE_URL
),
data={},
headers={
"Authorization": "Bearer {}".format(user_authorization.uuid)
},
)
url = f"{self.nlp_server if self.nlp_server else settings.BOTHUB_NLP_BASE_URL}train/"
data = {
"repository_version": data.get("repository_version")
}
headers = {"Authorization": f"Bearer {user_authorization.uuid}"}

r = requests.post(url, data=json.dumps(data), headers=headers)

return r # pragma: no cover

except requests.exceptions.ConnectionError: # pragma: no cover
raise APIException( # pragma: no cover
{"status_code": status.HTTP_503_SERVICE_UNAVAILABLE},
Expand Down
2 changes: 1 addition & 1 deletion bothub/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
schema_view = get_schema_view(
openapi.Info(
title="API Documentation",
default_version="v2.1.15",
default_version="v2.1.18",
description="Documentation",
terms_of_service="https://bothub.it/terms",
contact=openapi.Contact(email="bothub@ilhasoft.com.br"),
Expand Down

0 comments on commit f173ab7

Please sign in to comment.