Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crowdin_api/api_resources/source_strings/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ class ListStringsOrderBy(Enum):
CREATED_AT = "createdAt"
UPDATED_AT = "updatedAt"
TYPE = "type"


class StringUpdateOption(Enum):
CLEAR_TRANSLATIONS_AND_APPROVALS = "clear_translations_and_approvals"
KEEP_TRANSLATIONS = "keep_translations"
KEEP_TRANSLATIONS_AND_APPROVALS = "keep_translations_and_approvals"
46 changes: 35 additions & 11 deletions crowdin_api/api_resources/source_strings/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

from crowdin_api.api_resources.abstract.resources import BaseResource
from crowdin_api.api_resources.enums import DenormalizePlaceholders
from crowdin_api.api_resources.source_strings.enums import ScopeFilter
from crowdin_api.api_resources.source_strings.enums import (
ScopeFilter,
StringUpdateOption,
)
from crowdin_api.api_resources.source_strings.types import (
SourceStringsPatchRequest,
StringBatchOperationPatchRequest,
)
from crowdin_api.sorting import Sorting
from crowdin_api.utils import convert_enum_to_string_if_exists


class SourceStringsResource(BaseResource):
Expand Down Expand Up @@ -145,6 +149,7 @@ def edit_string(
stringId: int,
data: Iterable[SourceStringsPatchRequest],
projectId: Optional[int] = None,
updateOption: Optional[StringUpdateOption] = None,
):
"""
Edit String.
Expand All @@ -154,17 +159,27 @@ def edit_string(
"""

projectId = projectId or self.get_project_id()
params = {}

return self.requester.request(
method="patch",
path=self.get_source_strings_path(projectId=projectId, stringId=stringId),
request_data=data,
)
if updateOption is not None:
params["updateOption"] = convert_enum_to_string_if_exists(updateOption)

request_kwargs = {
"method": "patch",
"path": self.get_source_strings_path(projectId=projectId, stringId=stringId),
"request_data": data,
}

if params:
request_kwargs["params"] = params

return self.requester.request(**request_kwargs)

def string_batch_operation(
self,
data: Iterable[StringBatchOperationPatchRequest],
projectId: Optional[int] = None,
updateOption: Optional[StringUpdateOption] = None,
):
"""
String Batch Operations.
Expand All @@ -174,9 +189,18 @@ def string_batch_operation(
"""

projectId = projectId or self.get_project_id()
params = {}

return self.requester.request(
method="patch",
path=self.get_source_strings_path(projectId=projectId),
request_data=data,
)
if updateOption is not None:
params["updateOption"] = convert_enum_to_string_if_exists(updateOption)

request_kwargs = {
"method": "patch",
"path": self.get_source_strings_path(projectId=projectId),
"request_data": data,
}

if params:
request_kwargs["params"] = params

return self.requester.request(**request_kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
SourceStringsPatchPath,
StringBatchOperationsPath,
StringBatchOperations,
StringUpdateOption,
)
from crowdin_api.api_resources.source_strings.resource import SourceStringsResource
from crowdin_api.requester import APIRequester
Expand Down Expand Up @@ -198,6 +199,41 @@ def test_edit_string(self, m_request, base_absolut_url):
path=resource.get_source_strings_path(projectId=1, stringId=2),
)

@mock.patch("crowdin_api.requester.APIRequester.request")
def test_edit_string_with_update_option(self, m_request, base_absolut_url):
m_request.return_value = "response"

data = [
{
"value": "test",
"op": PatchOperation.REPLACE,
"path": SourceStringsPatchPath.TEXT,
},
]

resource = self.get_resource(base_absolut_url)
assert (
resource.edit_string(
projectId=1,
stringId=2,
data=data,
updateOption=StringUpdateOption.KEEP_TRANSLATIONS,
)
== "response"
)

m_request.assert_called_once_with(
method="patch",
request_data=data,
params={
"updateOption": "keep_translations",
},
path=resource.get_source_strings_path(
projectId=1,
stringId=2,
),
)

@mock.patch("crowdin_api.requester.APIRequester.request")
def test_string_batch_operation(self, m_request, base_absolut_url):
m_request.return_value = "response"
Expand All @@ -221,3 +257,34 @@ def test_string_batch_operation(self, m_request, base_absolut_url):
path=resource.get_source_strings_path(1),
request_data=data,
)

@mock.patch("crowdin_api.requester.APIRequester.request")
def test_string_batch_operation_with_update_option(self, m_request, base_absolut_url):
m_request.return_value = "response"

data = [
{
"op": StringBatchOperations.REPLACE,
"path": StringBatchOperationsPath.IS_HIDDEN,
"value": True,
},
]

resource = self.get_resource(base_absolut_url)
assert (
resource.string_batch_operation(
projectId=1,
data=data,
updateOption=StringUpdateOption.KEEP_TRANSLATIONS,
)
== "response"
)

m_request.assert_called_once_with(
method="patch",
path=resource.get_source_strings_path(1),
request_data=data,
params={
"updateOption": "keep_translations",
},
)
Loading