diff --git a/pyproject.toml b/pyproject.toml index 19a8851..8c27951 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "django-smartbase-admin" -version = "0.2.89" +version = "0.2.90" description = "" authors = ["SmartBase "] readme = "README.md" diff --git a/src/django_smartbase_admin/engine/filter_widgets.py b/src/django_smartbase_admin/engine/filter_widgets.py index ba532e6..fd1a7e8 100644 --- a/src/django_smartbase_admin/engine/filter_widgets.py +++ b/src/django_smartbase_admin/engine/filter_widgets.py @@ -326,12 +326,19 @@ def get_shortcuts_dict(self): shortcuts[key].append(self.process_shortcut(shortcut, now)) return shortcuts + def get_default_label(self): + if self.default_value_shortcut_index is not None: + return self.get_shortcuts()[self.default_value_shortcut_index]["label"] + return super().get_default_value() + def get_default_value(self): if self.default_value_shortcut_index is not None: selected_shortcut_value = self.get_shortcuts()[ self.default_value_shortcut_index ]["value"] - return self.get_value_from_date_or_range(selected_shortcut_value) + return SBAdminViewService.json_dumps_for_url( + self.get_value_from_date_or_range(selected_shortcut_value) + ) return super().get_default_value() def get_data(self): @@ -374,9 +381,14 @@ def get_range_from_value(cls, filter_value): return [None, None] date_format = cls.DATE_FORMAT if type(filter_value) is list: + if type(filter_value[0]) is int: + return [ + timezone.now() + timedelta(days=filter_value[0]), + timezone.now() + timedelta(days=filter_value[1]), + ] return [ - timezone.now() + timedelta(days=filter_value[0]), - timezone.now() + timedelta(days=filter_value[1]), + datetime.strptime(filter_value[0], date_format), + datetime.strptime(filter_value[1], date_format), ] try: days_range = json.loads(filter_value) @@ -397,9 +409,11 @@ def get_range_from_value(cls, filter_value): def get_value_from_date_or_range(cls, date_or_range): if not isinstance(date_or_range, list): return datetime.strftime(date_or_range, cls.DATE_FORMAT) + if type(date_or_range[0]) is int: + return date_or_range date_from = datetime.strftime(date_or_range[0], cls.DATE_FORMAT) date_to = datetime.strftime(date_or_range[1], cls.DATE_FORMAT) - return f"{date_from}{cls.DATE_RANGE_SPLIT}{date_to}" + return [date_from, date_to] def parse_value_from_input(self, request, filter_value): return self.get_range_from_value(filter_value) diff --git a/src/django_smartbase_admin/static/sb_admin/src/js/datepicker_plugins.js b/src/django_smartbase_admin/static/sb_admin/src/js/datepicker_plugins.js index 34b90ed..a20b56e 100644 --- a/src/django_smartbase_admin/static/sb_admin/src/js/datepicker_plugins.js +++ b/src/django_smartbase_admin/static/sb_admin/src/js/datepicker_plugins.js @@ -271,6 +271,24 @@ export const createRadioInput = (id, name, value, label, checked, index) => { // eslint-disable-next-line no-unused-vars export const customActionsPlugin = (fp) => { + const setInitialValueAndLabel = (realInput, shortcuts, baseId) => { + const shortcutValue = JSON.parse(realInput.value) + const from = new Date() + from.setDate(from.getDate() + shortcutValue[0]) + + const to = new Date() + to.setDate(to.getDate() + shortcutValue[1]) + fp.setDate([from, to], false, fp.config.dateFormat) + + shortcuts.forEach((shortcut, idx) => { + if(JSON.stringify(shortcut.value) === realInput.value) { + document.getElementById(`${baseId}_range${idx}`).checked = true + realInput.dataset['label'] = shortcut.label + } + }) + } + + const createShortcuts = () => { const realInput = document.getElementById(fp.element.dataset.sbadminDatepickerRealInputId) const baseId = realInput.id @@ -312,22 +330,13 @@ export const customActionsPlugin = (fp) => { }) fp.calendarContainer.prepend(el) + if(realInput.value){ + setInitialValueAndLabel(realInput, shortcuts, baseId) + } + realInput.addEventListener('SBTableFilterFormLoad', () => { try { - const shortcutValue = JSON.parse(realInput.value) - const from = new Date() - from.setDate(from.getDate() + shortcutValue[0]) - - const to = new Date() - to.setDate(to.getDate() + shortcutValue[1]) - fp.setDate([from, to], false, fp.config.dateFormat) - - shortcuts.forEach((shortcut, idx) => { - if(JSON.stringify(shortcut.value) === realInput.value) { - document.getElementById(`${baseId}_range${idx}`).checked = true - realInput.dataset['label'] = shortcut.label - } - }) + setInitialValueAndLabel(realInput, shortcuts, baseId) } catch { fp.setDate(realInput.value, false, fp.config.dateFormat) diff --git a/src/django_smartbase_admin/templates/sb_admin/filter_widgets/date_field.html b/src/django_smartbase_admin/templates/sb_admin/filter_widgets/date_field.html index 6a148aa..cc59886 100644 --- a/src/django_smartbase_admin/templates/sb_admin/filter_widgets/date_field.html +++ b/src/django_smartbase_admin/templates/sb_admin/filter_widgets/date_field.html @@ -7,7 +7,7 @@ id="{{ filter_widget.input_id }}" name="{{ filter_widget.input_name }}" form="{{ filter_widget.view_id }}-filter-form" - {% if filter_widget.get_default_value %}value="{{ filter_widget.get_default_value }}"{% endif %} + {% if filter_widget.get_default_value %}value="{{ filter_widget.get_default_value }}" data-label="{{ filter_widget.get_default_label }}"{% endif %} {% if not all_filters_visible %}disabled{% endif %} >