def __init__(self): #initialize the seeder dictionary with all necessary randomization functions seed = Seeder() gen_dict = {} #load the dishes and cuisines dictionaries as they are used for selecting random food and cuisine words during seeding dish_path = 'dishes.csv' self.dish_dictionary = load_dict.read(dish_path) cuisine_path = 'cuisine.csv' self.cuisine_dictionary = load_dict.read(cuisine_path) #retrieve list of all existing restaurants so that the inserted foods are randomly #associated with a restaurant self.restaurant_ids = [] Restaurants = Restaurant.get_all() for restaurant in Restaurants['Restaurants']: self.restaurant_ids.append(restaurant['_id']) seed.add_randomizer("name", lambda fake: fake.random_element(self.dish_dictionary), gen_dict) seed.add_randomizer("restaurant_id", lambda fake: fake.random_element(self.restaurant_ids), gen_dict) seed.add_randomizer("description", lambda fake: fake.random_element(self.dish_dictionary), gen_dict) #randomly generates a price from 1.00 - 99.99 seed.add_randomizer("price", lambda fake: fake.numerify(text = "!%.##"), gen_dict) #randomly generates a percentage string from 0% off - 99% off seed.add_randomizer("specials", lambda fake: fake.numerify(text = "!#") + "% off", gen_dict) self.seed_dict = gen_dict self.seeder = seed
def handle(self, *args, **options): all_restaurants = Restaurant.get_all()['Restaurants'] for restaurant in all_restaurants: try: geo_location = eval(restaurant['GEO_location']) price = restaurant['pricepoint'] print("geo" + str(geo_location)) if type(geo_location) == dict: continue if not type(geo_location) == tuple: print(f"one of the entries is not a tuple: {geo_location}") geo_location = eval(self.faker.location_on_land()) if not price.lower() in {'low', 'medium', 'high'}: print(f"invalid pricepoint {price}") if not price in {'$', "$$", "$$$"}: raise Exception else: price = { '$': 'Low', "$$": 'Medium', "$$$": "High" }[price] except NameError: traceback.print_exc() geo_location = self.faker.location_on_land() except Exception: print("execution halted") break new_location = { "lat": str(geo_location[0]), "long": str(geo_location[1]), "city": str(geo_location[2]), "state": str(geo_location[3]), "country": str(geo_location[4]) } restaurant['GEO_location'] = new_location restaurant['pricepoint'] = price.title() new_rest = Restaurant(**restaurant) new_rest.clean_fields() new_rest.clean() new_rest.save()