From 002f4e70b2217b6e7bd2fb80109115db89746ff0 Mon Sep 17 00:00:00 2001 From: Victor Salles Date: Fri, 12 Aug 2022 13:28:10 -0300 Subject: [PATCH 1/4] Create repository/authorization-by-user route Creates a RepositoryTokenByUserViewSet which retrieves the user access token based on a new environment variable named TEST_REPOSITORY_ID. --- README.md | 7 ++++--- bothub/api/v2/repository/views.py | 28 ++++++++++++++++++++++++++++ bothub/api/v2/routers.py | 6 ++++++ bothub/settings.py | 2 ++ docker-compose.yml | 3 +++ 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9dc314fd..85c8eef7 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ยท

- +

Bothub @@ -146,7 +146,7 @@ You can set environment variables in your OS, write on ```.env``` file or pass v | APM_SERVICE_ENVIRONMENT | ```string``` | ```''``` | Environment that APM is running on | ENVIRONMENT | ```string``` | ```production``` | Specify the environment you are going to run, it is also used for sentry | SUGGESTION_LANGUAGES | ```string``` | ```en|pt_br``` | Specify the the languages supported by environment for word and intent suggestions -| N_WORDS_TO_GENERATE | ```int``` | ```4``` | Specify the number of suggestions that will be returned for word suggestions +| N_WORDS_TO_GENERATE | ```int``` | ```4``` | Specify the number of suggestions that will be returned for word suggestions | N_SENTENCES_TO_GENERATE | ```int``` | ```10``` | Specify the number of suggestions that will be returned for intent suggestions | REDIS_TIMEOUT | ```int``` | ```3600``` | Specify a systemwide Redis keys life time | SECRET_KEY_CHECK_LEGACY_USER | ```string``` | ```None``` | Enables and specifies the token to use for the legacy user endpoint. @@ -184,6 +184,8 @@ You can set environment variables in your OS, write on ```.env``` file or pass v | GUNICORN_WORKERS | ``` int ``` | ``` multiprocessing.cpu_count() * 2 + 1 ``` | Gunicorn number of workers. | USE_ELASTICSEARCH | ```boolean``` | ```true``` | Change the logic in requirements_to_train to use either elasticsearch or postgres. | REPOSITORY_BLOCK_USER_LOGS | ```list``` | ```[]``` | List of repository authorization(api bearer) that won't save logs +| RUN_AS_DEVELOPMENT_MODE | ```boolean``` | ```false``` | Specifies how to run the server, in production or development mode. +| TEST_REPOSITORY_ID | ```string``` | ```None``` | The repository from which the RepositoryTokenByUserViewSet will retrieve the logged user's access token. ## Roadmap @@ -205,4 +207,3 @@ Contributions are what make the open source community such an amazing place to b 3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) 4. Push to the Branch (`git push origin feature/AmazingFeature`) 5. Open a Pull Request - diff --git a/bothub/api/v2/repository/views.py b/bothub/api/v2/repository/views.py index d988c7f1..386b8ad8 100644 --- a/bothub/api/v2/repository/views.py +++ b/bothub/api/v2/repository/views.py @@ -1068,6 +1068,34 @@ def destroy(self, request, *args, **kwargs): return super().destroy(request, *args, **kwargs) +class RepositoryTokenByUserViewSet(mixins.ListModelMixin, GenericViewSet): + def get_queryset(self): + user = self.request.user + if user.is_anonymous: + return RepositoryAuthorization.objects.none() + return RepositoryAuthorization.objects.filter(user=self.request.user) + + def list(self, request, **kwargs): + """ + Get repository access token based on logged user + """ + + repository_id = settings.TEST_REPOSITORY_ID + if not repository_id: + return Response( + {"TEST_REPOSITORY_ID": "Not set"}, status=status.HTTP_400_BAD_REQUEST + ) + try: + repository = Repository.objects.get(pk=repository_id) + except Repository.DoesNotExist: + return Response( + {"Repository": "Does Not Exist"}, status=status.HTTP_404_NOT_FOUND + ) + + authorization = repository.get_user_authorization(request.user) + return Response({"access_token": str(authorization.uuid)}) + + class RepositoryExampleViewSet( mixins.CreateModelMixin, mixins.RetrieveModelMixin, diff --git a/bothub/api/v2/routers.py b/bothub/api/v2/routers.py index fbf67a88..73b8db9a 100644 --- a/bothub/api/v2/routers.py +++ b/bothub/api/v2/routers.py @@ -39,6 +39,7 @@ from .repository.views import RepositoriesViewSet from .repository.views import RepositoryAuthorizationRequestsViewSet from .repository.views import RepositoryAuthorizationViewSet +from .repository.views import RepositoryTokenByUserViewSet from .repository.views import RepositoryCategoriesView from .repository.views import RepositoryExampleViewSet from .repository.views import RepositoryMigrateViewSet @@ -180,6 +181,11 @@ def get_lookup_regex(self, viewset, lookup_prefix=""): router.register( "repository/authorization-requests", RepositoryAuthorizationRequestsViewSet ) +router.register( + "repository/authorization-by-user", + RepositoryTokenByUserViewSet, + basename="authorization-by-user", +) router.register("repository/example", RepositoryExampleViewSet) router.register("repository/example-bulk", RepositoryExamplesBulkViewSet) router.register("repository/intent", RepositoryIntentViewSet) diff --git a/bothub/settings.py b/bothub/settings.py index a4ee2ac6..615f542a 100644 --- a/bothub/settings.py +++ b/bothub/settings.py @@ -626,3 +626,5 @@ ] REPOSITORY_BLOCK_USER_LOGS = env.list("REPOSITORY_BLOCK_USER_LOGS", default=[]) + +TEST_REPOSITORY_ID = env("TEST_REPOSITORY_ID", None) diff --git a/docker-compose.yml b/docker-compose.yml index 306cb714..56756893 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -72,6 +72,8 @@ services: - ELASTICSEARCH_REPOSITORYNLPLOG_INDEX=${ELASTICSEARCH_REPOSITORYNLPLOG_INDEX:-ai_repositorynlplog} - ELASTICSEARCH_REPOSITORYNLPLOG_INDEX=${ELASTICSEARCH_REPOSITORYQANLPLOG_INDEX:-ai_repositoryqanlplog} - ELASTICSEARCH_SIGNAL_PROCESSOR=${ELASTICSEARCH_SIGNAL_PROCESSOR:-celery} + - RUN_AS_DEVELOPMENT_MODE=${RUN_AS_DEVELOPMENT_MODE:-false} + - TEST_REPOSITORY_ID=${TEST_REPOSITORY_ID} celery: build: @@ -118,6 +120,7 @@ services: - ELASTICSEARCH_REPOSITORYBASICEXAMPLE_INDEX=${ELASTICSEARCH_REPOSITORYBASICEXAMPLE_INDEX:-ai_repositorybasicexample} - ELASTICSEARCH_SIGNAL_PROCESSOR=${ELASTICSEARCH_SIGNAL_PROCESSOR:-celery} - USE_ELASTICSEARCH=${USE_ELASTICSEARCH:-true} + - TEST_REPOSITORY_ID=${TEST_REPOSITORY_ID} bothub-engine-celery-redis: image: redis From 5b1ee930f7aa39fa2e45645c3a9ef565a9ba65f6 Mon Sep 17 00:00:00 2001 From: Victor Salles Date: Fri, 12 Aug 2022 13:42:49 -0300 Subject: [PATCH 2/4] Fix TEST_REPOSITORY_ID default --- bothub/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bothub/settings.py b/bothub/settings.py index 615f542a..4acaaeea 100644 --- a/bothub/settings.py +++ b/bothub/settings.py @@ -627,4 +627,4 @@ REPOSITORY_BLOCK_USER_LOGS = env.list("REPOSITORY_BLOCK_USER_LOGS", default=[]) -TEST_REPOSITORY_ID = env("TEST_REPOSITORY_ID", None) +TEST_REPOSITORY_ID = env("TEST_REPOSITORY_ID", default=None) From ca608426968f8a31531f8ccb817c3749c72e778c Mon Sep 17 00:00:00 2001 From: Victor Salles Date: Fri, 12 Aug 2022 16:40:21 -0300 Subject: [PATCH 3/4] Fix/ Add serializer_class to RepositoryTokenByUser --- bothub/api/v2/repository/views.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bothub/api/v2/repository/views.py b/bothub/api/v2/repository/views.py index 386b8ad8..74a6903e 100644 --- a/bothub/api/v2/repository/views.py +++ b/bothub/api/v2/repository/views.py @@ -1069,6 +1069,8 @@ def destroy(self, request, *args, **kwargs): class RepositoryTokenByUserViewSet(mixins.ListModelMixin, GenericViewSet): + serializer_class = RepositoryAuthorizationSerializer + def get_queryset(self): user = self.request.user if user.is_anonymous: From 25014e41e02bfd8ebefafbf52e8d036d56d37428 Mon Sep 17 00:00:00 2001 From: Victor Salles Date: Fri, 26 Aug 2022 11:40:46 -0300 Subject: [PATCH 4/4] Fix/ remove env variable from docker-compose --- README.md | 1 - docker-compose.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/README.md b/README.md index 85c8eef7..572c179a 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,6 @@ You can set environment variables in your OS, write on ```.env``` file or pass v | GUNICORN_WORKERS | ``` int ``` | ``` multiprocessing.cpu_count() * 2 + 1 ``` | Gunicorn number of workers. | USE_ELASTICSEARCH | ```boolean``` | ```true``` | Change the logic in requirements_to_train to use either elasticsearch or postgres. | REPOSITORY_BLOCK_USER_LOGS | ```list``` | ```[]``` | List of repository authorization(api bearer) that won't save logs -| RUN_AS_DEVELOPMENT_MODE | ```boolean``` | ```false``` | Specifies how to run the server, in production or development mode. | TEST_REPOSITORY_ID | ```string``` | ```None``` | The repository from which the RepositoryTokenByUserViewSet will retrieve the logged user's access token. diff --git a/docker-compose.yml b/docker-compose.yml index 56756893..7496fbf1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -72,7 +72,6 @@ services: - ELASTICSEARCH_REPOSITORYNLPLOG_INDEX=${ELASTICSEARCH_REPOSITORYNLPLOG_INDEX:-ai_repositorynlplog} - ELASTICSEARCH_REPOSITORYNLPLOG_INDEX=${ELASTICSEARCH_REPOSITORYQANLPLOG_INDEX:-ai_repositoryqanlplog} - ELASTICSEARCH_SIGNAL_PROCESSOR=${ELASTICSEARCH_SIGNAL_PROCESSOR:-celery} - - RUN_AS_DEVELOPMENT_MODE=${RUN_AS_DEVELOPMENT_MODE:-false} - TEST_REPOSITORY_ID=${TEST_REPOSITORY_ID} celery: