event_cards.py 817 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
15
16
@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,
            end__gte=timezone.now()
        ).order_by('end')[:6]
17
18

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

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