api.py 2.24 KB
Newer Older
1
2
3
4
5
# Future Imports
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

# Python stdlib Imports
Daniel Bond's avatar
Daniel Bond committed
6
import re
7

8
9
10
# App Imports
from .models import Facility

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