Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyue92tree committed Jul 4, 2023
2 parents 4625ddb + 1791389 commit 6f86a4a
Show file tree
Hide file tree
Showing 34 changed files with 15,907 additions and 1,708 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.html linguist-language=python
*.js linguist-language=python
*.js linguist-language=python
*.css linguist-language=python
2 changes: 1 addition & 1 deletion adminlteui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = '2.0.0'
version = '2.1.0'
default_app_config = 'adminlteui.apps.AdminlteUIConfig'
9 changes: 8 additions & 1 deletion adminlteui/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ class ModelAdmin(admin.ModelAdmin):

class Media:
css = {
"all": ("admin/components/select2/dist/css/select2.min.css",)
"all": (
"admin/components/select2/dist/css/select2.min.css",
# for daterangefilter
"admin/components/bootstrap-daterangepicker/daterangepicker.css"
)
}
js = (
"admin/components/select2/dist/js/select2.min.js",
# for daterangefilter
"admin/components/moment/moment-with-locales.min.js",
"admin/components/bootstrap-daterangepicker/daterangepicker.js",
)

def changelist_view(self, request, extra_context=None):
Expand Down
14 changes: 14 additions & 0 deletions adminlteui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def make(self, request, models=None, deep=1, deep_limit=0):
menu_item['url'] = model.get('admin_url')
elif self.menu_type == 'link':
menu_item['url'] = self.url
# check permissions when permissions are not None
if self.permissions:
if request.user.has_perms(self.permissions) is False:
return None
else:
# menu_type: group and child is empty will hide the menu
if not self.child:
Expand All @@ -51,9 +55,14 @@ def make(self, request, models=None, deep=1, deep_limit=0):
menu_item['target_blank'] = self.target_blank
menu_item['menu_type'] = self.menu_type or 'group'

if menu_item['menu_type'] != 'group':
if menu_item['url'] == request.path:
menu_item['active'] = True

if self.child:
if deep_limit == 0 or deep <= deep_limit:
child_list = []
has_child_active = False
for child in self.child:
deep += 1
child_menu = child.make(request, models, deep, deep_limit)
Expand All @@ -62,8 +71,13 @@ def make(self, request, models=None, deep=1, deep_limit=0):
if child_menu.get('menu_type', 'group') == 'group':
if len(child_menu.get('child')) == 0:
continue
if child_menu.get('active') is True:
has_child_active = True

child_list.append(child_menu)
menu_item['child'] = child_list
if has_child_active is True:
menu_item['active'] = True
else:
return None
return menu_item
Expand Down
67 changes: 67 additions & 0 deletions adminlteui/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import datetime

from django.contrib import admin, messages
from django.db import models
from django.conf import settings
from django.utils import timezone
from django.utils.translation import gettext_lazy as _


class DateRangeFilter(admin.FieldListFilter):
# Reference https://github.com/andreynovikov/django-daterangefilter
template = 'adminlte/date_range_filter.html'
date_format = 'YYYY/MM/DD'

def __init__(self, field, request, params, model, model_admin, field_path):
self.field_name = field_path
self.lookup_kwarg_gte = '{}__gte'.format(field_path)
self.lookup_kwarg_lte = '{}__lte'.format(field_path)
self.lookup_gte = params.get(self.lookup_kwarg_gte)
self.lookup_lte = params.get(self.lookup_kwarg_lte)

if self.lookup_gte == '':
params.pop(self.lookup_kwarg_gte)

if self.lookup_lte == '':
params.pop(self.lookup_kwarg_lte)
if self.lookup_gte and self.lookup_lte:
self.lookup_val = '{} - {}'.format(self.lookup_gte, self.lookup_lte)
# if we are filtering DateTimeField we should add one day to final date
if "__" in field_path:
related_model, field = field_path.split("__")
field = model._meta.get_field(related_model).related_model._meta.get_field(field)
else:
field = model._meta.get_field(field_path)

if isinstance(field, models.DateTimeField):
try:
gte_date = datetime.datetime.strptime(self.lookup_gte, '%Y-%m-%d')
lte_date = datetime.datetime.strptime(self.lookup_lte, '%Y-%m-%d')
lte_date = lte_date + datetime.timedelta(seconds=3600 * 24 - 1)
if settings.USE_TZ:
gte_date = timezone.make_aware(gte_date, timezone.get_current_timezone())
lte_date = timezone.make_aware(lte_date, timezone.get_current_timezone())
params[self.lookup_kwarg_gte] = gte_date.strftime('%Y-%m-%d %H:%M:%S%z')
params[self.lookup_kwarg_lte] = lte_date.strftime('%Y-%m-%d %H:%M:%S%z')
except ValueError:
messages.add_message(request, messages.ERROR,
_("Invalid date for '%(field_name)s' field range filter") % {
'field_name': field.verbose_name})
else:
self.lookup_val = ''

super().__init__(field, request, params, model, model_admin, field_path)

def choices(self, changelist):
yield {
'field_name': self.field_path,
'value': self.lookup_val,
'date_format': self.date_format,
'query_string': changelist.get_query_string(remove=self._get_expected_fields())
}

def expected_parameters(self):
return self._get_expected_fields()

def _get_expected_fields(self):
return [self.lookup_kwarg_gte, self.lookup_kwarg_lte]
Binary file removed adminlteui/locale/zh-Hans/LC_MESSAGES/django.mo
Binary file not shown.
Loading

0 comments on commit 6f86a4a

Please sign in to comment.