Skip to content

Commit

Permalink
Reformat with black
Browse files Browse the repository at this point in the history
  • Loading branch information
jaap3 committed Mar 7, 2024
1 parent 95ead14 commit a775267
Show file tree
Hide file tree
Showing 17 changed files with 196 additions and 179 deletions.
2 changes: 1 addition & 1 deletion djrichtextfield/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, *args, field_settings=None, **kwargs):
super(RichTextField, self).__init__(*args, **kwargs)

def formfield(self, **kwargs):
kwargs['widget'] = RichTextWidget(
kwargs["widget"] = RichTextWidget(
field_settings=self.field_settings, sanitizer=self.sanitizer
)
return super(RichTextField, self).formfield(**kwargs)
Expand Down
6 changes: 3 additions & 3 deletions djrichtextfield/sanitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class SanitizerMixin:
Get the field sanitizer from the provided kwargs during init, or from the settings.
"""

SANITIZER_KEY = 'sanitizer'
SANITIZER_PROFILES_KEY = 'sanitizer_profiles'
SANITIZER_KEY = "sanitizer"
SANITIZER_PROFILES_KEY = "sanitizer_profiles"

def __init__(self, *args, sanitizer=None, **kwargs):
self.sanitizer = sanitizer
Expand All @@ -34,7 +34,7 @@ def get_sanitizer(self):

if not sanitizer:
default_sanitizer = settings.CONFIG.get(self.SANITIZER_KEY)
field_settings = getattr(self, 'field_settings', None)
field_settings = getattr(self, "field_settings", None)
if isinstance(field_settings, str):
profiles = settings.CONFIG.get(self.SANITIZER_PROFILES_KEY, {})
sanitizer = profiles.get(field_settings, default_sanitizer)
Expand Down
12 changes: 6 additions & 6 deletions djrichtextfield/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
from django.test.signals import setting_changed

DEFAULT_CONFIG = {
'js': [],
'css': {},
'init_template': None,
'settings': {},
"js": [],
"css": {},
"init_template": None,
"settings": {},
}
CONFIG = DEFAULT_CONFIG.copy()
CONFIG.update(getattr(settings, 'DJRICHTEXTFIELD_CONFIG', {}))
CONFIG.update(getattr(settings, "DJRICHTEXTFIELD_CONFIG", {}))


@receiver(setting_changed)
def update_settings(setting=None, value=None, **kwargs):
global CONFIG
if setting == 'DJRICHTEXTFIELD_CONFIG': # pragma: no branch
if setting == "DJRICHTEXTFIELD_CONFIG": # pragma: no branch
CONFIG = DEFAULT_CONFIG.copy()
if value: # pragma: no branch
CONFIG.update(value)
2 changes: 1 addition & 1 deletion djrichtextfield/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
from djrichtextfield.views import InitView

urlpatterns = [
path('init.js', InitView.as_view(), name='djrichtextfield_init'),
path("init.js", InitView.as_view(), name="djrichtextfield_init"),
]
10 changes: 5 additions & 5 deletions djrichtextfield/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@


class InitView(TemplateView):
template_name = 'djrichtextfield/init.js'
content_type = 'application/javascript'
template_name = "djrichtextfield/init.js"
content_type = "application/javascript"

def get_settings_json(self):
return mark_safe(json.dumps(settings.CONFIG['settings'], default=force_str))
return mark_safe(json.dumps(settings.CONFIG["settings"], default=force_str))

def get_context_data(self, **kwargs):
context_data = super(InitView, self).get_context_data(**kwargs)
context_data['default_settings'] = self.get_settings_json()
context_data['init_template'] = settings.CONFIG['init_template']
context_data["default_settings"] = self.get_settings_json()
context_data["init_template"] = settings.CONFIG["init_template"]
return context_data
32 changes: 18 additions & 14 deletions djrichtextfield/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,32 @@


class RichTextWidget(SanitizerMixin, Textarea):
CSS_CLASS = 'djrichtextfield'
INIT_URL = 'djrichtextfield_init'
SETTINGS_ATTR = 'data-field-settings'
CONTAINER_CLASS = 'fieldBox'
PROFILE_KEY = 'profiles'
CSS_CLASS = "djrichtextfield"
INIT_URL = "djrichtextfield_init"
SETTINGS_ATTR = "data-field-settings"
CONTAINER_CLASS = "fieldBox"
PROFILE_KEY = "profiles"

def __init__(self, attrs=None, field_settings=None, sanitizer=None):
defaults = {'class': self.CSS_CLASS}
defaults = {"class": self.CSS_CLASS}
if attrs:
if 'class' in attrs:
attrs['class'] = ' '.join([
attrs['class'],
defaults['class'],
])
if "class" in attrs:
attrs["class"] = " ".join(
[
attrs["class"],
defaults["class"],
]
)
defaults.update(attrs)
self.field_settings = field_settings or {}
super(RichTextWidget, self).__init__(defaults, sanitizer=sanitizer)

@property
def media(self):
js = []
js.extend(settings.CONFIG['js'])
js.extend(settings.CONFIG["js"])
js.append(reverse(self.INIT_URL))
return Media(js=js, css=settings.CONFIG['css'])
return Media(js=js, css=settings.CONFIG["css"])

def get_field_settings(self):
"""
Expand All @@ -54,7 +56,9 @@ def render(self, name, value, attrs=None, renderer=None):
field_settings = self.get_field_settings()
if field_settings:
attrs[self.SETTINGS_ATTR] = json.dumps(field_settings, default=force_str)
textarea = super(RichTextWidget, self).render(name, value, attrs=attrs, renderer=renderer)
textarea = super(RichTextWidget, self).render(
name, value, attrs=attrs, renderer=renderer
)
return format_html('<div class="{0}">{1}</div>', self.CONTAINER_CLASS, textarea)

def value_from_datadict(self, *args, **kwargs):
Expand Down
50 changes: 25 additions & 25 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,45 @@

from setuptools import setup

version = '1.6.2.dev0'
version = "1.6.2.dev0"

if sys.argv[-1] == 'publish':
os.system('python setup.py sdist bdist_wheel upload')
if sys.argv[-1] == "publish":
os.system("python setup.py sdist bdist_wheel upload")
print("You probably want to also tag the version now:")
print(" git tag -a %s -m 'version %s'" % (version, version))
print(" git push --tags")
sys.exit()

readme = open('README.rst').read()
history = open('HISTORY.rst').read()
readme = open("README.rst").read()
history = open("HISTORY.rst").read()

setup(
name='django-richtextfield',
name="django-richtextfield",
version=version,
description='A Django model field and widget that renders a customizable WYSIWYG/rich text editor',
long_description=readme + '\n\n' + history,
author='Jaap Roes',
author_email='[email protected]',
url='https://github.com/jaap3/django-richtextfield',
description="A Django model field and widget that renders a customizable WYSIWYG/rich text editor",
long_description=readme + "\n\n" + history,
author="Jaap Roes",
author_email="[email protected]",
url="https://github.com/jaap3/django-richtextfield",
packages=[
'djrichtextfield',
"djrichtextfield",
],
include_package_data=True,
install_requires=[],
python_requires='>=3.6',
license='MIT',
python_requires=">=3.6",
license="MIT",
zip_safe=False,
keywords='django-richtextfield, djrichtextfield django wywiwyg field',
keywords="django-richtextfield, djrichtextfield django wywiwyg field",
classifiers=[
'Development Status :: 5 - Production/Stable',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
"Development Status :: 5 - Production/Stable",
"Framework :: Django",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
)
119 changes: 60 additions & 59 deletions testproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,102 @@

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

SECRET_KEY = '--1oh7*5-1_rr%*34^&@e^b59(vgv=zm1wt(wm1ga=pfm73(9i'
SECRET_KEY = "--1oh7*5-1_rr%*34^&@e^b59(vgv=zm1wt(wm1ga=pfm73(9i"

DEBUG = True
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djrichtextfield',
'testproject.testapp',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"djrichtextfield",
"testproject.testapp",
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = 'testproject.urls'
ROOT_URLCONF = "testproject.urls"

WSGI_APPLICATION = 'testproject.wsgi.application'
WSGI_APPLICATION = "testproject.wsgi.application"

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
}
]

STATIC_URL = '/static/'
STATIC_URL = "/static/"

# Django Rich Text Field
TINYMCE_CONFIG = {
'js': ['//cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js'],
'init_template': 'djrichtextfield/init/tinymce.js',
'settings': {
'menubar': False,
'plugins': 'link image table code',
'toolbar': 'formatselect | bold italic | removeformat |' ' link unlink image table | code',
'block_formats': 'Paragraph=p;Header 1=h1;Header 2=h2;Header 3=h3',
'width': 700,
"js": ["//cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js"],
"init_template": "djrichtextfield/init/tinymce.js",
"settings": {
"menubar": False,
"plugins": "link image table code",
"toolbar": "formatselect | bold italic | removeformat |"
" link unlink image table | code",
"block_formats": "Paragraph=p;Header 1=h1;Header 2=h2;Header 3=h3",
"width": 700,
},
'profiles': {
'mini': {
'toolbar': 'bold italic | removeformat',
"profiles": {
"mini": {
"toolbar": "bold italic | removeformat",
},
},
}

CKEDITOR_CONFIG = {
'js': ['//cdn.ckeditor.com/4.14.0/standard/ckeditor.js'],
'init_template': 'djrichtextfield/init/ckeditor.js',
'settings': {
'toolbar': [
{'items': ['Format', '-', 'Bold', 'Italic', '-', 'RemoveFormat']},
{'items': ['Link', 'Unlink', 'Image', 'Table']},
{'items': ['Source']},
"js": ["//cdn.ckeditor.com/4.14.0/standard/ckeditor.js"],
"init_template": "djrichtextfield/init/ckeditor.js",
"settings": {
"toolbar": [
{"items": ["Format", "-", "Bold", "Italic", "-", "RemoveFormat"]},
{"items": ["Link", "Unlink", "Image", "Table"]},
{"items": ["Source"]},
],
'format_tags': 'p;h1;h2;h3',
'width': 700,
"format_tags": "p;h1;h2;h3",
"width": 700,
},
'profiles': {
'mini': {
'toolbar': [
{'items': ['Bold', 'Italic', '-', 'RemoveFormat']},
"profiles": {
"mini": {
"toolbar": [
{"items": ["Bold", "Italic", "-", "RemoveFormat"]},
]
}
},
'sanitizer': lambda value: 'foo' + value,
'sanitizer_profiles': {
'baz': lambda value: value + 'baz',
"sanitizer": lambda value: "foo" + value,
"sanitizer_profiles": {
"baz": lambda value: value + "baz",
},
}

Expand Down
4 changes: 2 additions & 2 deletions testproject/testapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@


class CommentForm(forms.ModelForm):
content = forms.CharField(widget=RichTextWidget(field_settings='mini'))
content = forms.CharField(widget=RichTextWidget(field_settings="mini"))

class Meta:
model = Comment
exclude = ['post']
exclude = ["post"]
Loading

0 comments on commit a775267

Please sign in to comment.