Skip to content

Commit

Permalink
[f] Add dynamic placeholder for markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
huynguyengl99 committed May 22, 2024
1 parent 41add27 commit faac435
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
8 changes: 4 additions & 4 deletions headless_cms/auto_translate/openai_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@

system_prompt = f"""
You are a professional translator. Please translate and paraphrase (if needed) this content into {{lang}} language \
with friendly tone, concise and easy to understand. Just translate the content only, keep the HTML or markdown \
tag as it is, and keep the proper nouns as it is, too. You just response me with translated content only, do not \
with friendly tone, concise and easy to understand. Just translate the content only, keep the HTML, markdown \
tag, the content between <getattr><getattr/> as it is, and keep the proper nouns as it is, too. You just response me with translated content only, do not \
add any additional comment or explanation.
Additionally, keep these term as it is: {str(headless_cms_settings.AUTO_TRANSLATE_IGNORES)}.
Here is your content:
"""

system_batch_translate_prompt = f"""
You are a professional translator. Please translate and paraphrase (if needed) this json object into {{lang}} language \
with friendly tone, concise and easy to understand. Just translate the content only, keep the HTML or markdown \
tag as it is, and keep the proper nouns as it is, too. You just response me with translated json object only, do not \
with friendly tone, concise and easy to understand. Just translate the content only, keep the HTML, markdown \
tag, the content between <getattr><getattr/> as it is, and keep the proper nouns as it is, too. You just response me with translated json object only, do not \
add any additional comment or explanation.
Additionally, keep these term as it is: {str(headless_cms_settings.AUTO_TRANSLATE_IGNORES)}.
Here is your json object:
Expand Down
11 changes: 11 additions & 0 deletions headless_cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ class LocalizedDynamicFileModel(LocalizedPublicationModel):
class Meta:
abstract = True

@property
def src_link(self):
src_file = self.src_file.translate()
if src_file:
src_url = src_file.url
if src_url.startswith("/"):
src_url = f"{headless_cms_settings.CMS_HOST}{src_url}"
return src_url
elif self.src_url:
return self.src_url.translate()


class SortableGenericBaseModel(LocalizedPublicationModel):
content_type = models.ForeignKey(
Expand Down
7 changes: 7 additions & 0 deletions headless_cms/serializer_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
CharField,
)

from headless_cms.utils.markdown import replace_placeholder

LANGUAGE_PREFIXES = tuple(f"/{lang}/" for lang in settings.LANGUAGES)


Expand All @@ -19,3 +21,8 @@ def to_representation(self, value):
language_code = translation.get_language() or settings.LANGUAGE_CODE

return f"/{language_code}" + value


class LocalizedMartorField(CharField):
def to_representation(self, value):
return replace_placeholder(str(value), False)
8 changes: 6 additions & 2 deletions headless_cms/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
)
from rest_framework.serializers import ModelSerializer

from headless_cms import serializer_fields
from headless_cms.fields.martor_field import LocalizedMartorField
from headless_cms.fields.url_field import AutoLanguageUrlField
from headless_cms.models import LocalizedDynamicFileModel, LocalizedPublicationModel
from headless_cms.serializer_fields import UrlField
from headless_cms.settings import headless_cms_settings


Expand All @@ -33,7 +34,10 @@ class LocalizedModelSerializer(ModelSerializer):
serializer_field_mapping[fields.LocalizedFloatField] = FloatField
serializer_field_mapping[fields.LocalizedBooleanField] = BooleanField
serializer_field_mapping[fields.LocalizedBooleanField] = BooleanField
serializer_field_mapping[AutoLanguageUrlField] = UrlField
serializer_field_mapping[AutoLanguageUrlField] = serializer_fields.UrlField
serializer_field_mapping[LocalizedMartorField] = (
serializer_fields.LocalizedMartorField
)

def to_representation(self, instance):
data = instance.published_data
Expand Down
1 change: 1 addition & 0 deletions headless_cms/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"OPENAI_CHAT_MODEL": "",
"OPENAI_CLIENT": "openai.OpenAI",
"DEFAULT_CMS_PERMISSION_CLASS": "rest_framework.permissions.AllowAny",
"CMS_HOST": "http://localhost:8000",
}

IMPORT_STRINGS = [
Expand Down
31 changes: 31 additions & 0 deletions headless_cms/utils/markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import re
from ast import literal_eval

from django.apps import apps
from django.utils.translation import gettext_lazy as _
from martor.utils import markdownify


def replace_placeholder(markdown_text, show_invalid=True):
res = markdown_text
pattern = re.compile(r"<getattr>(.*?)<getattr/>", re.DOTALL)
groups = set(pattern.findall(markdown_text))
for match in groups:
try:
app_model, id, attr = literal_eval(match)
app_str, model_str = app_model.split(".")

app = apps.get_app_config(app_str)
model = app.get_model(model_str)
obj = model.objects.get(pk=id)
dt = getattr(obj, attr)
except Exception:
dt = str(_("Invalid placeholder")) if show_invalid else None
res = res.replace(f"<getattr>{match}<getattr/>", dt) if dt else res

return res


def custom_markdownify(markdown_text):
markdown_text = replace_placeholder(markdown_text)
return markdownify(markdown_text)

0 comments on commit faac435

Please sign in to comment.