aboutsummaryrefslogtreecommitdiffstats
path: root/schedule/views.py
blob: ad4bbc266cd8e07526810c8768cfb3598ac27bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-

from django.shortcuts import render
from django.views.generic.base import TemplateView

from schedule.models import Event, EventAttachment

import re
import datetime


class ScheduleView(TemplateView):
    """
    class-based view to show events with their attachments
    generate the schedule page
    """
    template_name = 'schedule/schedule.html'

    def get_context_data(self, **kwargs):
        context = super(type(self), self).get_context_data(**kwargs)
        event_all_qs = Event.objects.all().order_by('date')
        if event_all_qs:
            event_empty = False
            # get all the dates 
            day_all = ['%s'%e.date.isoformat() for e in event_all_qs]
            # deduplicate while preserver order
            tmplist = []
            for e in day_all:
                if e not in tmplist:
                    tmplist.append(e)
            day_all[:] = tmplist
            # make a dict to group events by date
            event_dict = {}
            for d in day_all:
                day = datetime.date(*map(int, re.split('[^\d]', d)))
                event_day = event_all_qs.filter(date=day).order_by('time_start')
                event_dict[d] = event_day
            # get time of last update
            event_last_updated = event_all_qs.order_by('-updated_at')[0]
            updated_at = event_last_updated.updated_at.isoformat()
        else:
            event_empty = True
            event_dict = {}
            day_all = []
            updated_at = 'N/A'
        #
        context['event_empty'] = event_empty
        context['event_dict'] = event_dict
        context['day_all'] = day_all
        context['updated_at'] = updated_at
        return context