Пример #1
0
def consolidate_dish(dish, duplicates, create_alias=True):
    """
    Remove duplicate dishes, add aliases to the dish we're consolidating on.
    """
    for duplicate_dish in duplicates:
        alias = None
        if create_alias:
            alias = add_alias(dish, duplicate_dish.name)

        for meal in duplicate_dish.meal_set.all():
            # delete duplicate dish
            MealDish.objects.filter(
                dish=duplicate_dish,
                meal=meal,
            ).delete()

            # add consolidated dish, with alias
            mealdish = MealDish(
                alias=alias,
                dish=dish,
                meal=meal,
            )
            mealdish.save()

        # NB: assumes we can safely delete DishIngredients on duplicate
        duplicate_dish.delete()
Пример #2
0
    def save(self, **kwargs):
        dishes = self.cleaned_data.pop('dishes')
        meal = super(MealAdminForm, self).save(commit=False)

        meal.dishes.clear()
        for dish in dishes:
            # NB: Any aliases will be lost. Assumed this will be edited 
            # infrequently enough for this to be okay.
            meal_dish = MealDish(
                dish=dish,
                meal=meal,
            )
            meal_dish.save()

        meal.save()
        return meal
Пример #3
0
def load_menu(filename, school_type):
    menu = open(filename, 'r').readlines()

    month = None
    year = None
    current_day = None
    current_meal = None

    for line, next_line in _pairwise(menu):
        line = _prepare_string(line)
        next_line = _prepare_string(next_line)

        if not line:
            continue

        # <month> <year>
        date_pattern_match = date_pattern.match(line)
        if date_pattern_match:
            menu_date = datetime.strptime(line, '%B %Y')
            print menu_date
            month = menu_date.month
            year = menu_date.year
            continue

        # day of the month
        if day_of_month_pattern.match(line):
            current_day = date(year, month, int(line))
            current_meal = Meal(date=current_day, school_type=school_type)
            current_meal.save()
            print current_day
            continue

        # meal
        if meal_name_pattern.match(line):
            # TODO could combine meal name lines
            print 'meal name', line
            continue

        # dish
        dish_name_match = dish_name_pattern.match(line)
        if dish_name_match:
            dish_name = dish_name_match.group(1)

            # should have already joined this line with the previous, ignore it
            if any([line.startswith(joiner) for joiner in JOINERS]):
                continue

            # try to match with next line
            if any([next_line.startswith(joiner) for joiner in JOINERS]):
                dish_name = ' '.join((dish_name, next_line,))
            dish_name = dish_name.lower()

            alias = None
            try:
                # attempt to find dish, including possible aliases
                dish = Dish.objects.with_alias(dish_name).get(school_type=school_type)

                # get the alias used, if any, to refer to later
                try:
                    alias = dish.aliases.get(text=dish_name)
                except Exception:
                    pass
            except Exception:
                # add the dish
                dish = Dish(name=dish_name, school_type=school_type)
                dish.save()

            mealdish = MealDish(
                dish=dish,
                meal=current_meal,
                alias=alias,
            )
            mealdish.save()

            current_meal.save()
            print 'dish name (%s as %s)' % (dish_name, dish.name)