def import_our_recipes(): cur.execute("SELECT * FROM recipes") bad_recipes = []#54,53,83, 101,108,109,114,115,120,121,122,123,125,126,128,132] max_length = 0 for row in cur.fetchall() : if row[0] not in bad_recipes: rec = Recipe() print row[0] rec.recipe_name = row[2] rec.recipe_name = clean_string(rec.recipe_name) rec.ingredients = row[3].replace("|","\n") rec.ingredients = clean_string(rec.ingredients) rec.directions = row[4].replace("|","\n") rec.directions = clean_string(rec.directions) rec.notes =row[5].replace("|","\n") rec.notes = clean_string(rec.notes) rec.notes = clean_string(rec.notes) rec.user_id = 1 if row[1]: rec.timestamp = row[1] else: rec.timestamp = datetime.strptime('01012013', "%d%m%Y").date() rec.image_path = row[9] rec.rating = row[8] rec.was_cooked = 1 db_session.add(rec) db_session.commit()
def import_from_txt(web_file): return_char = "\r\n" recipes = [] state = ParserState.recipe_name recipe = Recipe() last_line_blank = True for line in web_file.readlines(): line = line.strip().decode('utf-8') if len(line.strip()) == 0: if not last_line_blank: state = ParserState((state.value + 1) % (len(ParserState))) last_line_blank = True else: last_line_blank = False if state is ParserState.recipe_name: if recipe.name is not None: recipe.ingredients = recipe.ingredients.strip() recipe.instructions = recipe.instructions.strip() recipe.save() recipes.append(recipe) recipe = Recipe() recipe.name = line.strip() recipe.ingredients = "" recipe.instructions = "" state = ParserState.recipe_info elif state is ParserState.recipe_info: values = line.strip().split("\t") recipe.category = int(values[0]) recipe.source = values[1] recipe.rating = int(values[2]) recipe.preparation_time = int(values[3]) recipe.cooking_time = int(values[4]) recipe.portion = int(values[5]) elif state is ParserState.ingredients: recipe.ingredients += line.strip() + return_char elif state is ParserState.instructions: recipe.instructions += line.strip() + return_char if state is ParserState.recipe_name and recipe.name is not None: recipe.ingredients = recipe.ingredients.strip() recipe.instructions = recipe.instructions.strip() recipe.save() recipes.append(recipe) return recipes