Add DateTimeFieldRenderer to show human-friendly timestamps.

This leverages the `humanize` package to do so.  Unfortunately that doesn't
seem to handle tz-aware times though, so we may need to revisit that at
some point...?
This commit is contained in:
Lance Edgar 2014-11-18 19:37:52 -06:00
parent 2326482bb6
commit 9806c7a0a2
3 changed files with 68 additions and 11 deletions

View file

@ -75,6 +75,7 @@ requires = [
# deprecated 'paster create' (and friends). # deprecated 'paster create' (and friends).
'pyramid>=1.3a1', # 1.3b2 1.4.5 'pyramid>=1.3a1', # 1.3b2 1.4.5
'humanize', # 0.5.1
'Mako', # 0.6.2 'Mako', # 0.6.2
'pyramid_beaker>=0.6', # 0.6.1 'pyramid_beaker>=0.6', # 0.6.1
'pyramid_debugtoolbar', # 1.0 'pyramid_debugtoolbar', # 1.0

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar # Copyright © 2010-2014 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -21,11 +20,16 @@
# along with Rattail. If not, see <http://www.gnu.org/licenses/>. # along with Rattail. If not, see <http://www.gnu.org/licenses/>.
# #
################################################################################ ################################################################################
""" """
FormAlchemy Field Renderers FormAlchemy Field Renderers
""" """
from .common import * from tailbone.forms.renderers.common import (
AutocompleteFieldRenderer,
DateTimeFieldRenderer,
EnumFieldRenderer,
YesNoFieldRenderer,
)
from .people import * from .people import *
from .products import * from .products import *

View file

@ -1,9 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2012 Lance Edgar # Copyright © 2010-2014 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -21,16 +20,21 @@
# along with Rattail. If not, see <http://www.gnu.org/licenses/>. # along with Rattail. If not, see <http://www.gnu.org/licenses/>.
# #
################################################################################ ################################################################################
""" """
Common Field Renderers Common Field Renderers
""" """
from __future__ import unicode_literals
import pytz
import humanize
import formalchemy
from formalchemy.fields import FieldRenderer, SelectFieldRenderer, CheckBoxFieldRenderer from formalchemy.fields import FieldRenderer, SelectFieldRenderer, CheckBoxFieldRenderer
from pyramid.renderers import render from pyramid.renderers import render
from webhelpers.html import HTML
from rattail.time import timezone
__all__ = ['AutocompleteFieldRenderer', 'EnumFieldRenderer', 'YesNoFieldRenderer']
class AutocompleteFieldRenderer(FieldRenderer): class AutocompleteFieldRenderer(FieldRenderer):
@ -76,6 +80,54 @@ class AutocompleteFieldRenderer(FieldRenderer):
return unicode(value) return unicode(value)
def pretty_datetime(config, value):
"""
Formats a datetime as a "pretty" human-readable string, with a tooltip
showing the ISO string value.
:param config: Reference to a config object.
:param value: A ``datetime.datetime`` instance. Note that if this instance
is not timezone-aware, its timezone is assumed to be UTC.
"""
if not value:
return ''
# Make sure we're dealing with a tz-aware value. If we're given a naive
# value, we assume it to be local to the UTC timezone.
if not value.tzinfo:
value = pytz.utc.localize(value)
# Convert value to local timezone, and make a naive copy.
local = timezone(config)
value = local.normalize(value.astimezone(local))
naive_value = value.replace(tzinfo=None)
return HTML.tag('span',
title=value.strftime('%Y-%m-%d %H:%M:%S %Z%z'),
c=humanize.naturaltime(naive_value))
class DateTimeFieldRenderer(formalchemy.DateTimeFieldRenderer):
"""
Custom date/time field renderer, which displays a "pretty" value in
read-only mode, leveraging config to show the correct timezone.
"""
def __init__(self, config):
self.config = config
def __call__(self, field):
super(DateTimeFieldRenderer, self).__init__(field)
return self
def render_readonly(self, **kwargs):
value = self.raw_value
if not value:
return ''
return pretty_datetime(self.config, value)
class EnumFieldRenderer(SelectFieldRenderer): class EnumFieldRenderer(SelectFieldRenderer):
""" """
Renderer for simple enumeration fields. Renderer for simple enumeration fields.