def doSize(args): glasses = Glass.select() print('{} glasses'.format(len(glasses))) ingredients = Ingredient.select() print('{} ingredients'.format(len(ingredients))) drinks = Drink.select() print('{} drinks'.format(len(drinks)))
def doDelete(args): drinks = Drink.select().where(Drink.source == args.source) print('Found {} drinks'.format(len(drinks))) drinksCount = len(drinks) for drink in drinks: drink.delete_instance() ingredients = Ingredient.select().where(Ingredient.source == args.source) ingredientsCount = 0 print('Found {} ingredients'.format(len(ingredients))) for ingredient in ingredients: if not ingredient.drinks: ingredient.delete_instance() ingredientsCount = ingredientsCount + 1 else: print('Ingredient "{}" is still used by at least one drink'.format( ingredient.name)) glasses = Glass.select().where(Glass.source == args.source) glassesCount = 0 print('Found {} glasses'.format(len(glasses))) for glass in glasses: if not glass.drinks: glass.delete_instance() glassesCount = glassesCount + 1 else: print('Glass "{}" is still used by at least one drink'.format( glass.name())) print('Deleted {} glasses'.format(glassesCount)) print('Deleted {} ingredients'.format(ingredientsCount)) print('Deleted {} drinks'.format(drinksCount))
def doExport(args): glasses = Glass.select().where(Glass.source == args.source) print('Found {} glasses'.format(len(glasses))) ingredients = Ingredient.select().where(Ingredient.source == args.source) print('Found {} ingredients'.format(len(ingredients))) drinks = Drink.select().where(Drink.source == args.source) print('Found {} drinks'.format(len(drinks))) # add all non-source glasses and ingredients used for drink in drinks: if drink.glass.source != args.source: if not [glass for glass in glasses if glass.id == drink.glass_id]: print('Adding glass {}'.format(drink.glass.name())) glasses.append(drink.glass) for di in drink.ingredients: if di.ingredient.source != args.source: if not [ ingredient for ingredient in ingredients if ingredient.id == di.ingredient_id ]: print('Adding ingredient {}'.format(di.ingredient.name)) ingredients.append(di.ingredient) data = {} data['glasses'] = [glass.export(compact=args.compact) for glass in glasses] data['ingredients'] = [ ingredient.export(stats=args.stats, compact=args.compact, alternatives=True) for ingredient in ingredients ] data['drinks'] = [ drink.export(stats=args.stats, compact=args.compact) for drink in drinks ] print('Writing {}...'.format(args.file)) with open(args.file, 'w') as file: file.write(yaml.dump(data, default_flow_style=False)) print('Wrote {} glasses'.format(len(data['glasses']))) print('Wrote {} ingredients'.format(len(data['ingredients']))) print('Wrote {} drinks'.format(len(data['drinks']))) print('Done')
def doSources(args): sources = [] for glass in Glass.select(Glass.source).distinct(): if glass.source not in sources: sources.append(glass.source) for ingredient in Ingredient.select(Ingredient.source).distinct(): if ingredient.source not in sources: sources.append(ingredient.source) for drink in Drink.select(Drink.source).distinct(): if drink.source not in sources: sources.append(drink.source) sources.sort() for source in sources: print(source)
def importDrinks(drinks, glasses, ingredients): count = 0 for drink in drinks: drinkName = 'unknown' try: d = Drink() if 'name' in drink: drinkName = drink['name'].strip() match = namePattern.match(drinkName) if not match: raise ValueError('drink name format not recognized') d.primaryName = match.group(1) if match.group(2): d.secondaryName = match.group(2) elif 'primaryName' in drink: d.primaryName = drink['primaryName'].strip() if 'secondaryName' in drink: d.secondaryName = drink['secondaryName'].strip() drinkName = d.name() else: raise ValueError('drink requires a name') if 'instructions' in drink: d.instructions = drink['instructions'].strip() if 'glass' in drink: match = glassPattern.match(drink['glass'].strip()) if not match: raise ValueError('glass "{}" format not recognized'.format( drink['glass'])) glass = Glass.get_or_none(Glass.type == match.group(3), Glass.size == match.group(1), Glass.units == match.group(2)) if not glass: raise ValueError('glass "{}" not found'.format( drink['glass'])) d.glass_id = glass.id else: glass = [ glass for glass in glasses if glass['id'] == drink['glass_id'] ][0] d.glass_id = glass['local_id'] if args.stats: if 'isFavorite' in drink: d.isFavorite = drink['isFavorite'] if 'timesDispensed' in drink: d.timesDispensed = int(drink['timesDispensed']) d.source = args.source od = d.alreadyExists() if d.alreadyExists(): print('Drink "{}" already exists'.format(drinkName)) continue d.save() ingredients = [] for drinkIngredient in drink['ingredients']: di = DrinkIngredient() di.drink_id = d.id if isinstance(drinkIngredient, str): match = ingredientPattern.match(drinkIngredient) if not match: raise ValueError( 'ingredient format not recognized: {}'.format( drinkIngredient)) di.step = int(match.group(1)) if match.group(2) == '1/4': di.amount = 0.25 elif match.group(2) == '1/3': di.amount = 0.333 elif match.group(2) == '1/2': di.amount = 0.5 elif match.group(2) == '2/3': di.amount = 0.667 elif match.group(2) == '3/4': di.amount = 0.75 else: di.amount = float(match.group(2)) di.units = match.group(3) ingredient = Ingredient.get_or_none( Ingredient.name == match.group(4)) if not ingredient: raise ValueError('ingredient "{}" not found!'.format( match.group(4))) di.ingredient_id = ingredient.id else: if 'ingredient' in drinkIngredient: ingredient = Ingredient.get_or_none( Ingredient.name == drinkIngredient['ingredient']) if not ingredient: raise ValueError( 'ingredient "{}" not found!'.format( drinkIngredient['ingredient'])) di.ingredient_id = ingredient.id elif 'ingredient_id' in drinkIngredient: ingredient = next( iter([ ingredient for ingredient in ingredients if ingredient['id'] == drinkIngredient['ingredient_id'] ]), None) if not ingredient: raise ValueError('ingredient {} not found'.format( drinkIngredient['ingredient_id'])) di.ingredient_id = ingredient['local_id'] else: raise ValueError( 'invalid ingredient {}'.format(drinkIngredient)) if 'amount' not in drinkIngredient: raise ValueError( 'ingredient {} requires an amount'.format( di.ingredient.name)) if drinkIngredient['amount'] == '1/4': di.amount = 0.25 elif drinkIngredient['amount'] == '1/3': di.amount = 0.333 elif drinkIngredient['amount'] == '1/2': di.amount = 0.5 elif drinkIngredient['amount'] == '2/3': di.amount = 0.667 elif drinkIngredient['amount'] == '3/4': di.amount = 0.75 else: di.amount = float(drinkIngredient['amount']) if 'units' not in drinkIngredient: raise ValueError('ingredient {} requires units'.format( di.ingredient.name)) di.units = drinkIngredient['units'] if 'step' in drinkIngredient: di.step = drinkIngredient['step'] else: di.step = 1 ingredients.append(di) d.setIngredients(ingredients) d.save() count = count + 1 except ValueError as e: raise DBAdminError('Drink "{}": {}'.format(drinkName, str(e))) except ModelError as e: raise DBAdminError('Drink "{}": {}'.format(drinkName, str(e))) return count
def importGlasses(glasses): count = 0 for glass in glasses: try: g = Glass() g.type = glass['type'] g.size = glass['size'] g.units = glass['units'] if 'description' in glass: g.description = glass['description'].strip() g.source = args.source og = g.alreadyExists() if og: g = og print('Glass "{}" already exists'.format(g.name())) else: g.save() count = count + 1 glass['local_id'] = g.id except ModelError as e: raise DBAdminError('Glass "{}": {}'.format(glass['type'], str(e))) return count
def deleteAll(): DrinkIngredient.delete().execute() Drink.delete().execute() Ingredient.delete().execute() Glass.delete().execute()