def post(self, request): form = AddRestaurantForm(request.POST, request.FILES) data = dict(form=form) if form.is_valid(): data = form.cleaned_data restaurant = Restaurant() manager = Manager() restaurant.name = data['name'] restaurant.description = data['description'] restaurant.profile_image = request.FILES['profile_image'] manager.email = data['manager_email'] chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" password = "".join([chars[ord(c) % len(chars)] for c in urandom(8)]) manager.set_password(password) manager.is_confirmed = True manager.save() manager.send_email("Login details - Restaurant booking online", "Your login details :\n E-mail : " + manager.email + "\n Password : "******"\n You can login now " + get_current_site(request).domain + "\n Restaurant booking online") restaurant.manager = manager restaurant.category = data['category'] restaurant.save() return render(request, self.template_name, dict(form=form))
def seed(self): restaurant_names = [ 'El Celler de Can Roca', 'Osteria Francescana', 'Noma', 'Central', 'Eleven Madison Park', 'Mugaritz', 'Dinner by Heston Blumenthal', 'Narisawa', 'D.O.M.', 'Gaggan', ] description = """Lorem ipsum dolor sit amet, et mea legere blandit abhorreant, nam exerci accusam elaboraret no. Mea te minimum sensibus. Cu qui commodo omnesque percipit. Per munere nullam temporibus ea. Nec mentitum antiopam no. Ad duis doming indoctum his. Eos ex fabulas singulis, natum labores periculis mea ex. Quo libris apeirian eu. Per solet aperiri ea. Eligendi hendrerit nam id, eos eu alienum antiopam intellegebat, eam viderer denique cu. Homero equidem eu pro. Mel nostro constituam ad, ius ut modus cetero verear. Ad nam eros omnis, mea ei offendit molestiae. Vix adhuc possit inciderint ad, forensibus posidonium sed in, in mei decore vivendo volumus. No per labore nemore. Eos ei molestie percipit maiestatis, oratio audire molestiae ne est, dolore assentior prodesset sed ei. Ea tibique fastidii quo, sit accusata reformidans ei. Elitr primis an quo, quem contentiones eu pro. Eius cotidieque reformidans ex est, rebum expetenda has at. Ius nulla inermis disputando an, idque meliore sit et. Agam reque pericula ne mea, pro postea graeco debitis ne, mei habemus gubergren cotidieque id. Timeam eleifend cu sed, vero labitur per in. Vim ei laoreet minimum officiis, pri alterum gloriatur eu. Id erat debitis comprehensam vix, vix ea dicit dissentiet, cu vix ipsum luptatum. Ad eros ridens malorum eam.""".strip() for i in range(len(restaurant_names)): restaurant_name = restaurant_names[i] restaurant = Restaurant() restaurant.name = restaurant_name restaurant.description = description restaurant.manager = Manager.objects.filter(email="*****@*****.**" % str(i)).first() restaurant.category = random.choice(RestaurantCategory.objects.all()) menu = Menu() menu.save() restaurant.menu = menu restaurant.save()
def get_restaurants_and_food_data(): r = requests.get('https://api.food.jumia.com.gh/api/v5/vendors', headers={"X-FP-API-KEY": "HTML5"}) restaurants = r.json()["data"]["items"] for restaurant in restaurants: if restaurant['name'] == 'Hellofood Test Restaurant 2' or restaurant['name'] == 'Hellofood Test Restaurant 2': print('test restaurants avoid') else: existing_restaurant = get_object_or_none(Restaurant, name=restaurant['name']) if not existing_restaurant: rest_obj = Restaurant() rest_obj.name = restaurant['name'] rest_obj.restaurant_crawl_link = restaurant['web_path'] rest_obj.description = restaurant['description'] rest_obj.city = restaurant['city']['name'] rest_obj.address = '{} \n {}'.format(restaurant['address'], restaurant['address_line2'] if restaurant['address_line2'] else '') rest_obj.latitude = restaurant['latitude'] rest_obj.longitude = restaurant['longitude'] logo_path = restaurant['logo'].split('%s/') if len(logo_path) == 3: img = download_image(logo_path[2]) try: filename = '{}.{}'.format(restaurant['name'].replace(' ', '_'), img.format) rest_obj.logo = filename tempfile = img tempfile_io = BytesIO() # Will make a file-like object in memory that you can then save tempfile.save(tempfile_io, format=img.format) rest_obj.logo.save(filename, ContentFile(tempfile_io.getvalue()), save=False) # Set save=False otherwise you will have a looping save method except: print("Error trying to save model: saving image failed: ") pass if restaurant['cms_content']['vendor_wide_logo']: try: img = download_image(restaurant['cms_content']['vendor_wide_logo']) filename = '{}_cover.{}'.format(restaurant['name'].replace(' ', '_'), img.format) rest_obj.cover_photo = filename tempfile = img tempfile_io = BytesIO() # Will make a file-like object in memory that you can then save tempfile.save(tempfile_io, format=img.format) rest_obj.cover_photo.save(filename, ContentFile(tempfile_io.getvalue()), save=False) # Set save=False otherwise you will have a looping save method except: print("Error trying to save model: saving image failed: ") pass rest_obj.save() food_path = requests.get(restaurant['web_path']) soup = BeautifulSoup(food_path.text, 'html.parser') food_items = soup.find_all('article', class_='product-variation') food_name_parent = None for food_item in food_items: the_food_name = None food_name = food_item.find('span', class_='mrs') right_text_food = food_item.find('div', class_='has-text-right') if food_name and right_text_food: right_text_food_select = right_text_food.find('span', class_='has-ellipsis') food_name_parent = food_name.get_text() the_food_name = '{} {}'.format(food_name.get_text(), right_text_food_select.get_text()) elif right_text_food and not food_name: right_text_food_select = right_text_food.find('span', class_='has-ellipsis') the_food_name = '{} {}'.format(food_name_parent, right_text_food_select.get_text()) elif food_name: food_name = food_name.get_text() food_name_parent = food_name the_food_name = food_name food_description = food_item.find('p', class_='dsc').get_text() if food_item.find('p', class_='dsc') else '' food_price = food_item.find('span', class_='mlxs').get_text() existing_food = get_object_or_none(Food, name=the_food_name) if not existing_food: food_obj = Food() food_obj.name = the_food_name.strip() food_obj.description = food_description.strip() food_obj.price = food_price food_obj.restaurant = rest_obj if not existing_restaurant else existing_restaurant food_obj.save()