-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.py
210 lines (189 loc) · 7.9 KB
/
seed.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# seed.py
from app import create_app, db
from app.models import FrontendUser, User, Reservation, Restaurant, Category
from datetime import datetime, timedelta
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut, GeocoderServiceError
import time
import random
app = create_app()
# initialize geolocator
geolocator = Nominatim(user_agent="restaurant_reservation_app")
def geocode_address(address, max_retries=3):
for attempt in range(max_retries):
try:
location = geolocator.geocode(address)
if location:
return (location.latitude, location.longitude)
else:
return (None, None)
except (GeocoderTimedOut, GeocoderServiceError) as e:
app.logger.error(f"Geocoding error for address '{address}': {e}")
time.sleep(1) # Wait before retrying
return (None, None)
def seed():
with app.app_context():
# Clear existing data
db.drop_all()
db.create_all()
# ---------------------------
# 1. Create Users
# ---------------------------
users = [
User(email='manager1@example.com', name='Manager One'),
User(email='manager2@example.com', name='Manager Two'),
User(email='manager3@example.com', name='Manager Three'),
# Add more users if needed
]
# Set passwords for users
for user in users:
user.set_password('password123') # Use a default password for all seed users
db.session.add_all(users)
db.session.commit()
# Map user IDs to variables for easy reference
user1, user2, user3 = users
# Create Categories
categories = [
Category(name='Italian'),
Category(name='Chinese'),
Category(name='Mexican'),
Category(name='Indian'),
Category(name='Japanese'),
Category(name='French'),
Category(name='Thai'),
Category(name='American'),
Category(name='Mediterranean'),
Category(name='Vegetarian'),
Category(name='Seafood'),
Category(name='Steakhouse'),
Category(name='Vegan'),
Category(name='BBQ'),
Category(name='Bakery'),
Category(name='Cafe'),
Category(name='Bar'),
Category(name='Dessert'),
Category(name='Fusion'),
Category(name='Buffet')
]
db.session.add_all(categories)
db.session.commit()
# ---------------------------
# 2. Create Restaurants with Updated Locations and Geocoding
# ---------------------------
restaurants = [
{
'name': 'Luigi’s Italian Bistro',
'address': 'Via Madonna del Suffragio, 17, 39011 Lana BZ, Italy',
'phone_number': '0123456789',
'description': 'Authentic Italian cuisine with a modern twist.',
'manager': user1, # Assign user instance directly
'categories': [categories[0], categories[19]] # Italian
},
{
'name': 'Dragon Palace',
'address': 'Via Argentieri, 16, 39100 Bolzano BZ, Italy',
'phone_number': '0123456789',
'description': 'Traditional Chinese dishes served in a cozy environment.',
'manager': user2, # Assign user instance directly
'categories': [categories[1], categories[16]] # Chinese
},
{
'name': 'El Taco Loco',
'address': 'Via del Laghetto, 17, 39042 Bressanone BZ, Italy',
'phone_number': '0123456789',
'description': 'A vibrant spot for the best tacos and margaritas.',
'manager': user3, # Assign user instance directly
'categories': [categories[2], categories[17]] # Mexican
},
# Add more restaurants as needed
]
for r in restaurants:
lat, lon = geocode_address(r['address'])
restaurant = Restaurant(
name=r['name'],
address=r['address'],
phone_number=r['phone_number'],
description=r['description'],
manager=r['manager'], # Use the manager relationship
categories=r['categories'],
latitude=lat,
longitude=lon
)
db.session.add(restaurant)
# Optional: Log if geocoding was unsuccessful
if lat is None or lon is None:
app.logger.warning(f"Geocoding failed for restaurant '{r['name']}' at address '{r['address']}'.")
db.session.commit()
# ---------------------------
# 3. Create FrontendUsers
# ---------------------------
frontend_users = [
FrontendUser(
user_id='user_001',
email='john.doe@example.com', # Nullable for now
password=None, # Nullable for now
name=None # Nullable for now
),
FrontendUser(
user_id='user_002',
email='jane.smith@example.com',
password=None,
name=None
),
FrontendUser(
user_id='user_003',
email='alice.wonder@example.com',
password=None,
name=None
),
# Add more frontend users as needed
]
db.session.add_all(frontend_users)
db.session.commit()
# Create Reservations
# Generate reservations for the first restaurant
for i in range(1, 6):
reservation_datetime = datetime.now() + timedelta(days=random.randint(1, 10), hours=random.randint(0, 23))
timestamp = datetime.now() - timedelta(hours=random.randint(1, 48))
reservation = Reservation(
reservation_datetime=reservation_datetime,
timestamp=timestamp,
person_count=random.randint(1, 6),
restaurant_id=1,
name=f'Customer {i}',
status='pending',
frontend_user_id= frontend_users[0].id # Add the frontend user ID here
)
db.session.add(reservation)
# Generate reservations for the second restaurant
for i in range(6, 11):
reservation_datetime = datetime.now() + timedelta(days=random.randint(1, 10), hours=random.randint(0, 23))
timestamp = datetime.now() - timedelta(hours=random.randint(1, 48))
reservation = Reservation(
reservation_datetime=reservation_datetime,
timestamp=timestamp,
person_count=random.randint(1, 6),
restaurant_id=2,
name=f'Customer {i}',
status='pending',
frontend_user_id= frontend_users[1].id # Add the frontend user ID here
)
db.session.add(reservation)
# Generate reservations for the third restaurant
for i in range(11, 16):
reservation_datetime = datetime.now() + timedelta(days=random.randint(1, 10), hours=random.randint(0, 23))
timestamp = datetime.now() - timedelta(hours=random.randint(1, 48))
reservation = Reservation(
reservation_datetime=reservation_datetime,
timestamp=timestamp,
person_count=random.randint(1, 6),
restaurant_id=3,
name=f'Customer {i}',
status='pending',
frontend_user_id= frontend_users[2].id # Add the frontend user ID here
)
db.session.add(reservation)
db.session.commit()
print("Database seeded successfully!")
if __name__ == '__main__':
seed()