-
Notifications
You must be signed in to change notification settings - Fork 471
Draft: ✨ Import of documents #1609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
aebe7d0
75fe73c
9af753a
eeeaa0c
970fcd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| from django.conf import settings | ||
|
|
||
| import requests | ||
| import typing | ||
|
|
||
| from core.services import mime_types | ||
|
|
||
| class ConversionError(Exception): | ||
| """Base exception for conversion-related errors.""" | ||
|
|
@@ -19,8 +21,65 @@ class ServiceUnavailableError(ConversionError): | |
| """Raised when the conversion service is unavailable.""" | ||
|
|
||
|
|
||
| class ConverterProtocol(typing.Protocol): | ||
| def convert(self, text, content_type, accept): ... | ||
|
|
||
|
|
||
| class Converter: | ||
| docspec: ConverterProtocol | ||
| ydoc: ConverterProtocol | ||
|
|
||
| def __init__(self): | ||
| self.docspec = DocSpecConverter() | ||
| self.ydoc = YdocConverter() | ||
|
|
||
| def convert(self, input, content_type, accept): | ||
| """Convert input into other formats using external microservices.""" | ||
|
|
||
| if content_type == mime_types.DOCX and accept == mime_types.YJS: | ||
| return self.convert( | ||
| self.docspec.convert(input, mime_types.DOCX, mime_types.BLOCKNOTE), | ||
| mime_types.BLOCKNOTE, | ||
| mime_types.YJS | ||
| ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ease the read and comprehension, you can maybe break down in multiple calls avoiding the recursion ? |
||
|
|
||
| return self.ydoc.convert(input, content_type, accept) | ||
|
|
||
|
|
||
| class DocSpecConverter: | ||
| """Service class for DocSpec conversion-related operations.""" | ||
|
|
||
| def _request(self, url, data, content_type): | ||
| """Make a request to the DocSpec API.""" | ||
|
|
||
| response = requests.post( | ||
| url, | ||
| headers={"Accept": mime_types.BLOCKNOTE}, | ||
| files={"file": ("document.docx", data, content_type)}, | ||
| timeout=settings.CONVERSION_API_TIMEOUT, | ||
| verify=settings.CONVERSION_API_SECURE, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see this settings defined in the |
||
| ) | ||
| response.raise_for_status() | ||
| return response | ||
|
|
||
| def convert(self, data, content_type, accept): | ||
| """Convert a Document to BlockNote.""" | ||
| if not data: | ||
| raise ValidationError("Input data cannot be empty") | ||
|
|
||
| if content_type != mime_types.DOCX or accept != mime_types.BLOCKNOTE: | ||
| raise ValidationError(f"Conversion from {content_type} to {accept} is not supported.") | ||
|
|
||
| try: | ||
| return self._request(settings.DOCSPEC_API_URL, data, content_type).content | ||
| except requests.RequestException as err: | ||
| raise ServiceUnavailableError( | ||
| "Failed to connect to DocSpec conversion service", | ||
| ) from err | ||
|
|
||
|
|
||
| class YdocConverter: | ||
| """Service class for conversion-related operations.""" | ||
| """Service class for YDoc conversion-related operations.""" | ||
|
|
||
| @property | ||
| def auth_header(self): | ||
|
|
@@ -45,7 +104,7 @@ def _request(self, url, data, content_type, accept): | |
| return response | ||
|
|
||
| def convert( | ||
| self, text, content_type="text/markdown", accept="application/vnd.yjs.doc" | ||
| self, text, content_type=mime_types.MARKDOWN, accept=mime_types.YJS | ||
| ): | ||
| """Convert a Markdown text into our internal format using an external microservice.""" | ||
|
|
||
|
|
@@ -59,14 +118,14 @@ def convert( | |
| content_type, | ||
| accept, | ||
| ) | ||
| if accept == "application/vnd.yjs.doc": | ||
| if accept == mime_types.YJS: | ||
| return b64encode(response.content).decode("utf-8") | ||
| if accept in {"text/markdown", "text/html"}: | ||
| if accept in {mime_types.MARKDOWN, "text/html"}: | ||
| return response.text | ||
| if accept == "application/json": | ||
| if accept == mime_types.JSON: | ||
| return response.json() | ||
| raise ValidationError("Unsupported format") | ||
| except requests.RequestException as err: | ||
| raise ServiceUnavailableError( | ||
| "Failed to connect to conversion service", | ||
| f"Failed to connect to YDoc conversion service {content_type}, {accept}", | ||
| ) from err | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| BLOCKNOTE = "application/vnd.blocknote+json" | ||
| YJS = "application/vnd.yjs.doc" | ||
| MARKDOWN = "text/markdown" | ||
| JSON = "application/json" | ||
| DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | ||
| HTML = "text/html" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can maybe be consistent between all the
convertmethod ? In this one the first parameter is calledinput, in the calssDocSpecConverterit is calleddataand then in theYdocConverterclass it is calledtext.datalooks like good.