def handle_save(self, sender, instance, **kwargs): app_label = instance._meta.app_label model_name = instance._meta.model_name model = instance._meta.concrete_model if model in registry._models or model in registry._related_models: transaction.on_commit(lambda: celery_app.send_task( "es_handle_save", args=[instance.pk, app_label, model_name]))
def auto_translation(self, request, **kwargs): repository_version = self.get_object() user_authorization = repository_version.repository.get_user_authorization( request.user ) if not user_authorization.can_translate: raise PermissionDenied() serializer = RepositoryAutoTranslationSerializer( data=request.data ) # pragma: no cover serializer.is_valid(raise_exception=True) # pragma: no cover target_language = serializer.data.get("target_language") # Validates if the language is available languages.validate_language(value=target_language) if target_language not in languages.GOOGLE_API_TRANSLATION_LANGUAGES_SUPPORTED: raise APIException( # pragma: no cover detail=_("This language is not available in machine translation") ) if repository_version.repository.language == target_language: raise APIException( # pragma: no cover detail=_( "It is not possible to translate the base language into your own language" ) ) queue_running = ( repository_version.get_version_language(language=target_language) .queues.filter( Q(status=RepositoryQueueTask.STATUS_PENDING) | Q(status=RepositoryQueueTask.STATUS_PROCESSING) ) .filter( Q(type_processing=RepositoryQueueTask.TYPE_PROCESSING_AUTO_TRANSLATE) ) ) if queue_running: raise APIException( # pragma: no cover detail=_( "It is only possible to perform an automatic translation per language, a translation is already running" ) ) task = celery_app.send_task( "auto_translation", args=[ repository_version.pk, repository_version.repository.language, target_language, ], ) return Response({"id_queue": task.task_id})
def list_project_organizatiton(self, request, **kwargs): project_uuid = request.query_params.get("project_uuid") if not project_uuid: raise ValidationError( _("Need to pass 'project_uuid' in query params")) task = celery_app.send_task(name="get_project_organization", args=[project_uuid]) task.wait() repositories = Repository.objects.filter( authorizations__uuid__in=task.result) serialized_data = ShortRepositorySerializer(repositories, many=True) return Response(serialized_data.data)
def word_suggestions(self, request, **kwargs): """ Get four suggestions for words on a example on same language """ self.permission_classes = [permissions.IsAuthenticatedOrReadOnly] example = self.get_object() authorization = example.repository_version_language.repository_version.repository.get_user_authorization( request.user) if not authorization.can_read: raise PermissionDenied() task = celery_app.send_task(name="word_suggestions", args=[example.pk, str(authorization)]) task.wait() suggestions = task.result return Response({"suggestions": suggestions})
def add_repository_project(self, request, **kwargs): repository = self.get_object().repository organization_pk = request.data.get("organization") try: organization = Organization.objects.get(pk=organization_pk) except Organization.DoesNotExist: raise ValidationError(_("Organization not found")) organization_authorization = organization.get_organization_authorization( request.user) if not organization_authorization.can_contribute: raise PermissionDenied() serializer_data = dict( user=request.user.email, access_token=str( repository.get_user_authorization( organization_authorization.organization).uuid), **request.data, ) serializer = AddRepositoryProjectSerializer(data=serializer_data) serializer.is_valid(raise_exception=True) project_uuid = serializer.validated_data.get("project_uuid") task = celery_app.send_task(name="get_project_organization", args=[project_uuid]) task.wait() organization_authorization = organization.organization_authorizations.filter( uuid__in=task.result) if organization_authorization.exists(): raise ValidationError(_("Repository already added")) data = serializer.save() return Response(data)
def create(self, validated_data): # pragma: no cover id_clone = validated_data.pop("id") repository = validated_data.get("repository") name = validated_data.get("name") clone = get_object_or_404(RepositoryVersion, pk=id_clone, repository=repository) instance = self.Meta.model( name=name, last_update=clone.last_update, is_default=False, repository=clone.repository, created_by=self.context["request"].user, is_deleted=True, ) instance.save() answer_task = celery_app.send_task( "clone_version", args=[instance.pk, id_clone, repository.pk]) answer_task.wait() return instance
def intent_suggestions(self, request, **kwargs): """ Get 10 suggestions for intent on your self language """ self.filter_class = None intent = self.get_object() language = self.request.query_params.get("language") authorization = intent.repository_version.repository.get_user_authorization( request.user) if not authorization.can_read: raise PermissionDenied() task = celery_app.send_task( name="intent_suggestions", args=[intent.pk, language, str(authorization.pk)]) task.wait() suggestions = task.result return Response({"suggestions": suggestions})
def projectrepository(self, request, **kwargs): repository = self.get_object().repository project_uuid = request.query_params.get("project_uuid") organization_pk = request.query_params.get("organization") try: organization = (Organization.objects.get( pk=organization_pk) if organization_pk else None) except Organization.DoesNotExist: raise ValidationError(_("Organization not found")) if not project_uuid: raise ValidationError( _("Need to pass 'project_uuid' in query params")) authorization = organization.get_organization_authorization( request.user) if not authorization.can_contribute: raise PermissionDenied() task = celery_app.send_task(name="get_project_organization", args=[project_uuid]) task.wait() repositories = repository.authorizations.filter(uuid__in=task.result) data = dict(in_project=repositories.exists()) if organization: organization_authorization = ( organization.organization_authorizations.filter( uuid__in=task.result)) data["in_project"] = (data["in_project"] or organization_authorization.exists()) return Response(data)
def remove_repository_project(self, request, **kwargs): repository_version = self.get_object() repository = repository_version.repository project_uuid = request.data.get("project_uuid") organization_pk = request.data.get("organization") if not project_uuid: raise ValidationError( _("Need to pass 'project_uuid' in query params")) try: organization = (Organization.objects.get( pk=organization_pk) if organization_pk else None) except Organization.DoesNotExist: raise ValidationError(_("Organization not found")) user_authorization = organization.get_organization_authorization( request.user) if not user_authorization.is_admin: raise PermissionDenied() project_organization = celery_app.send_task( name="get_project_organization", args=[project_uuid]) project_organization.wait() authorizations = list( repository.authorizations.filter( uuid__in=project_organization.result).values_list("uuid", flat=True)) if organization: organization_authorization = organization.get_organization_authorization( request.user) if not organization_authorization.is_admin: raise PermissionDenied() authorizations += list( organization.organization_authorizations.filter( uuid__in=project_organization.result).values_list( "uuid", flat=True)) if not len(authorizations): raise ValidationError( _("Repository or organization is not be included on project")) authorizations_uuids = map(lambda authorization: str(authorization), authorizations) task = celery_app.send_task( name="remove_authorizations_project", args=[ project_uuid, list(authorizations_uuids), request.user.email ], ) task.wait() return Response(status=status.HTTP_204_NO_CONTENT)