示例#1
0
def category(request: HttpRequest, category_id: str) -> HttpResponse:
    categories = Fermentable.get_categories()
    if category_id not in categories:
        raise Http404('Unknown fermentable category %s.' % category_id)

    category_name = categories[category_id]

    fermentables_query = Fermentable.objects.filter(category=category_id,
                                                    recipes_count__gt=0)
    (fermentables, types) = group_by_type(fermentables_query.order_by('name'))

    most_popular = []
    if fermentables_query.count() > 5:
        most_popular = fermentables_query.order_by('-recipes_count')[:5]

    meta = FermentableOverviewMeta((category_id, category_name)).get_meta()
    context = {
        'category_name': category_name,
        'fermentables': fermentables,
        'types': types,
        'most_popular': most_popular,
        'meta': meta,
    }

    return render(request, 'fermentable/category.html', context)
示例#2
0
def group_by_category(fermentables: iter) -> list:
    fermentable_categories = {}
    categories = Fermentable.get_categories()

    # Create category object
    for category in categories:
        fermentable_categories[category] = {
            'id': category,
            'name': categories[category],
            'fermentables': [],
            'most_popular': []
        }

    # Assign fermentables
    for fermentable in fermentables:
        fermentable_categories[fermentable.category]['fermentables'].append(
            fermentable)

    # Filter categories with fermentables
    categories_filtered = []
    for fermentable_category in fermentable_categories.values():
        if len(fermentable_category['fermentables']):
            categories_filtered.append(fermentable_category)

    return categories_filtered
示例#3
0
def group_by_type(fermentables: iter) -> Tuple[list, list]:
    untyped_fermentables = []
    types = Fermentable.get_types()
    fermentable_types = {}

    # Create type objects
    for t in types:
        fermentable_types[t] = {
            'id': t,
            'name': types[t],
            'fermentables': [],
        }

    # Assign fermentables (if possible)
    for fermentable in fermentables:
        if fermentable.type is not None:
            fermentable_types[fermentable.type]['fermentables'].append(
                fermentable)
        else:
            untyped_fermentables.append(fermentable)

    # Filter types with fermentables
    types_filtered = []
    for fermentable_type in fermentable_types.values():
        if len(fermentable_type['fermentables']):
            types_filtered.append(fermentable_type)

    return untyped_fermentables, types_filtered
示例#4
0
def load_fermentables():
    csv_file = load_csv('fermentables.csv')
    header = next(csv_file)

    for row in csv_file:
        if len(row) == 1:
            continue  # Skip empty lines

        fermentable_id = Fermentable.create_id(row[0])
        row = map(cast_values, row)
        data = dict(zip(header, row))

        try:
            fermentable = Fermentable.objects.get(pk=fermentable_id)
        except Fermentable.DoesNotExist:
            fermentable = Fermentable()
            pass

        for field in data:
            setattr(fermentable, field, data[field])
        fermentable.save()
示例#5
0
    def calculate_all_fermentable_metrics(
            self, calculator: FermentableMetricCalculator,
            fermentable: Fermentable) -> None:
        fermentable.recipes_count = calculator.calc_recipes_count(fermentable)

        for metric in calculator.available_metrics:
            self.stdout.write('Calculate {} for fermentable {}'.format(
                metric.value, fermentable.name))
            (min, mean, max) = calculator.calc_metric(fermentable, metric)
            self.stdout.write(str((min, mean, max)))
            setattr(fermentable, "recipes_%s_min" % metric.value,
                    None if math.isnan(min) else min)
            setattr(fermentable, "recipes_%s_mean" % metric.value,
                    None if math.isnan(mean) else mean)
            setattr(fermentable, "recipes_%s_max" % metric.value,
                    None if math.isnan(max) else max)