Compare commits

..

4 Commits

6 changed files with 65 additions and 60 deletions

View File

@ -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

View File

@ -7,6 +7,9 @@ urlpatterns = [
path('', views.UserList.as_view(), name="user_list"),
path('create/', views.CreateUser.as_view(), name="user_create"),
path('<int:pk>/edit/', views.UpdateUser.as_view(), name="user_update"),
path('<int:pk>/change-password/', views.ChangePasswordUser.as_view(), name="user_change_password"),
path(
'<int:pk>/change-password/',
views.ChangePasswordUser.as_view(),
name="user_change_password"),
path('<int:pk>/delete/', views.DeleteUser.as_view(), name="user_delete"),
]

View File

@ -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(

View File

@ -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)

View File

@ -1,11 +1,10 @@
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
from django.utils.text import capfirst
from django.utils.translation import gettext_lazy as _
@ -44,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)
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)
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:
@ -97,7 +92,10 @@ class GASMixin:
def dispatch(self, *args, **kwargs):
if self.check_user_forbidden():
return HttpResponseRedirect(reverse('gas:login') + '?next={}'.format(self.request.path))
path = self.request.path
response = HttpResponseRedirect(reverse('gas:login') + f'?next={path}')
add_never_cache_headers(response)
return response
return super().dispatch(*args, **kwargs)
def form_valid(self, form):
@ -119,7 +117,6 @@ class GASMixin:
# Forcing possible reverse_lazy evaluation
url = str(self.cancel_url)
return url
else:
return self.get_success_url()
def get_continue_url(self):
@ -127,7 +124,6 @@ class GASMixin:
# Forcing possible reverse_lazy evaluation
url = str(self.continue_url)
return url
else:
raise ImproperlyConfigured("No URL to redirect to. Provide a continue_url.")
def get_header_title(self):
@ -152,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(),
@ -167,7 +163,7 @@ class GASMixin:
'gas_title': gas_settings.TITLE,
'logo_static_url': gas_settings.LOGO,
'css': css,
'js': js,
'js': javascript,
})
return ctx
@ -186,7 +182,6 @@ 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
def get_queryset(self):
@ -216,12 +211,15 @@ 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></title></head><body>'
'<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script></body></html>' %
# escape() calls force_text.
(escape(self.object.pk), escapejs(self.object)))
else:
'<script type="text/javascript">'
f' opener.dismissAddAnotherPopup(window, "{obj_pk}", "{obj}");'
'</script></body></html>'
)
return response
@ -255,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):
@ -278,7 +280,7 @@ class GASDeleteView(GASMixin, DeleteView):
})
return ctx
def delete(self, request, *args, **kwargs):
response = super().delete(request, *args, **kwargs)
messages.add_message(request, messages.SUCCESS, self.get_deleted_text())
def form_valid(self, form):
response = super().form_valid(form)
messages.add_message(self.request, messages.SUCCESS, self.get_deleted_text())
return response

View File

@ -1 +1 @@
Django==3.2.13
Django>=3.2.13,<5.0