From 295d9fbe855963afb6fa9fd85272fde758f64cd0 Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Thu, 29 Dec 2022 19:24:08 -0300 Subject: [PATCH 1/5] add recent activities --- bothub/api/v2/internal/connect_rest_client.py | 7 ++++++ bothub/api/v2/repository/serializers.py | 13 +++++++++- bothub/api/v2/repository/views.py | 25 ++++++++++++++++++- bothub/common/tasks.py | 6 +++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/bothub/api/v2/internal/connect_rest_client.py b/bothub/api/v2/internal/connect_rest_client.py index df2409d09..cdb2d4dbf 100644 --- a/bothub/api/v2/internal/connect_rest_client.py +++ b/bothub/api/v2/internal/connect_rest_client.py @@ -79,3 +79,10 @@ def create_classifier(self, **kwargs): params={**kwargs, "classifier_type": "bothub"}, ) return request.json() + + def create_recent_activity(self, recent_activity_data): + requests.post( + url=f"{self.base_url}/v1/recent-activity", + headers=self.headers, + json=recent_activity_data + ) diff --git a/bothub/api/v2/repository/serializers.py b/bothub/api/v2/repository/serializers.py index 30b6b6155..12dda54a8 100644 --- a/bothub/api/v2/repository/serializers.py +++ b/bothub/api/v2/repository/serializers.py @@ -1015,7 +1015,18 @@ def create(self, validated_data): repository.versions.create( is_default=True, created_by=self.context["request"].user ) - + celery_app.send_task( + "send_recent_activity", + [ + { + "user": owner.user.email, + "entity": "AI", + "action": "CREATE", + "entity_name": repository.name, + "intelligence_id": repository.owner.organization.id + } + ] + ) return repository def get_intents(self, obj): diff --git a/bothub/api/v2/repository/views.py b/bothub/api/v2/repository/views.py index bc9c2332d..f5b6a3fef 100644 --- a/bothub/api/v2/repository/views.py +++ b/bothub/api/v2/repository/views.py @@ -439,7 +439,18 @@ def add_repository_project(self, request, **kwargs): organization_authorization = ( organization.organization_authorizations.filter(uuid__in=task) ) - + task = celery_app.send_task( + "send_recent_activity", + [ + { + "user": request.user.email, + "entity": "AI", + "action": "INTEGRATE", + "entity_name": repository.name, + "project_uuid": project_uuid + } + ] + ) if organization_authorization.exists(): raise ValidationError(_("Repository already added")) @@ -534,6 +545,18 @@ def train(self, request, **kwargs): raise APIException( # pragma: no cover {"status_code": request.status_code}, code=request.status_code ) + celery_app.send_task( + "send_recent_activity", + [ + { + "user": request.user.email, + "entity": "AI", + "action": "TRAIN", + "entity_name": repository.name, + "intelligence_id": repository.owner.organization.id + } + ] + ) return Response(request.json()) # pragma: no cover @action( diff --git a/bothub/common/tasks.py b/bothub/common/tasks.py index 50135c545..0e60b9e05 100644 --- a/bothub/common/tasks.py +++ b/bothub/common/tasks.py @@ -677,3 +677,9 @@ def create_repository_project(**kwargs): grpc_client = ConnectClient() grpc_client.create_classifier(**kwargs) return kwargs + + +@app.task(name="send_recent_activity") +def send_recent_activity(recent_activity_data): + connect_client = ConnectClient() + connect_client.create_recent_activity(recent_activity_data=recent_activity_data) From 9ec1daf2c769ad45c60e9437b3692ccbc0bd1de9 Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Thu, 29 Dec 2022 20:38:59 -0300 Subject: [PATCH 2/5] check if owner user existis and save the user before call nlp_train --- bothub/api/v2/repository/serializers.py | 26 +++++++++++++------------ bothub/api/v2/repository/views.py | 3 ++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/bothub/api/v2/repository/serializers.py b/bothub/api/v2/repository/serializers.py index 12dda54a8..552137263 100644 --- a/bothub/api/v2/repository/serializers.py +++ b/bothub/api/v2/repository/serializers.py @@ -1015,18 +1015,20 @@ def create(self, validated_data): repository.versions.create( is_default=True, created_by=self.context["request"].user ) - celery_app.send_task( - "send_recent_activity", - [ - { - "user": owner.user.email, - "entity": "AI", - "action": "CREATE", - "entity_name": repository.name, - "intelligence_id": repository.owner.organization.id - } - ] - ) + + if owner.user: + celery_app.send_task( + "send_recent_activity", + [ + { + "user": owner.user.email, + "entity": "AI", + "action": "CREATE", + "entity_name": repository.name, + "intelligence_id": repository.owner.organization.id + } + ] + ) return repository def get_intents(self, obj): diff --git a/bothub/api/v2/repository/views.py b/bothub/api/v2/repository/views.py index f5b6a3fef..5c05b0aa2 100644 --- a/bothub/api/v2/repository/views.py +++ b/bothub/api/v2/repository/views.py @@ -536,6 +536,7 @@ def train(self, request, **kwargs): user_authorization = repository.get_user_authorization(request.user) serializer = TrainSerializer(data=request.data) # pragma: no cover serializer.is_valid(raise_exception=True) # pragma: no cover + user = request.user if not user_authorization.can_write: raise PermissionDenied() request = repository.request_nlp_train( @@ -549,7 +550,7 @@ def train(self, request, **kwargs): "send_recent_activity", [ { - "user": request.user.email, + "user": user.email, "entity": "AI", "action": "TRAIN", "entity_name": repository.name, From 8398fa701a6d03721eacd91f423945fab73dbf76 Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Mon, 2 Jan 2023 15:16:00 -0300 Subject: [PATCH 3/5] changed the get user when create a new AI --- bothub/api/v2/repository/serializers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bothub/api/v2/repository/serializers.py b/bothub/api/v2/repository/serializers.py index 552137263..23d6061ff 100644 --- a/bothub/api/v2/repository/serializers.py +++ b/bothub/api/v2/repository/serializers.py @@ -1016,12 +1016,12 @@ def create(self, validated_data): is_default=True, created_by=self.context["request"].user ) - if owner.user: + if "request" in self.context and "user" in self.context["request"]: celery_app.send_task( "send_recent_activity", [ { - "user": owner.user.email, + "user": self.context["request"].user, "entity": "AI", "action": "CREATE", "entity_name": repository.name, From df2f93a40d13ca010d74e46fd6ae33a130dcc5f8 Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Mon, 2 Jan 2023 15:27:37 -0300 Subject: [PATCH 4/5] remove check context has request --- bothub/api/v2/repository/serializers.py | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/bothub/api/v2/repository/serializers.py b/bothub/api/v2/repository/serializers.py index 23d6061ff..16e95376a 100644 --- a/bothub/api/v2/repository/serializers.py +++ b/bothub/api/v2/repository/serializers.py @@ -1016,19 +1016,19 @@ def create(self, validated_data): is_default=True, created_by=self.context["request"].user ) - if "request" in self.context and "user" in self.context["request"]: - celery_app.send_task( - "send_recent_activity", - [ - { - "user": self.context["request"].user, - "entity": "AI", - "action": "CREATE", - "entity_name": repository.name, - "intelligence_id": repository.owner.organization.id - } - ] - ) + celery_app.send_task( + "send_recent_activity", + [ + { + "user": self.context["request"].user, + "entity": "AI", + "action": "CREATE", + "entity_name": repository.name, + "intelligence_id": repository.owner.organization.id + } + ] + ) + return repository def get_intents(self, obj): From 8bfb6cef88a691ef9fce2aea148675968d010a7d Mon Sep 17 00:00:00 2001 From: Jackson Barbosa Date: Mon, 2 Jan 2023 15:46:07 -0300 Subject: [PATCH 5/5] adding email field on parameter --- bothub/api/v2/repository/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bothub/api/v2/repository/serializers.py b/bothub/api/v2/repository/serializers.py index 16e95376a..90d11ac20 100644 --- a/bothub/api/v2/repository/serializers.py +++ b/bothub/api/v2/repository/serializers.py @@ -1020,7 +1020,7 @@ def create(self, validated_data): "send_recent_activity", [ { - "user": self.context["request"].user, + "user": self.context["request"].user.email, "entity": "AI", "action": "CREATE", "entity_name": repository.name,