From d62b8ab2529c5faca6826e27010ef9422e2b8a01 Mon Sep 17 00:00:00 2001 From: shagi Date: Thu, 26 Oct 2023 15:38:44 +0200 Subject: [PATCH] chore: lintin --- gas/gas/core/views.py | 6 ++-- gas/gas/users/urls.py | 5 +++- gas/sites.py | 14 ++++----- gas/utils.py | 20 ++++++------- gas/views.py | 66 +++++++++++++++++++++---------------------- 5 files changed, 56 insertions(+), 55 deletions(-) diff --git a/gas/gas/core/views.py b/gas/gas/core/views.py index 4adcfbe..979cf7a 100644 --- a/gas/gas/core/views.py +++ b/gas/gas/core/views.py @@ -19,14 +19,14 @@ class GASLoginView(LoginView): def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) css = gas_settings.MEDIA['css'] - js = gas_settings.MEDIA['js'] + javascript = gas_settings.MEDIA['js'] if gas_settings.EXTRA_MEDIA: css = css + gas_settings.EXTRA_MEDIA.get('css', []) - js = js + gas_settings.EXTRA_MEDIA.get('js', []) + javascript = javascript + gas_settings.EXTRA_MEDIA.get('js', []) ctx.update({ 'logo_static_url': gas_settings.LOGO, 'css': css, - 'js': js, + 'js': javascript, }) return ctx diff --git a/gas/gas/users/urls.py b/gas/gas/users/urls.py index 5282d1c..66bddfb 100644 --- a/gas/gas/users/urls.py +++ b/gas/gas/users/urls.py @@ -7,6 +7,9 @@ urlpatterns = [ path('', views.UserList.as_view(), name="user_list"), path('create/', views.CreateUser.as_view(), name="user_create"), path('/edit/', views.UpdateUser.as_view(), name="user_update"), - path('/change-password/', views.ChangePasswordUser.as_view(), name="user_change_password"), + path( + '/change-password/', + views.ChangePasswordUser.as_view(), + name="user_change_password"), path('/delete/', views.DeleteUser.as_view(), name="user_delete"), ] diff --git a/gas/sites.py b/gas/sites.py index 3f6043e..2ffb9a7 100644 --- a/gas/sites.py +++ b/gas/sites.py @@ -10,7 +10,7 @@ class Entry: self.icon = icon self.url = url self.roles = roles - self.children = dict() + self.children = {} self.parent = parent self.order = order if order is not None else float("inf") @@ -20,7 +20,7 @@ class Entry: return super().__lt__(other) -class GASSite(object): +class GASSite: base_role = 'admins' def __init__(self): @@ -42,12 +42,12 @@ class GASSite(object): exposed when gas is not active. """ if prefix in self._registry['urls']: - raise ImproperlyConfigured("Prefix {0} already in use".format(prefix)) + raise ImproperlyConfigured(f"Prefix {prefix} already in use") self._registry['urls'][prefix] = urls def register_menu(self, name, label, url=None, icon=None, roles=None, parent=None, order=None): if name in self._registry['menu']: - raise ImproperlyConfigured("Menu entry '{0}' already registered.".format(name)) + raise ImproperlyConfigured(f"Menu entry '{name}' already registered.") if roles is None: roles = set() @@ -61,8 +61,8 @@ class GASSite(object): if parent: try: parent_entry = self._registry['menu'][parent] - except KeyError: - raise ImproperlyConfigured("Parent {} not registered.".format(parent)) + except KeyError as exc: + raise ImproperlyConfigured("Parent {parent} not registered.") from exc parent_entry.children[name] = entry self._registry['menu'][name] = entry @@ -86,7 +86,7 @@ class GASSite(object): for prefix, urls in self._registry['urls'].items(): if prefix: urlpatterns.append( - re_path(r'^{0}/'.format(prefix), include(urls)), + re_path(r'^{prefix}/', include(urls)), ) else: urlpatterns.append( diff --git a/gas/utils.py b/gas/utils.py index 96292fc..28c05bb 100644 --- a/gas/utils.py +++ b/gas/utils.py @@ -6,13 +6,13 @@ from django.utils.functional import Promise class JSONEncoder(json.JSONEncoder): - def default(self, obj): - if isinstance(obj, datetime.datetime): - return obj.strftime('%Y-%m-%d %H:%M') - if isinstance(obj, datetime.date): - return obj.strftime('%Y-%m-%d') - if isinstance(obj, QuerySet): - return list(obj) - if isinstance(obj, Promise): - return str(obj) - return json.JSONEncoder.default(self, obj) + def default(self, o): + if isinstance(o, datetime.datetime): + return o.strftime('%Y-%m-%d %H:%M') + if isinstance(o, datetime.date): + return o.strftime('%Y-%m-%d') + if isinstance(o, QuerySet): + return list(o) + if isinstance(o, Promise): + return str(o) + return json.JSONEncoder.default(self, o) diff --git a/gas/views.py b/gas/views.py index b30c9cc..37b98a3 100644 --- a/gas/views.py +++ b/gas/views.py @@ -1,10 +1,8 @@ -import json - from django.contrib import messages from django.contrib.admin.utils import NestedObjects from django.core.exceptions import ImproperlyConfigured from django.db import router -from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest +from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest, JsonResponse from django.urls import reverse from django.utils.cache import add_never_cache_headers from django.utils.html import escape, escapejs @@ -45,30 +43,26 @@ class AjaxCommandsMixin: """ def post(self, request, *args, **kwargs): if 'command' in self.request.POST: - command_processor = getattr(self, 'do_{0}'.format(self.request.POST['command']), None) + command = self.request.POST['command'] + command_processor = getattr(self, f'do_{command}', None) if command_processor is not None: return command_processor() - else: - return HttpResponseBadRequest() - else: - handler = getattr(super(), 'post', self.http_method_not_allowed) - return handler(request, *args, **kwargs) + return HttpResponseBadRequest() + handler = getattr(super(), 'post', self.http_method_not_allowed) + return handler(request, *args, **kwargs) def get(self, request, *args, **kwargs): if 'command' in self.request.GET: - command_processor = getattr(self, 'send_{0}'.format(self.request.GET['command']), None) + command = self.request.POST['command'] + command_processor = getattr(self, f'send_{command}', None) if command_processor is not None: return command_processor() - else: - return HttpResponseBadRequest() - else: - handler = getattr(super(), 'get', self.http_method_not_allowed) - return handler(request, *args, **kwargs) + return HttpResponseBadRequest() + handler = getattr(super(), 'get', self.http_method_not_allowed) + return handler(request, *args, **kwargs) def render_json(self, data, encoder=utils.JSONEncoder): - return HttpResponse( - json.dumps(data, indent=2, cls=encoder), - content_type='application/json') + return JsonResponse(data, json_dumps_params={"indent": 2}, encoder=encoder) class GASMixin: @@ -123,16 +117,14 @@ class GASMixin: # Forcing possible reverse_lazy evaluation url = str(self.cancel_url) return url - else: - return self.get_success_url() + return self.get_success_url() def get_continue_url(self): if self.continue_url: # Forcing possible reverse_lazy evaluation url = str(self.continue_url) return url - else: - raise ImproperlyConfigured("No URL to redirect to. Provide a continue_url.") + raise ImproperlyConfigured("No URL to redirect to. Provide a continue_url.") def get_header_title(self): " Contents for the tag. " @@ -156,10 +148,10 @@ class GASMixin: def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) css = gas_settings.MEDIA['css'] - js = gas_settings.MEDIA['js'] + javascript = gas_settings.MEDIA['js'] if gas_settings.EXTRA_MEDIA: css = css + gas_settings.EXTRA_MEDIA.get('css', []) - js = js + gas_settings.EXTRA_MEDIA.get('js', []) + javascript = javascript + gas_settings.EXTRA_MEDIA.get('js', []) ctx.update({ 'base_template': self.base_template, 'home_url': self.get_home_url(), @@ -171,7 +163,7 @@ class GASMixin: 'gas_title': gas_settings.TITLE, 'logo_static_url': gas_settings.LOGO, 'css': css, - 'js': js, + 'js': javascript, }) return ctx @@ -190,8 +182,7 @@ class GASListView(GASMixin, ListView): self.filter_form = self.get_filter_form() if self.filter_form is not None and self.filter_form.is_valid(): return self.filter_form.filter(qs) - else: - return qs + return qs def get_queryset(self): qs = super().get_queryset() @@ -220,13 +211,16 @@ class GASCreateView(GASMixin, CreateView): def form_valid(self, form): response = super().form_valid(form) if '_popup' in self.request.POST: + # escape() calls force_text. + obj_pk = escape(self.object.pk) + obj = escapejs(self.object) return HttpResponse( '<!DOCTYPE html><html><head><title>' - '' % - # escape() calls force_text. - (escape(self.object.pk), escapejs(self.object))) - else: - return response + '' + ) + return response class GASUpdateView(GASMixin, UpdateView): @@ -259,10 +253,14 @@ class GASDeleteView(GASMixin, DeleteView): def format_callback(obj): opts = obj._meta - return '%s: %s' % (capfirst(opts.verbose_name), obj) + name = capfirst(opts.verbose_name) + return f'{name}: {obj}' collector.collect([self.object]) - model_count = {model._meta.verbose_name_plural: len(objs) for model, objs in collector.model_objs.items()} + model_count = { + model._meta.verbose_name_plural: len(objs) + for model, objs in collector.model_objs.items() + } return collector.nested(format_callback), model_count def get_context_data(self, **kwargs):