async def crawl(self): search_results = await self.crawler.get_json( self.crawler.SEARCH_URL, params={'q': self.input}) if len(search_results['results']) == 0: raise ImportException( f'No recipes found for title "{self.input}"') self.json = search_results['results'][0] reliability = self.validate(self.json['name']) if reliability < 0.6: # so far, all correct matches have been >= 0.8 # todo: maybe import anyways and add a note raise ImportException( f'Probably not the one we were looking for, found: {self.json["name"]}' ) slug = self.json['slug'] recipe_url = self.crawler.RECIPE_URL + f'/{slug}' q = Recipe.objects.filter(source=recipe_url) if q.exists(): r = q.first() url = r.get_absolute_url() raise ImportException(f'Already imported: {url}') raw = await self.crawler.http_get(recipe_url) self.dom = BeautifulSoup(raw, 'html.parser') r = Recipe() r.title = self.find_title() r.preparationtime = self.find_preparationtime() r.cooktime = self.find_cooktime() r.resttime = self.find_resttime() r.portion_quantity = self.find_portion_quantity() r.portion_unit = self.find_portion_unit() r.nutrition_kcal = self.find_nutrition_kcal() r.nutrition_carbs = self.find_nutrition_carbs() r.nutrition_fat = self.find_nutrition_fat() r.nutrition_protein = self.find_nutrition_protein() r.note = self.find_note() r.author = self.find_author() r.source = self.find_source() r.save() r.tags.add(self.find_tags()) try: self.add_ingredients(r.id) self.add_directions(r.id) await self.add_pictures(r.id) except Exception as e: r.delete() raise e return { 'input': self.input, 'found_recipe': r.title, 'reliability': self.validate(r.title), 'url': r.get_absolute_url() }
from django.contrib.auth import get_user_model from recipes.models import Recipe, Comment, Like, Ingredient, Complaint from django.utils import timezone from datetime import datetime, date, time UserModel = get_user_model() for i in range(100): k = str(i) recipe = Recipe(title = 'title'+k ,text = 'text' + k, preview ='preview' + k) recipe.author = UserModel.objects.get(pk = 1) recipe.save() ingredient = Ingredient (ingredient_name = 'ingredient' + k) ingredient.dish = Recipe.objects.get(pk = i+1) ingredient.save() comment = Comment(creator=)