Skip to content

Commit

Permalink
refactoring, fix ruff check
Browse files Browse the repository at this point in the history
  • Loading branch information
hvlads committed Jan 6, 2024
1 parent fcd4202 commit b6fb3d1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- run: pip install -e ".[dev]"
- run: black --check .
- run: codespell --ignore-words-list="ro" --skip="*.json,*.lock,./.git"
- run: ruff --format=github .
- run: ruff check .
- run: pip install --editable .
- run: mypy --non-interactive .
- run: cp -R django_ckeditor_5 example/blog
Expand Down
6 changes: 3 additions & 3 deletions django_ckeditor_5/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@


class CKEditor5Field(models.Field):
def __init__(self, *args, config_name="default", **kwargs):
def __init__(self, *args, config_name="default", **kwargs) -> None:
self.config_name = config_name
super().__init__(*args, **kwargs)

def get_internal_type(self):
def get_internal_type(self) -> str:
return "TextField"

def formfield(self, **kwargs):
Expand All @@ -17,5 +17,5 @@ def formfield(self, **kwargs):
"max_length": self.max_length,
**({"widget": CKEditor5Widget(config_name=self.config_name)}),
**kwargs,
}
},
)
2 changes: 2 additions & 0 deletions django_ckeditor_5/templates/django_ckeditor_5/widget.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<div>
<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
<span class="word-count" id="{{script_id}}-word-count"></span>
{% if errors %}
{{ errors }}
{% endif %}
</div>
<input type="hidden" id="{{script_id}}-ck-editor-5-upload-url" data-upload-url="{{upload_url}}" data-csrf_cookie_name="{{ csrf_cookie_name }}">

<span>{{ config|json_script:script_id }}</span>
11 changes: 7 additions & 4 deletions django_ckeditor_5/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ def get_storage_class():
try:
return import_string(default_storage_setting)
except ImportError:
raise ImproperlyConfigured(f"Invalid default storage class: {default_storage_setting}")
error_msg = f"Invalid default storage class: {default_storage_setting}"
raise ImproperlyConfigured(error_msg)
elif default_storage_name:
try:
return import_string(default_storage_name)
except ImportError:
raise ImproperlyConfigured(f"Invalid default storage class: {default_storage_name}")
error_msg = f"Invalid default storage class: {default_storage_name}"
raise ImproperlyConfigured(error_msg)
else:
raise ImproperlyConfigured(
"Either CKEDITOR_5_FILE_STORAGE, DEFAULT_FILE_STORAGE, or STORAGES['default'] setting is required.")
error_msg = ("Either CKEDITOR_5_FILE_STORAGE, DEFAULT_FILE_STORAGE, "
"or STORAGES['default'] setting is required.")
raise ImproperlyConfigured(error_msg)


storage = get_storage_class()
Expand Down
10 changes: 5 additions & 5 deletions django_ckeditor_5/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,28 @@ def __init__(self, config_name="default", attrs=None):

def format_error(self, ex):
return "{} {}".format(
_("Check the correct settings.CKEDITOR_5_CONFIGS "), str(ex)
_("Check the correct settings.CKEDITOR_5_CONFIGS "), str(ex),
)

class Media:
css = {
"all": [
"django_ckeditor_5/dist/styles.css",
]
],
}
custom_css = getattr(settings, "CKEDITOR_5_CUSTOM_CSS", None)
if custom_css:
css["all"].append(custom_css)
js = ["django_ckeditor_5/dist/bundle.js", ]
js = ["django_ckeditor_5/dist/bundle.js" ]
configs = getattr(settings, "CKEDITOR_5_CONFIGS", None)
if configs is not None:
for config in configs:
language = configs[config].get('language')
if language:
if isinstance(language, str) and language != "en":
js += [f"django_ckeditor_5/dist/translations/{language}.js", ]
js += [f"django_ckeditor_5/dist/translations/{language}.js" ]
elif isinstance(language, dict) and language.get('ui') and language["ui"] != "en":
js += [f"django_ckeditor_5/dist/translations/{language['ui']}.js", ]
js += [f"django_ckeditor_5/dist/translations/{language['ui']}.js" ]

def render(self, name, value, attrs=None, renderer=None):
context = super().get_context(name, value, attrs)
Expand Down
48 changes: 31 additions & 17 deletions example/blog/articles/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,51 @@


class Migration(migrations.Migration):

initial = True

dependencies = [
]
dependencies = []

operations = [
migrations.CreateModel(
name='Article',
name="Article",
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200, null=True, verbose_name='Title')),
('text', django_ckeditor_5.fields.CKEditor5Field(verbose_name='Text')),
('text2', django_ckeditor_5.fields.CKEditor5Field(blank=True, null=True, verbose_name='Text 2')),
(
"id",
models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID"),
),
("title", models.CharField(max_length=200, null=True, verbose_name="Title")),
("text", django_ckeditor_5.fields.CKEditor5Field(verbose_name="Text")),
(
"text2",
django_ckeditor_5.fields.CKEditor5Field(blank=True, null=True, verbose_name="Text 2"),
),
],
options={
'verbose_name': 'Article',
'verbose_name_plural': 'Articles',
"verbose_name": "Article",
"verbose_name_plural": "Articles",
},
),
migrations.CreateModel(
name='Comment',
name="Comment",
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('author', models.CharField(max_length=250)),
('text', django_ckeditor_5.fields.CKEditor5Field(verbose_name='Text')),
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='articles.article')),
(
"id",
models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID"),
),
("author", models.CharField(max_length=250)),
("text", django_ckeditor_5.fields.CKEditor5Field(verbose_name="Text")),
(
"article",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="comments",
to="articles.article",
),
),
],
options={
'verbose_name': 'Comment',
'verbose_name_plural': 'Comments',
"verbose_name": "Comment",
"verbose_name_plural": "Comments",
},
),
]
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ keywords = ["CKEditor", "CKEditor5", "Django"]
license = {text = "BSD-3-Clause"}
readme = "README.rst"
requires-python = ">=3.7"
version = "0.2.10"
version = "0.2.11"

authors = [
{"name" = "Vladislav Khoboko", "email" = "[email protected]"},
Expand All @@ -21,6 +21,7 @@ classifiers = [
"Framework :: Django :: 2.2",
"Framework :: Django :: 3.0",
"Framework :: Django :: 4.0",
"Framework :: Django :: 5.0",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
Expand All @@ -29,6 +30,7 @@ classifiers = [
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
]
Expand Down Expand Up @@ -146,9 +148,9 @@ select = [
"W", # pycodestyle
"YTT", # flake8-2020
# "ANN", # flake8-annotations
# "COM", # flake8-commas
"COM", # flake8-commas
# "D", # pydocstyle
# "EM", # flake8-errmsg
"EM", # flake8-errmsg
# "NPY", # NumPy-specific rules
# "PD", # pandas-vet
# "PTH", # flake8-use-pathlib
Expand Down

0 comments on commit b6fb3d1

Please sign in to comment.