event_cards.py 889 Bytes
Newer Older
1
2
3
from django import template
from django.utils import timezone

4
from events import services
5
6
7
8
9
from events.models import Event

register = template.Library()


Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
10
11
12
13
14
@register.inclusion_tag('events/event_cards.html', takes_context=True)
def render_event_cards(context, events=None):
    if events is None:
        events = Event.objects.filter(
            published=True,
15
            start__gte=timezone.now() - timezone.timedelta(hours=24),
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
16
            end__gte=timezone.now()
17
        ).order_by('start')[:6]
18
19

    try:
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
20
        cards = [{
21
                'event': x,
22
                'current_user_registration': services.is_user_registered(
23
                    context['user'], x),
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
24
            } for x in events]
25
    except AttributeError:
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
26
        cards = [{
27
28
                'event': x,
                'current_user_registration': None
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
29
            } for x in events]
30

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
31
    return {'events': cards}