Skip to content

Instantly share code, notes, and snippets.

@jensens
Created October 16, 2017 11:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jensens/74dd6e43ad4e7ddc19a2ce28ebfa3a2d to your computer and use it in GitHub Desktop.
Save jensens/74dd6e43ad4e7ddc19a2ce28ebfa3a2d to your computer and use it in GitHub Desktop.
Show Event Start-End with Zope Content Provider usage
<time class="datum"
datetime="${result/iso}"
tal:define="result python:view.datedict"
tal:condition="result"
i18n:domain="my.fancy.site">
<--
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-sameday>
<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-wholeday-notsameday>
<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-openend>
<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>
&ndash;
<span class="stime">${result/etime}</span>
<span i18n:translate="label_time_postfix"></span>
</tal:if-notwholeday-sameday-notopenend>
<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>
&ndash;
<br />
<span class="edate">${result/eday}, ${result/edate}</span><span class="sep">, </span>
<span class="stime">${result/etime}</span>
</tal:if-notwholeday-notsameday-notopenend>
</time>
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
@provider(ITALNamespaceData)
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')
@implementer(ITALDateValue)
class DateProvider(ContentProviderBase):
datedict = {}
wide = False
template = ViewPageTemplateFile('date.pt')
def render(self, *args, **kwargs):
return self.template()
<configure
xmlns="http://namespaces.zope.org/zope">
<!-- a contentprovider -->
<adapter
factory=".date.DateProvider"
for="*
my.fancy.site.interfaces.IBrowserLayer
*"
name="fancy.date"
provides="zope.contentprovider.interfaces.IContentProvider"
/>
</configure>
<!DOCTYPE html>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:tal="http://xml.zope.org/namespaces/tal" xmlns:i18n="http://xml.zope.org/namespaces/i18n" i18n:domain="my.fancy.site">
<body>
<time tal:define="datedict view/value;"
tal:replace="structure provider:fancy.date">
</time>
</body>
</html>
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 plone.app.event.base 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'
TIME_FORMAT = '%H:%M'
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:
return
result = {
'edate': None,
'stime': None,
'etime': None,
}
start = accessor.start
end = accessor.end
if not start and end:
return
try:
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
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment