""" Content processor to display upcoming events on every page you want. """
from django.core.cache import cache
from .models import Event


def events_overview(request):  # Ignore PyLintBear (W0613)
    """
    Adds event information as variables to the template context on every page.

    For speed reasons everything will be cached for an hour. the following
    variables will be added to the template context:
    * current_event: If an event is running at this moment, the correspondi
      event object.
    * next_event: the next event that is upcoming.
    * upcoming_events: the next 3 events that are upcoming.

    :param request: An Django HTTPRequest object
    :return: dict() with the new context variables
    """
    current_event = cache.get('current_event', False)
    next_event = cache.get('next_event', False)
    upcoming_events = cache.get('upcoming_events', False)

    if not current_event:
        current_event = Event.objects.current_event()
        cache.set('current_event', current_event, 360)
    if not next_event:
        next_event = Event.objects.next_event()
        cache.set('next_event', next_event, 360)

    if not upcoming_events and current_event:
        upcoming_events = Event.objects.upcoming(limit=3)
        cache.set('upcoming_events', upcoming_events, 360)
    elif not upcoming_events:
        upcoming_events = Event.objects.upcoming()[1:4]
        cache.set('upcoming_events', upcoming_events, 360)
    return {
        'current_event': current_event,
        'next_event': next_event,
        'upcoming_events': upcoming_events
    }
