Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move todayBtn to quoted_bool_options #79

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions datetimewidget/widgets.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@

__author__ = 'Alfredo Saglimbeni'

from datetime import datetime
import re
import uuid

from django.forms import forms, widgets
from django.forms.widgets import MultiWidget, DateTimeInput, DateInput, TimeInput
from django.forms import widgets
from django.forms.widgets import DateTimeInput, DateInput, TimeInput
from django.utils.formats import get_format, get_language
from django.utils.safestring import mark_safe
from django.utils.six import string_types

try:
from django.forms.widgets import to_current_timezone
except ImportError:
to_current_timezone = lambda obj: obj # passthrough, no tz support
to_current_timezone = lambda obj: obj # passthrough, no tz support


# This should be updated as more .po files are added to the datetime picker javascript code
Expand All @@ -38,7 +37,7 @@
'th', 'tr',
'ua', 'uk',
'zh-CN', 'zh-TW',
])
])


def get_supported_language(language_country_code):
Expand Down Expand Up @@ -121,7 +120,7 @@ def get_supported_language(language_country_code):
$(function(){$("#%(id)s").datetimepicker({%(options)s}).find('input').addClass("form-control");});
</script>
"""
}
}

CLEAR_BTN_TEMPLATE = {2: """<span class="add-on"><i class="icon-remove"></i></span>""",
3: """<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>"""}
Expand All @@ -136,23 +135,23 @@ def get_supported_language(language_country_code):
'startView',
'minView',
'maxView',
'todayBtn',
'language',
'pickerPosition',
'viewSelect',
'initialDate',
'weekStart',
'minuteStep'
'daysOfWeekDisabled',
])
])

# to traslate boolean object to javascript
quoted_bool_options = set([
'autoclose',
'todayHighlight',
'showMeridian',
'clearBtn',
])
'todayBtn',
])


def quote(key, value):
Expand All @@ -165,7 +164,7 @@ def quote(key, value):
return "'%s'" % value

if key in quoted_bool_options and isinstance(value, bool):
return {True:'true',False:'false'}[value]
return {True: 'true', False: 'false'}[value]

return value

Expand All @@ -177,7 +176,7 @@ class PickerWidgetMixin(object):

def __init__(self, attrs=None, options=None, usel10n=None, bootstrap_version=None):

if bootstrap_version in [2,3]:
if bootstrap_version in [2, 3]:
self.bootstrap_version = bootstrap_version
else:
# default 2 to mantain support to old implemetation of django-datetime-widget
Expand Down Expand Up @@ -206,7 +205,7 @@ def __init__(self, attrs=None, options=None, usel10n=None, bootstrap_version=Non
self.options['format'] = toJavascript_re.sub(
lambda x: dateConversiontoJavascript[x.group()],
self.format
)
)

# Set the local language
self.options['language'] = get_supported_language(get_language())
Expand All @@ -219,15 +218,15 @@ def __init__(self, attrs=None, options=None, usel10n=None, bootstrap_version=Non
self.format = toPython_re.sub(
lambda x: dateConversiontoPython[x.group()],
format
)
)

super(PickerWidgetMixin, self).__init__(attrs, format=self.format)

def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs)
rendered_widget = super(PickerWidgetMixin, self).render(name, value, final_attrs)

#if not set, autoclose have to be true.
# if not set, autoclose have to be true.
self.options.setdefault('autoclose', True)

# Build javascript options out of python dictionary
Expand All @@ -240,19 +239,19 @@ def render(self, name, value, attrs=None):
# Use provided id or generate hex to avoid collisions in document
id = final_attrs.get('id', uuid.uuid4().hex)

clearBtn = quote('clearBtn', self.options.get('clearBtn', 'true')) == 'true'
labelField = final_attrs.get('label', False)
clear_btn = quote('clearBtn', self.options.get('clearBtn', 'true')) == 'true'
label_field = final_attrs.get('label', False)

return mark_safe(
BOOTSTRAP_INPUT_TEMPLATE[self.bootstrap_version]
% dict(
id=id,
rendered_widget=rendered_widget,
label=LABEL_TEMPLATE[self.bootstrap_version] % dict(label=labelField) if labelField else "",
clear_button=CLEAR_BTN_TEMPLATE[self.bootstrap_version] if clearBtn else "",
glyphicon=self.glyphicon,
options=js_options
)
% dict(
id=id,
rendered_widget=rendered_widget,
label=LABEL_TEMPLATE[self.bootstrap_version] % dict(label=label_field) if label_field else "",
clear_button=CLEAR_BTN_TEMPLATE[self.bootstrap_version] if clear_btn else "",
glyphicon=self.glyphicon,
options=js_options
)
)

def _media(self):
Expand All @@ -266,14 +265,15 @@ def _media(self):
return widgets.Media(
css={
'all': ('css/datetimepicker.css',)
},
},
js=js
)
)

media = property(_media)


class DateTimeWidget(PickerWidgetMixin, DateTimeInput):

"""
DateTimeWidget is the corresponding widget for Datetime field, it renders both the date and time
sections of the datetime picker.
Expand All @@ -294,6 +294,7 @@ def __init__(self, attrs=None, options=None, usel10n=None, bootstrap_version=Non


class DateWidget(PickerWidgetMixin, DateInput):

"""
DateWidget is the corresponding widget for Date field, it renders only the date section of
datetime picker.
Expand All @@ -316,6 +317,7 @@ def __init__(self, attrs=None, options=None, usel10n=None, bootstrap_version=Non


class TimeWidget(PickerWidgetMixin, TimeInput):

"""
TimeWidget is the corresponding widget for Time field, it renders only the time section of
datetime picker.
Expand All @@ -336,4 +338,3 @@ def __init__(self, attrs=None, options=None, usel10n=None, bootstrap_version=Non
options['format'] = options.get('format', 'hh:ii')

super(TimeWidget, self).__init__(attrs, options, usel10n, bootstrap_version)