From 7affea498b94e91bd0a57ea02ae5b53f66f3d32d Mon Sep 17 00:00:00 2001 From: Park Hyunwoo Date: Sat, 13 Feb 2021 00:38:47 +0900 Subject: [PATCH] Apply flake8 --- .flake8 | 5 ++- django_summernote/admin.py | 9 ++-- django_summernote/forms.py | 2 +- django_summernote/test_django_summernote.py | 2 +- django_summernote/urls.py | 6 +-- django_summernote/utils.py | 48 ++++----------------- django_summernote/views.py | 23 +++++----- django_summernote/widgets.py | 24 +++++------ 8 files changed, 43 insertions(+), 76 deletions(-) diff --git a/.flake8 b/.flake8 index c46ce315..82271cc4 100644 --- a/.flake8 +++ b/.flake8 @@ -1,3 +1,4 @@ [flake8] -# E501 line too long -ignore = E501 +# E501 Line too long +# W503 Line break occurred before a binary operator / replaced by W504 +ignore = E501, W503 diff --git a/django_summernote/admin.py b/django_summernote/admin.py index 754e1dd2..60fb4107 100644 --- a/django_summernote/admin.py +++ b/django_summernote/admin.py @@ -3,16 +3,15 @@ from django.db import models from django_summernote.forms import AttachmentAdminForm -from django_summernote.utils import get_attachment_model, using_config +from django_summernote.utils import get_attachment_model, get_config from django_summernote.widgets import SummernoteWidget, SummernoteInplaceWidget class SummernoteModelAdminMixin: summernote_fields = '__all__' - @using_config def formfield_for_dbfield(self, db_field, *args, **kwargs): - summernote_widget = SummernoteWidget if config['iframe'] else SummernoteInplaceWidget + summernote_widget = SummernoteWidget if get_config()['iframe'] else SummernoteInplaceWidget if self.summernote_fields == '__all__': if isinstance(db_field, models.TextField): @@ -38,4 +37,6 @@ class AttachmentAdmin(admin.ModelAdmin): ordering = ('-id',) form = AttachmentAdminForm -admin.site.register(get_attachment_model(), AttachmentAdmin) + +if not get_config()['disable_attachment']: + admin.site.register(get_attachment_model(), AttachmentAdmin) diff --git a/django_summernote/forms.py b/django_summernote/forms.py index f8542081..80ef8442 100644 --- a/django_summernote/forms.py +++ b/django_summernote/forms.py @@ -1,7 +1,7 @@ from django import forms from django_summernote.utils import get_attachment_model try: - from PIL import Image + from PIL import Image # noqa: F401 FIELD = forms.ImageField except ImportError: FIELD = forms.FileField diff --git a/django_summernote/test_django_summernote.py b/django_summernote/test_django_summernote.py index 3ead6861..fac7190c 100644 --- a/django_summernote/test_django_summernote.py +++ b/django_summernote/test_django_summernote.py @@ -297,7 +297,7 @@ def test_wrong_attachment(self, mock_logging): url = reverse('django_summernote-upload_attachment') try: - from PIL import Image + from PIL import Image # noqa: F401 with open(IMAGE_FILE, 'rb') as fp: response = self.client.post(url, {'files': [fp]}) self.assertEqual(response.status_code, 200) diff --git a/django_summernote/urls.py b/django_summernote/urls.py index a4391f51..33c3ab60 100644 --- a/django_summernote/urls.py +++ b/django_summernote/urls.py @@ -1,12 +1,12 @@ from django.urls import path +from django_summernote.utils import get_config from django_summernote.views import ( SummernoteEditor, SummernoteUploadAttachment ) + urlpatterns = [ path('editor//', SummernoteEditor.as_view(), - name='django_summernote-editor'), - path('upload_attachment/', SummernoteUploadAttachment.as_view(), - name='django_summernote-upload_attachment'), + name='django_summernote-editor'), ] diff --git a/django_summernote/utils.py b/django_summernote/utils.py index df91aad2..16f91af3 100644 --- a/django_summernote/utils.py +++ b/django_summernote/utils.py @@ -5,7 +5,6 @@ from django.core.exceptions import ImproperlyConfigured from django.core.files.storage import default_storage from django.utils.translation import get_language -from functools import wraps from importlib import import_module # A conversion table from language to locale @@ -113,38 +112,8 @@ } -def using_config(_func=None): - """ - This allows a function to use Summernote configuration - as a global variable, temporarily. - """ - - def decorator(func): - @wraps(func) - def inner_dec(*args, **kwargs): - g = func.__globals__ - var_name = 'config' - sentinel = object() - - oldvalue = g.get(var_name, sentinel) - g[var_name] = apps.get_app_config('django_summernote').config - - try: - res = func(*args, **kwargs) - finally: - if oldvalue is sentinel: - del g[var_name] - else: - g[var_name] = oldvalue - - return res - - return inner_dec - - if _func is None: - return decorator - else: - return decorator(_func) +def get_config(): + return apps.get_app_config('django_summernote').config def uploaded_filepath(instance, filename): @@ -168,11 +137,11 @@ def example_test_func(request): return True -@using_config def get_proper_language(): """ Return the proper language by get_language() """ + config = get_config() lang = config['summernote'].get('lang') if not lang: @@ -181,11 +150,11 @@ def get_proper_language(): return lang -@using_config def get_attachment_model(): """ Returns the Attachment model that is active in this project. """ + config = get_config() try: from .models import AbstractAttachment @@ -205,19 +174,17 @@ def get_attachment_model(): ) -@using_config def get_attachment_upload_to(): """ Return 'attachment_upload_to' from configuration """ - return config['attachment_upload_to'] + return get_config()['attachment_upload_to'] -@using_config def get_attachment_storage(): # module importer code comes from # https://github.com/django-debug-toolbar/django-debug-toolbar/ - config = apps.get_app_config('django_summernote').config + config = get_config() if config['attachment_storage_class']: storage_path = config['attachment_storage_class'] @@ -248,7 +215,8 @@ def get_attachment_storage(): else: return default_storage -@using_config + def has_codemirror_config(): + config = get_config() return 'summernote' in config and \ 'codemirror' in config['summernote'] diff --git a/django_summernote/views.py b/django_summernote/views.py index c23b25d3..76537ce8 100644 --- a/django_summernote/views.py +++ b/django_summernote/views.py @@ -1,5 +1,4 @@ import logging -from django import VERSION as django_version from django.contrib.auth.mixins import UserPassesTestMixin from django.templatetags.static import static from django.http import HttpResponse, JsonResponse @@ -10,7 +9,7 @@ from django.views.decorators.clickjacking import xframe_options_sameorigin from django_summernote.forms import UploadForm -from django_summernote.utils import get_attachment_model, using_config, \ +from django_summernote.utils import get_attachment_model, get_config, \ has_codemirror_config logger = logging.getLogger(__name__) @@ -19,9 +18,9 @@ class SummernoteEditor(TemplateView): template_name = 'django_summernote/widget_iframe_editor.html' - @using_config def __init__(self): super().__init__() + config = get_config() static_default_css = tuple(static(x) for x in config['default_css']) static_default_js = tuple(static(x) for x in config['default_js']) @@ -37,11 +36,12 @@ def __init__(self): + static_default_js \ + config['js'] + self.config = config + @method_decorator(xframe_options_sameorigin) def dispatch(self, *args, **kwargs): return super(SummernoteEditor, self).dispatch(*args, **kwargs) - @using_config def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -49,18 +49,18 @@ def get_context_data(self, **kwargs): context['id_safe'] = self.kwargs['id'].replace('-', '_') context['css'] = self.css context['js'] = self.js - context['config'] = config + context['config'] = get_config() return context class SummernoteUploadAttachment(UserPassesTestMixin, View): - @using_config def test_func(self): - return config['test_func_upload_view'](self.request) + return get_config()['test_func_upload_view'](self.request) def __init__(self): super().__init__() + self.config = get_config() @method_decorator(xframe_options_sameorigin) def dispatch(self, *args, **kwargs): @@ -72,11 +72,10 @@ def get(self, request, *args, **kwargs): 'message': _('Only POST method is allowed'), }, status=400) - @using_config def post(self, request, *args, **kwargs): authenticated = request.user.is_authenticated - if config['disable_attachment']: + if self.config['disable_attachment']: logger.error( 'User<%s:%s> tried to use disabled attachment module.', getattr(request.user, 'pk', None), @@ -87,7 +86,7 @@ def post(self, request, *args, **kwargs): 'message': _('Attachment module is disabled'), }, status=403) - if config['attachment_require_authentication'] and \ + if self.config['attachment_require_authentication'] and \ not authenticated: return JsonResponse({ 'status': 'false', @@ -135,7 +134,7 @@ def post(self, request, *args, **kwargs): attachment = klass() attachment.file = file - if file.size > config['attachment_filesize_limit']: + if file.size > self.config['attachment_filesize_limit']: return JsonResponse({ 'status': 'false', 'message': _('File size exceeds the limit allowed and cannot be saved'), @@ -147,7 +146,7 @@ def post(self, request, *args, **kwargs): # choose relative/absolute url by config attachment.url = attachment.file.url - if config['attachment_absolute_uri']: + if self.config['attachment_absolute_uri']: attachment.url = request.build_absolute_uri(attachment.url) attachments.append(attachment) diff --git a/django_summernote/widgets.py b/django_summernote/widgets.py index 1741c152..41a615d4 100644 --- a/django_summernote/widgets.py +++ b/django_summernote/widgets.py @@ -5,7 +5,7 @@ from django.forms.utils import flatatt from django.template.loader import render_to_string from django.utils.safestring import mark_safe -from django_summernote.utils import get_proper_language, using_config, \ +from django_summernote.utils import get_proper_language, get_config, \ has_codemirror_config try: @@ -17,11 +17,10 @@ class SummernoteWidgetBase(forms.Textarea): - @using_config def summernote_settings(self): lang = get_proper_language() - summernote_settings = config.get('summernote', {}).copy() + summernote_settings = get_config().get('summernote', {}).copy() summernote_settings.update({ 'lang': lang, 'url': { @@ -31,8 +30,8 @@ def summernote_settings(self): }) return summernote_settings - @using_config def value_from_datadict(self, data, files, name): + config = get_config() value = data.get(name, None) if value in config['empty']: @@ -82,25 +81,24 @@ def render(self, name, value, attrs=None, **kwargs): class SummernoteInplaceWidget(SummernoteWidgetBase): - @using_config def _media(self): + config = get_config() return forms.Media( css={ 'all': ( - (config['codemirror_css'] if has_codemirror_config() else ()) + - config['default_css'] + - config['css_for_inplace'] + (config['codemirror_css'] if has_codemirror_config() else ()) + + config['default_css'] + + config['css_for_inplace'] ) }, js=( - (config['codemirror_js'] if has_codemirror_config() else ()) + - config['default_js'] + - config['js_for_inplace'] + (config['codemirror_js'] if has_codemirror_config() else ()) + + config['default_js'] + + config['js_for_inplace'] )) media = property(_media) - @using_config def render(self, name, value, attrs=None, **kwargs): summernote_settings = self.summernote_settings() summernote_settings.update(self.attrs.get('summernote', {})) @@ -110,7 +108,7 @@ def render(self, name, value, attrs=None, **kwargs): 'id': attrs['id'], 'id_safe': attrs['id'].replace('-', '_'), 'attrs': self.final_attr(attrs), - 'config': config, + 'config': get_config(), 'settings': json.dumps(summernote_settings), 'CSRF_COOKIE_NAME': django_settings.CSRF_COOKIE_NAME, }