Skip to content

Instantly share code, notes, and snippets.

Created October 16, 2017 11:52
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jensens/74dd6e43ad4e7ddc19a2ce28ebfa3a2d to your computer and use it in GitHub Desktop.
Show Event Start-End with Zope Content Provider usage
<time class="datum"
tal:define="result python:view.datedict"
case 1: whole day, same day
Mo, 21.01.2015
case 2: whole day, not same day
Mo, 21.01.2015 -
Mi, 23.01.2015
but no line break in wide mode
case 3: not whole day, same day, open end
Mo, 21.01.2015 8:00 Uhr
case 4: not whole day, same day, not open end
Mo, 21.01.2015
8:00 - 23:00 Uhr
but no line break in wide mode
case 5: not whole day, not same day, not open end
Mo, 21.01.2015 8:00 Uhr -
Mi, 23.01.2015 23:00 Uhr
<tal:if-wholeday-sameday tal:condition="python: result['whole'] and result['same']">
<!-- case 1 -->
<span class="sdate">${result/sday}, ${result/sdate}</span>
<tal:if-wholeday-notsameday tal:condition="python: result['whole'] and not result['same']">
<!-- case 2 -->
<span class="sdate">${result/sday}, ${result/sdate}</span> &ndash;<br tal:condition="python: not view.wide" />
<span class="edate">${result/eday}, ${result/edate}</span>
<tal:if-notwholeday-sameday-openend tal:condition="python: not result['whole'] and result['same'] and result['open']">
<!-- case 3 -->
<span class="sdate">${result/sday}, ${result/sdate}</span><span class="sep">, </span>
<span class="stime">${result/stime}</span>
<span i18n:translate="label_time_postfix"></span>
<tal:if-notwholeday-sameday-notopenend tal:condition="python: not result['whole'] and result['same'] and not result['open']">
<!-- case 4 -->
<span class="sdate">${result/sday}, ${result/sdate}</span><br tal:condition="python: not view.wide" />
<span class="stime">${result/stime}</span>
<span class="stime">${result/etime}</span>
<span i18n:translate="label_time_postfix"></span>
<tal:if-notwholeday-notsameday-notopenend tal:condition="python: not result['whole'] and not result['same'] and not result['open']">
<!-- case 5 -->
<span class="sdate">${result/sday}, ${result/sdate}</span><span class="sep">, </span>
<span class="stime">${result/stime}</span>
<br />
<span class="edate">${result/eday}, ${result/edate}</span><span class="sep">, </span>
<span class="stime">${result/etime}</span>
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from zope.contentprovider.interfaces import ITALNamespaceData
from zope.contentprovider.provider import ContentProviderBase
from zope.interface import implementer
from zope.interface import Interface
from zope.interface import provider
import zope.schema
class ITALDateValue(Interface):
datedict = zope.schema.Dict(title=u'date range dict data')
wide = zope.schema.Bool(title=u'try to line break or not')
class DateProvider(ContentProviderBase):
datedict = {}
wide = False
template = ViewPageTemplateFile('')
def render(self, *args, **kwargs):
return self.template()
<!-- a contentprovider -->
<!DOCTYPE html>
<html lang="en" xml:lang="en" xmlns="" xmlns:tal="" xmlns:i18n="" i18n:domain="">
<time tal:define="datedict view/value;"
from .utils import prepare_datetime
from plone.tiles import Tile
class DateDisplayTile(Tile):
"""A tile to display the date."""
def value(self):
return prepare_datetime(self.context)
from Acquisition import aq_base
from import date_speller
from plone.event.interfaces import IEventAccessor
from plone.event.utils import is_same_day
from plone.event.utils import pydt
DATE_FORMAT = '%d.%m.%Y'
ISO_LONG = '%Y-%m-%dT%H:%MT%Z'
ISO_SHORT = '%Y-%m-%d'
def prepare_datetime(event):
accessor = IEventAccessor(aq_base(event), None)
if not accessor or not accessor.start:
result = {
'edate': None,
'stime': None,
'etime': None,
start = accessor.start
end = accessor.end
if not start and end:
result['sdate'] = start.strftime(DATE_FORMAT)
result['iso'] = start.strftime(ISO_LONG)
start_speller = date_speller(event, start)
result['sday'] = start_speller['wkday_abbr']
result['whole'] = accessor.whole_day
result['open'] = accessor.open_end
result['same'] = is_same_day(start, end)
if not accessor.whole_day:
result['stime'] = start.strftime(TIME_FORMAT)
if not accessor.whole_day and not accessor.open_end:
result['etime'] = end.strftime(TIME_FORMAT)
if not is_same_day(start, end):
result['edate'] = end.strftime(DATE_FORMAT)
end_speller = date_speller(event, end)
result['eday'] = end_speller['wkday_abbr']
result['smonth'] = start.month
result['syear'] = start.year
except ValueError:
# might be wrong date, like datetime.datetime(1010, 5, 29, 10, 0)
# e.g. /ogfa/seiten/pristavba-budovy-slovenskeho-narodneho-divadla
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment