api.py 2.28 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
    for facility in alphalist:
        facility_data = {
            'name': facility.name,
22
23
            'location': facility.location,
            'address': facility.address,
24
            'id': facility.id
25
        }
26
        open_times = list()
Renfred Harper's avatar
Renfred Harper committed
27
        # Sort open times by their start day and time
28
        sorted_times = sorted(facility.main_schedule.open_times.all(),
Renfred Harper's avatar
Renfred Harper committed
29
30
                key=lambda t: (t.start_day, t.start_time, t.end_time))
        for time in sorted_times:
31
32
33
34
35
36
            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()
            })
37
38
        facility_data['main_schedule'] = {
                'name': facility.main_schedule.name,
39
40
41
                'open_times': open_times
        }
        special_schedules = list()
42
        for schedule in facility.special_schedules.all():
43
            open_times = list()
Renfred Harper's avatar
Renfred Harper committed
44
45
46
            sorted_times = sorted(schedule.open_times.all(),
                key=lambda t: (t.start_day, t.start_time, t.end_time))
            for time in sorted_times:
47
48
49
50
51
52
53
54
55
56
57
58
                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
            })
59
60
61
        facility_data['special_schedules'] = special_schedules
        facilities.append(facility_data)
    return facilities