Skip to content

Commit

Permalink
Merge pull request getpelican#1354 from moormaster/master
Browse files Browse the repository at this point in the history
[events] fixed: populate events-list template variable with the Article() objects
  • Loading branch information
justinmayer authored Dec 18, 2021
2 parents 9c08225 + f03cf23 commit 483215d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
56 changes: 32 additions & 24 deletions events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""

from datetime import datetime, timedelta
from pelican import signals, utils
from pelican import signals, utils, contents
from collections import namedtuple, defaultdict
import icalendar
import logging
Expand All @@ -33,30 +33,29 @@

events = []
localized_events = defaultdict(list)
Event = namedtuple("Event", "dtstart dtend metadata")


def parse_tstamp(ev, field_name):
def parse_tstamp(metadata, field_name):
"""Parse a timestamp string in format "YYYY-MM-DD HH:MM"
:returns: datetime
"""
try:
return datetime.strptime(ev[field_name], '%Y-%m-%d %H:%M')
return datetime.strptime(metadata[field_name], '%Y-%m-%d %H:%M')
except Exception as e:
log.error("Unable to parse the '%s' field in the event named '%s': %s" \
% (field_name, ev['title'], e))
% (field_name, metadata['title'], e))
raise


def parse_timedelta(ev):
def parse_timedelta(metadata):
"""Parse a timedelta string in format [<num><multiplier> ]*
e.g. 2h 30m
:returns: timedelta
"""

chunks = ev['event-duration'].split()
chunks = metadata['event-duration'].split()
tdargs = {}
for c in chunks:
try:
Expand All @@ -66,41 +65,50 @@ def parse_timedelta(ev):
except KeyError:
log.error("""Unknown time multiplier '%s' value in the \
'event-duration' field in the '%s' event. Supported multipliers \
are: '%s'.""" % (c, ev['title'], ' '.join(TIME_MULTIPLIERS)))
are: '%s'.""" % (c, metadata['title'], ' '.join(TIME_MULTIPLIERS)))
raise RuntimeError("Unknown time multiplier '%s'" % c)
except ValueError:
log.error("""Unable to parse '%s' value in the 'event-duration' \
field in the '%s' event.""" % (c, ev['title']))
field in the '%s' event.""" % (c, metadata['title']))
raise ValueError("Unable to parse '%s'" % c)


return timedelta(**tdargs)


def parse_article(generator, metadata):
def basic_isoformat(datetime_value):
return datetime_value.strftime("%Y%m%dT%H%M%S")


def parse_article(content):
"""Collect articles metadata to be used for building the event calendar
:returns: None
"""
if 'event-start' not in metadata:
if not isinstance(content, contents.Article):
return

dtstart = parse_tstamp(metadata, 'event-start')
if 'event-start' not in content.metadata:
return

if 'event-end' in metadata:
dtend = parse_tstamp(metadata, 'event-end')
dtstart = parse_tstamp(content.metadata, 'event-start')

elif 'event-duration' in metadata:
dtdelta = parse_timedelta(metadata)
if 'event-end' in content.metadata:
dtend = parse_tstamp(content.metadata, 'event-end')

elif 'event-duration' in content.metadata:
dtdelta = parse_timedelta(content.metadata)
dtend = dtstart + dtdelta

else:
msg = "Either 'event-end' or 'event-duration' must be" + \
" speciefied in the event named '%s'" % metadata['title']
" speciefied in the event named '%s'" % content.metadata['title']
log.error(msg)
raise ValueError(msg)

events.append(Event(dtstart, dtend, metadata))
content.event_plugin_data = {"dtstart": dtstart, "dtend": dtend}

events.append(content)


def generate_ical_file(generator):
Expand All @@ -127,9 +135,9 @@ def generate_ical_file(generator):
for e in curr_events:
ie = icalendar.Event(
summary=e.metadata['summary'],
dtstart=e.dtstart,
dtend=e.dtend,
dtstamp=e.metadata['date'],
dtstart=basic_isoformat(e.event_plugin_data["dtstart"]),
dtend=basic_isoformat(e.event_plugin_data["dtend"]),
dtstamp=basic_isoformat(e.metadata['date']),
priority=5,
uid=e.metadata['title'] + e.metadata['summary'],
)
Expand Down Expand Up @@ -161,10 +169,10 @@ def generate_events_list(generator):

if not localized_events:
generator.context['events_list'] = sorted(events, reverse = True,
key=lambda ev: (ev.dtstart, ev.dtend))
key=lambda ev: (ev.event_plugin_data["dtstart"], ev.event_plugin_data["dtend"]))
else:
generator.context['events_list'] = {k: sorted(v, reverse = True,
key=lambda ev: (ev.dtstart, ev.dtend))
key=lambda ev: (ev.event_plugin_data["dtstart"], ev.event_plugin_data["dtend"]))
for k, v in localized_events.items()}

def initialize_events(article_generator):
Expand All @@ -178,7 +186,7 @@ def initialize_events(article_generator):

def register():
signals.article_generator_init.connect(initialize_events)
signals.article_generator_context.connect(parse_article)
signals.content_object_init.connect(parse_article)
signals.article_generator_finalized.connect(generate_localized_events)
signals.article_generator_finalized.connect(generate_ical_file)
signals.article_generator_finalized.connect(generate_events_list)
Expand Down
16 changes: 8 additions & 8 deletions events/events_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

{% if events_list %}
<ul class="post-list">
{% for evstart, evend, event in events_list %}
{% for event in events_list %}
<li>
<p>
<a href="../{{event.slug}}.html">
<b>{{event['title']}}</b>
<a href="{{ SITEURL }}/{{ event.url }}">
<b>{{ event.metadata["title"] }}</b>
</a>
</p>
<p>
{% if evstart.date() == evend.date() %}
From {{evstart}} to {{evend.time()}}
{% if event.event_plugin_data["dtstart"].date() == event.event_plugin_data["dtend"].date() %}
From {{ event.event_plugin_data["dtstart"] }} to {{ event.event_plugin_data["dtend"].time() }}
{% else %}
From {{evstart}} to {{evend}}
From {{ event.event_plugin_data["dtstart"] }} to {{ event.event_plugin_data["dtend"] }}
{% endif %}
</p>

{% if event.location %}
<p>Location: {{event.location}}</p>
<p>Location: {{ event.metadata["location"] }}</p>
{% endif %}

<p>{{event['summary']}}</p>
<p>{{ event.metadata["summary"] }}</p>

</li>
{% endfor %}
Expand Down

0 comments on commit 483215d

Please sign in to comment.