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

def export_data():
    restaurants = list()
Daniel Bond's avatar
Daniel Bond committed
6
7
8
9
10
11
12
    
    # Sort the restaurants by alphabetical order ignoring "the" and "a"
    alphalist = sorted(Restaurant.objects.all(),
            key=lambda r: re.sub('^(the|a) ', '', r.name, count=1,
            flags=re.IGNORECASE))
    
    for restaurant in alphalist:
13
14
15
16
17
        restaurant_data = {
            'name': restaurant.name,
            'location': restaurant.location, 
            'id': restaurant.id
        }
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
        open_times = list()
        for time in restaurant.main_schedule.open_times.all():
            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()
            })
        restaurant_data['main_schedule'] = {
                'name': restaurant.main_schedule.name,
                'open_times': open_times
        }
        special_schedules = list()
        for schedule in restaurant.special_schedules.all():
            open_times = list()
            for time in schedule.open_times.all():
                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
            })
        restaurant_data['special_schedules'] = special_schedules
        restaurants.append(restaurant_data)
    return restaurants