Skip to content

Commit 5eca3de

Browse files
committed
♻️(backend) stylistic and consistency changes
Refactored converter services based on PR #1609 review comments: - Renamed parameter to `data` across all convert methods for consistency - Replaced recursive call with explicit sequential calls for readability - Hardcoded CONVERSION_API_SECURE=True in Production class for security - Removed unused YdocConverter import from viewsets.py - Updated tests to match new error message wording Signed-off-by: Stephan Meijer <me@stephanmeijer.com>
1 parent f5d1036 commit 5eca3de

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

src/backend/core/api/viewsets.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@
5454
from core.services.converter_services import (
5555
ValidationError as YProviderValidationError,
5656
)
57-
from core.services.converter_services import (
58-
YdocConverter,
59-
)
6057
from core.services.search_indexers import (
6158
get_document_indexer,
6259
get_visited_document_ids_of,

src/backend/core/services/converter_services.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ServiceUnavailableError(ConversionError):
2525
class ConverterProtocol(typing.Protocol):
2626
"""Protocol for converter classes."""
2727

28-
def convert(self, text, content_type, accept):
28+
def convert(self, data, content_type, accept):
2929
"""Convert content from one format to another."""
3030

3131

@@ -43,10 +43,11 @@ def convert(self, data, content_type, accept):
4343
"""Convert input into other formats using external microservices."""
4444

4545
if content_type == mime_types.DOCX and accept == mime_types.YJS:
46-
return self.convert(
47-
self.docspec.convert(data, mime_types.DOCX, mime_types.BLOCKNOTE),
48-
mime_types.BLOCKNOTE,
49-
mime_types.YJS,
46+
blocknote_data = self.docspec.convert(
47+
data, mime_types.DOCX, mime_types.BLOCKNOTE
48+
)
49+
return self.ydoc.convert(
50+
blocknote_data, mime_types.BLOCKNOTE, mime_types.YJS
5051
)
5152

5253
return self.ydoc.convert(data, content_type, accept)
@@ -111,16 +112,16 @@ def _request(self, url, data, content_type, accept):
111112
response.raise_for_status()
112113
return response
113114

114-
def convert(self, text, content_type=mime_types.MARKDOWN, accept=mime_types.YJS):
115+
def convert(self, data, content_type=mime_types.MARKDOWN, accept=mime_types.YJS):
115116
"""Convert a Markdown text into our internal format using an external microservice."""
116117

117-
if not text:
118-
raise ValidationError("Input text cannot be empty")
118+
if not data:
119+
raise ValidationError("Input data cannot be empty")
119120

120121
try:
121122
response = self._request(
122123
f"{settings.Y_PROVIDER_API_BASE_URL}{settings.CONVERSION_API_ENDPOINT}/",
123-
text,
124+
data,
124125
content_type,
125126
accept,
126127
)

src/backend/core/tests/test_services_converter_services.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ def test_auth_header(settings):
2222

2323

2424
def test_convert_empty_text():
25-
"""Should raise ValidationError when text is empty."""
25+
"""Should raise ValidationError when data is empty."""
2626
converter = YdocConverter()
27-
with pytest.raises(ValidationError, match="Input text cannot be empty"):
27+
with pytest.raises(ValidationError, match="Input data cannot be empty"):
2828
converter.convert("")
2929

3030

@@ -143,5 +143,5 @@ def test_convert_none_input():
143143
"""Should raise ValidationError when input is None."""
144144
converter = YdocConverter()
145145

146-
with pytest.raises(ValidationError, match="Input text cannot be empty"):
146+
with pytest.raises(ValidationError, match="Input data cannot be empty"):
147147
converter.convert(None)

src/backend/impress/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,9 @@ class Production(Base):
10571057
# Privacy
10581058
SECURE_REFERRER_POLICY = "same-origin"
10591059

1060+
# Conversion API: Always verify SSL in production
1061+
CONVERSION_API_SECURE = True
1062+
10601063
CACHES = {
10611064
"default": {
10621065
"BACKEND": "django_redis.cache.RedisCache",

0 commit comments

Comments
 (0)