Skip to content

Commit

Permalink
Apply flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
lqez committed Feb 12, 2021
1 parent 2d350ac commit 7affea4
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 76 deletions.
5 changes: 3 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions django_summernote/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
2 changes: 1 addition & 1 deletion django_summernote/forms.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion django_summernote/test_django_summernote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions django_summernote/urls.py
Original file line number Diff line number Diff line change
@@ -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/<id>/', SummernoteEditor.as_view(),
name='django_summernote-editor'),
path('upload_attachment/', SummernoteUploadAttachment.as_view(),
name='django_summernote-upload_attachment'),
name='django_summernote-editor'),
]
48 changes: 8 additions & 40 deletions django_summernote/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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']
Expand Down Expand Up @@ -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']
23 changes: 11 additions & 12 deletions django_summernote/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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__)
Expand All @@ -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'])
Expand All @@ -37,30 +36,31 @@ 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)

context['id'] = self.kwargs['id']
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):
Expand All @@ -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),
Expand All @@ -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',
Expand Down Expand Up @@ -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'),
Expand All @@ -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)
Expand Down
24 changes: 11 additions & 13 deletions django_summernote/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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': {
Expand All @@ -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']:
Expand Down Expand Up @@ -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', {}))
Expand All @@ -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,
}
Expand Down

0 comments on commit 7affea4

Please sign in to comment.