api.py 2.34 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
13
    """
    """
14
    facilities = list()
15

16
17
    # Sort the facilities by alphabetical order ignoring "the" and "a"
    alphalist = sorted(Facility.objects.all(),
18
19
20
                       key=lambda r: re.sub('^(the|a) ', '', r.name,
                                            count=1, flags=re.IGNORECASE))

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