api.py 1.53 KB
Newer Older
1
2
3
4
5
6
from website.models import Restaurant


def export_data():
    restaurants = list()
    for restaurant in Restaurant.objects.all():
7
        restaurant_data = {'name': restaurant.name, 'id': restaurant.id}
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
        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