๐Ÿ“ฆ awasthi21 / LoopRestaurantMonitoring

๐Ÿ“„ 0003_populate_restaurant_timings_data.py ยท 68 lines
1
2
3
4
5
6
7
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68# Generated by Django 4.1.7 on 2023-04-04 16:59

from django.db import migrations,transaction
from RestaurantMonitoring.commons import *
from .. import  restaurant_montioring_local_settings as  local
from RestaurantMonitoring.models import *

"""
This function, get_restaurant_timing_data, reads restaurant timing data from a CSV file and 
processes it to return a dictionary containing the unique schedules for each restaurant by store ID 
and day of the week. The dictionary's keys are tuples of the form (store_id, day_of_week), 
and the values are tuples containing start_time_local and end_time_local.


It Loops through the unique store IDs and for each day of the week (0 to 6):
a. If a schedule for the store ID and day of the week is not present in the unique_schedules dictionary, 
the function adds a default schedule of 00:00:00 to 23:59:59.

"""
def get_restaurant_timing_data():
    restaurant_schedule = CsvFileReader(local.RESTAURANT_SCHEDULE_CSV_URL, local.RESTAURANT_SCHEDULE_FILEPATH).get_data()
    unique_schedules = {}
    for row in restaurant_schedule:
        store_id = row["store_id"].strip()
        day_of_week = int(row["day"].strip())
        start_time_local = row["start_time_local"].strip()
        end_time_local = row["end_time_local"].strip()
        unique_schedules[(store_id, day_of_week)] = (start_time_local, end_time_local)
    unique_store_ids = Restaurant.objects.values_list('store_id', flat=True).distinct()
    for store_id in unique_store_ids:
        for i in range(7):
            if (store_id, i) not in unique_schedules:
                unique_schedules[(store_id, i)] = ("00:00:00", "23:59:59")
    return unique_schedules

@transaction.atomic
def populate_restaurant_timings(apps,schema_editor):
    unique_schedules=get_restaurant_timing_data()
    resataurant_timing_objs=[]
    for key in unique_schedules:
        try:
            restaurant = Restaurant.objects.get(store_id=key[0])
        except Restaurant.DoesNotExist:
            restaurant = Restaurant(store_id=key[0])
            restaurant.save()
        resataurant_timing_objs.append(RestaurantTimings(Restaurant=restaurant,day_of_week=key[1],start_time_local=unique_schedules[key][0],end_time_local=unique_schedules[key][1]))
    RestaurantTimings.objects.bulk_create(resataurant_timing_objs)
    
    
    
@transaction.atomic
def reverse_populate_restaurant_timings(apps,schema_editor):
    RestaurantTimings.objects.all().delete()
    
    
    

class Migration(migrations.Migration):

    dependencies = [
        ('RestaurantMonitoring', '0002_populate_restaurant_timezone_data'),
    ]

    operations = [
        migrations.RunPython(populate_restaurant_timings,reverse_code=reverse_populate_restaurant_timings)
    ]