Skip to content
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

Add context to translation payload when dealing with TextPlugin #452

Merged
merged 10 commits into from Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
122 changes: 107 additions & 15 deletions .gitignore
@@ -1,16 +1,108 @@
*.log
*.pot
*.pyc
.project
.pydevproject
.settings
build
env*
dist
!djangocms_text_ckeditor/static/djangocms_text_ckeditor/js/dist
djangocms_text_ckeditor.egg-info
*.DS_Store
.idea
.tox
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
/node_modules/
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
.static_storage/
.media/
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha 🙈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sanity, I'm using the default .gitignore for python porvided by github. =]


# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

# Custom
*.sqlite3
*.sqlite
5 changes: 5 additions & 0 deletions Makefile
@@ -0,0 +1,5 @@
test:
flake8 djangocms_text_ckeditor --max-line-length=120 --ignore=E731 --exclude=.*,*/migrations/*,*/static/*,*__init__*
coverage erase
coverage run setup.py test
coverage report
28 changes: 27 additions & 1 deletion djangocms_text_ckeditor/cms_plugins.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import json
from distutils.version import LooseVersion
import json
import re

import cms
from cms.models import CMSPlugin
Expand Down Expand Up @@ -30,11 +31,14 @@
from .forms import ActionTokenValidationForm, DeleteOnCancelForm, RenderPluginForm, TextForm
from .models import Text
from .utils import (
OBJ_ADMIN_WITH_CONTENT_RE_PATTERN,
plugin_tags_to_admin_html,
plugin_tags_to_id_list,
plugin_tags_to_user_html,
random_comment_exempt,
replace_plugin_tags,
plugin_to_tag,
_plugin_tags_to_html,
)
from .widgets import TextEditorWidget

Expand Down Expand Up @@ -197,6 +201,28 @@ def do_post_copy(self, instance, source_map):
new_text = replace_plugin_tags(instance.body, ids_map)
self.model.objects.filter(pk=instance.pk).update(body=new_text)

@staticmethod
def get_translation_export_content(field, plugin_data):
def _render_plugin_with_content(obj, match):
from djangocms_translations.utils import get_text_field_child_label
field = get_text_field_child_label(obj.plugin_type)
content = getattr(obj, field)
return plugin_to_tag(obj, content)

content = _plugin_tags_to_html(plugin_data[field], output_func=_render_plugin_with_content)
subplugins_within_this_content = plugin_tags_to_id_list(content)
return content, subplugins_within_this_content

@staticmethod
def set_translation_import_content(content, plugin):
data = [x.groups() for x in re.finditer(OBJ_ADMIN_WITH_CONTENT_RE_PATTERN, content)]
data = {int(pk): value for pk, value in data}

return {
subplugin_id: data[subplugin_id]
for subplugin_id in plugin_tags_to_id_list(content)
}

def get_editor_widget(self, request, plugins, plugin):
"""
Returns the Django form Widget to be used for
Expand Down
7 changes: 0 additions & 7 deletions djangocms_text_ckeditor/html.py

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion djangocms_text_ckeditor/settings.py
Expand Up @@ -30,7 +30,9 @@
TEXT_ADDITIONAL_PROTOCOLS = getattr(settings, 'TEXT_ADDITIONAL_PROTOCOLS', ())
TEXT_CKEDITOR_CONFIGURATION = getattr(settings, 'TEXT_CKEDITOR_CONFIGURATION', None)
TEXT_HTML_SANITIZE = getattr(settings, 'TEXT_HTML_SANITIZE', True)
TEXT_CKEDITOR_BASE_PATH = getattr(settings, 'TEXT_CKEDITOR_BASE_PATH', urljoin(settings.STATIC_URL, 'djangocms_text_ckeditor/ckeditor/'))
TEXT_CKEDITOR_BASE_PATH = getattr(
settings, 'TEXT_CKEDITOR_BASE_PATH', urljoin(settings.STATIC_URL, 'djangocms_text_ckeditor/ckeditor/')
)
TEXT_AUTO_HYPHENATE = getattr(settings, 'TEXT_AUTO_HYPHENATE', True)
TEXT_PLUGIN_NAME = getattr(settings, 'TEXT_PLUGIN_NAME', _("Text"))
TEXT_PLUGIN_MODULE_NAME = getattr(settings, 'TEXT_PLUGIN_MODULE_NAME', _("Generic"))
Expand Down
8 changes: 8 additions & 0 deletions djangocms_text_ckeditor/test_app/cms_plugins.py
Expand Up @@ -3,6 +3,8 @@
from django.template import engines
from djangocms_text_ckeditor.cms_plugins import TextPlugin

from djangocms_text_ckeditor.test_app.models import DummyLink


@plugin_pool.register_plugin
class PreviewDisabledPlugin(CMSPluginBase):
Expand All @@ -22,3 +24,9 @@ class SekizaiPlugin(CMSPluginBase):
@plugin_pool.register_plugin
class ExtendedTextPlugin(TextPlugin):
name = 'Extended'


@plugin_pool.register_plugin
class DummyLinkPlugin(CMSPluginBase):
render_plugin = False
model = DummyLink
13 changes: 13 additions & 0 deletions djangocms_text_ckeditor/test_app/models.py
@@ -1,8 +1,21 @@
# -*- coding: utf-8 -*-
from cms.models import CMSPlugin
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from djangocms_text_ckeditor.fields import HTMLField


class SimpleText(models.Model):
text = HTMLField(blank=True)


@python_2_unicode_compatible
class DummyLink(CMSPlugin):
label = models.TextField()

class Meta:
abstract = False

def __str__(self):
return 'dummy link object'
24 changes: 24 additions & 0 deletions djangocms_text_ckeditor/test_app/templates/test_app/base.html
@@ -0,0 +1,24 @@
{% load cms_tags static menu_tags sekizai_tags %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}This is my new project home page{% endblock title %}</title>
{% render_block "css" %}
<style type="text/css">
.nav {
padding-left: 0;
}
.nav li {
display: inline;
list-style-type: none;
padding-right: 20px;
}
</style>
</head>
<body>
{% cms_toolbar %}
<div style="width: 940px; margin:0 auto">
<ul class="nav">
{% show_menu 0 100 100 100 %}
</ul>
{% block content %}
8 changes: 8 additions & 0 deletions djangocms_text_ckeditor/test_app/templates/test_app/page.html
@@ -0,0 +1,8 @@
{% extends "base.html" %}
{% load cms_tags %}

{% block title %}{% page_attribute 'title' %}{% endblock title %}

{% block content %}
{% placeholder "content" %}
{% endblock content %}