api.py 2.08 KB
Newer Older
1
from website.models import Facility
Daniel Bond's avatar
Daniel Bond committed
2
import re
3
4

def export_data():
5
    facilities = list()
Daniel Bond's avatar
Daniel Bond committed
6
    
7
8
    # Sort the facilities by alphabetical order ignoring "the" and "a"
    alphalist = sorted(Facility.objects.all(),
Daniel Bond's avatar
Daniel Bond committed
9
10
11
            key=lambda r: re.sub('^(the|a) ', '', r.name, count=1,
            flags=re.IGNORECASE))
    
12
13
14
15
16
    for facility in alphalist:
        facility_data = {
            'name': facility.name,
            'location': facility.location, 
            'id': facility.id
17
        }
18
        open_times = list()
Renfred Harper's avatar
Renfred Harper committed
19
        # Sort open times by their start day and time
20
        sorted_times = sorted(facility.main_schedule.open_times.all(),
Renfred Harper's avatar
Renfred Harper committed
21
22
                key=lambda t: (t.start_day, t.start_time, t.end_time))
        for time in sorted_times:
23
24
25
26
27
28
            open_times.append({
                    'start_day': time.start_day,
                    'start_time': time.start_time.isoformat(),
                    'end_day': time.end_day,
                    'end_time': time.end_time.isoformat()
            })
29
30
        facility_data['main_schedule'] = {
                'name': facility.main_schedule.name,
31
32
33
                'open_times': open_times
        }
        special_schedules = list()
34
        for schedule in facility.special_schedules.all():
35
            open_times = list()
Renfred Harper's avatar
Renfred Harper committed
36
37
38
            sorted_times = sorted(schedule.open_times.all(),
                key=lambda t: (t.start_day, t.start_time, t.end_time))
            for time in sorted_times:
39
40
41
42
43
44
45
46
47
48
49
50
                open_times.append({
                        'start_day': time.start_day,
                        'start_time': time.start_time.isoformat(),
                        'end_day': time.end_day,
                        'end_time': time.end_time.isoformat()
                })
            special_schedules.append({
                    'name': schedule.name,
                    'start': schedule.valid_start.isoformat(),
                    'end': schedule.valid_end.isoformat(),
                    'open_times': open_times
            })
51
52
53
        facility_data['special_schedules'] = special_schedules
        facilities.append(facility_data)
    return facilities