misc: apply some typographic rules on ckeditor fields (#16107)

This follows https://fr.wikipedia.org/wiki/Ponctuation.
This commit is contained in:
Frédéric Péters 2017-05-02 17:36:46 +02:00
parent b6894ae792
commit 14497be115
8 changed files with 108 additions and 5 deletions

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals
from django.db import models, migrations
import combo.data.fields
class Migration(migrations.Migration):
@ -37,4 +38,19 @@ class Migration(migrations.Migration):
},
bases=(models.Model,),
),
migrations.AlterField(
model_name='activeitems',
name='text',
field=combo.data.fields.RichTextField(null=True, verbose_name='Text', blank=True),
),
migrations.AlterField(
model_name='itemshistory',
name='text',
field=combo.data.fields.RichTextField(null=True, verbose_name='Text', blank=True),
),
migrations.AlterField(
model_name='selfdeclaredinvoicepayment',
name='text',
field=combo.data.fields.RichTextField(null=True, verbose_name='Text', blank=True),
),
]

View File

@ -36,8 +36,7 @@ from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.utils.http import urlencode
from ckeditor.fields import RichTextField
from combo.data.fields import RichTextField
from combo.data.models import CellBase
from combo.data.library import register_cell_class
from combo.utils import NothingInCacheException, aes_hex_encrypt, requests

View File

@ -4,6 +4,7 @@ from __future__ import unicode_literals
from django.db import migrations, models
import datetime
from django.utils.timezone import utc
import combo.data.fields
class Migration(migrations.Migration):
@ -19,4 +20,9 @@ class Migration(migrations.Migration):
field=models.DateTimeField(default=datetime.datetime.now(utc), auto_now=True),
preserve_default=False,
),
migrations.AlterField(
model_name='momoiconcell',
name='description',
field=combo.data.fields.RichTextField(null=True, verbose_name='Description', blank=True),
),
]

View File

@ -20,8 +20,7 @@ from django.forms import models as model_forms
from django.forms import Select
from django.utils.translation import ugettext_lazy as _
from ckeditor.fields import RichTextField
from combo.data.fields import RichTextField
from combo.data.models import CellBase
from combo.data.library import register_cell_class

50
combo/data/fields.py Normal file
View File

@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
#
# combo - content management system
# Copyright (C) 2015-2017 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import forms
from django.conf import settings
import ckeditor.fields
class RichTextField(ckeditor.fields.RichTextField):
def formfield(self, **kwargs):
defaults = {
'form_class': RichTextFormField,
'config_name': self.config_name,
'extra_plugins' : self.extra_plugins,
'external_plugin_resources': self.external_plugin_resources
}
defaults.update(kwargs)
return super(RichTextField, self).formfield(**defaults)
class RichTextFormField(ckeditor.fields.RichTextFormField):
def clean(self, value):
value = super(RichTextFormField, self).clean(value)
if settings.LANGUAGE_CODE.startswith('fr-'):
# apply some typographic rules
value = value.replace(u'&laquo; ', u'«\u202f')
value = value.replace(u'« ', u'«\u202f')
value = value.replace(u' &raquo;', u'\u202f»')
value = value.replace(u' »', u'\u202f»')
value = value.replace(u' :', u'\u00a0:')
value = value.replace(u' ;', u'\u202f;')
value = value.replace(u' !', u'\u202f!')
value = value.replace(u' ?', u'\u202f?')
return value

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals
from django.db import migrations, models
import combo.data.fields
class Migration(migrations.Migration):
@ -16,4 +17,9 @@ class Migration(migrations.Migration):
name='varnames_str',
field=models.CharField(help_text='Comma separated list of query-string variables to be copied in template context', max_length=200, verbose_name='Variable names', blank=True),
),
migrations.AlterField(
model_name='textcell',
name='text',
field=combo.data.fields.RichTextField(null=True, verbose_name='Text', blank=True),
),
]

View File

@ -42,7 +42,7 @@ from django.forms.widgets import MediaDefiningClass
from django.template import Context, RequestContext, Template
from django.test.client import RequestFactory
from ckeditor.fields import RichTextField
from .fields import RichTextField
import cmsplugin_blurp.utils
from jsonfield import JSONField

View File

@ -425,6 +425,33 @@ def test_edit_cell_order(app, admin_user):
assert TextCell.objects.get(id=cell.id).order == new_order[i]
def test_edit_text_cell(app, admin_user):
Page.objects.all().delete()
page = Page(title='One', slug='one', template_name='standard')
page.save()
app = login(app)
resp = app.get('/manage/pages/%s/' % page.id)
data_add_url = [x for x in resp.html.find_all('option') if x.text == 'Text'][0].get('data-add-url')
resp = app.get(data_add_url)
assert resp.location == 'http://testserver/manage/pages/%s/' % page.id
cells = CellBase.get_cells(page_id=page.id)
assert len(cells) == 1
assert isinstance(cells[0], TextCell)
resp = app.get('/manage/pages/%s/' % page.id)
resp.form['cdata_textcell-%s-text' % cells[0].id].value = 'Hello : World'
resp = resp.form.submit()
assert TextCell.objects.get(id=cells[0].id).text == 'Hello : World'
with override_settings(LANGUAGE_CODE='fr-fr'):
resp = app.get('/manage/pages/%s/' % page.id)
resp.form['cdata_textcell-%s-text' % cells[0].id].value = 'Hello : World'
resp = resp.form.submit()
assert TextCell.objects.get(id=cells[0].id).text == u'Hello\u00a0: World'
def test_edit_config_json_cell(app, admin_user):
Page.objects.all().delete()
page = Page(title='One', slug='one', template_name='standard')