Skip to content

Commit

Permalink
Allow datetime types
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrián Cuesta committed Dec 14, 2021
1 parent b934990 commit 8f7deef
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
21 changes: 21 additions & 0 deletions django_better_admin_arrayfield/forms/widgets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import forms
from django.contrib.admin.widgets import AdminSplitDateTime


class DynamicArrayWidget(forms.TextInput):
Expand Down Expand Up @@ -46,3 +47,23 @@ class DynamicArrayTextareaWidget(DynamicArrayWidget):
def __init__(self, *args, **kwargs):
kwargs.setdefault("subwidget_form", forms.Textarea)
super().__init__(*args, **kwargs)

class DatetimeWidget(DynamicArrayWidget):
"""Datetime widget for array datetime lists"""

def __init__(self, *args, **kwargs):
kwargs['subwidget_form'] = AdminSplitDateTime
super().__init__(*args, **kwargs)

def value_from_datadict(self, data, files, name):
try:
getter = data.getlist
dates = getter(name + '_0')
times = getter(name + '_1')
return_data = []
for i in range(len(dates)):
if dates[i] and times[i]:
return_data.append(' '.join([dates[i], times[i]]))
return return_data
except AttributeError:
return data.get(name)
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,29 @@ window.addEventListener('load', function () {

addRemoveEventListener(newElement);
parentElement.appendChild(newElement);
});
}

for (const inp of document.getElementsByTagName('input')) {
if (
inp.type === 'text' &&
inp.classList.contains('vTimeField') &&
!inp.nextSibling?.classList.contains('datetimeshortcuts')
) {
DateTimeShortcuts.addClock(inp);
DateTimeShortcuts.addTimezoneWarning(inp);
inp.value = '';
} else if (
inp.type === 'text' &&
inp.classList.contains('vDateField') &&
!inp.nextSibling?.classList.contains('datetimeshortcuts')
) {
DateTimeShortcuts.addCalendar(inp);
DateTimeShortcuts.addTimezoneWarning(inp);
inp.value = '';
}
}
});
}

django.jQuery(".dynamic-array-widget").not(".empty-form .dynamic-array-widget").each(
(index, widgetElement) => initializeWidget(widgetElement)
);
Expand All @@ -45,4 +65,4 @@ window.addEventListener('load', function () {
widgetElement => initializeWidget(widgetElement)
);
});
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8f7deef

Please sign in to comment.