Created
May 11, 2016 17:42
-
-
Save bmihelac/51f57912a762a219a262a27d48424fa1 to your computer and use it in GitHub Desktop.
Wagtail FormattedDateBlock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import unicode_literals | |
from django import forms | |
from django.utils.functional import cached_property | |
from wagtail.wagtailcore.blocks.field_block import DateBlock | |
class FormattedDateBlock(DateBlock): | |
def __init__(self, django_format=None, js_format=None, *args, **kwargs): | |
self.django_format = django_format | |
self.js_format = js_format | |
super(FormattedDateBlock, self).__init__(*args, **kwargs) | |
@cached_property | |
def field(self): | |
from .widgets import MyAdminDateInput | |
field_kwargs = {'widget': MyAdminDateInput( | |
format=self.django_format, | |
js_format=self.js_format, | |
)} | |
field_kwargs.update(self.field_options) | |
return forms.DateField(**field_kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
published_on = FormattedDateBlock( | |
django_format='%d.%m.%Y.', | |
js_format='d.m.Y.' | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import unicode_literals | |
import json | |
from django.utils.formats import get_format | |
from wagtail.wagtailadmin.widgets import AdminDateInput | |
class MyAdminDateInput(AdminDateInput): | |
def __init__(self, js_format='Y-m-d', *args, **kwargs): | |
self.js_format = js_format | |
super(MyAdminDateInput, self).__init__(*args, **kwargs) | |
def render_js_init(self, id_, name, value): | |
return 'initDateChooser({0}, {1});'.format( | |
json.dumps(id_), | |
json.dumps({ | |
'dayOfWeekStart': get_format('FIRST_DAY_OF_WEEK'), | |
'format': self.js_format | |
}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment