def load_hops():
    csv_file = load_csv('hops.csv')
    header = next(csv_file)

    hops_by_name = {}

    data_rows = []
    for row in csv_file:
        if len(row) == 1:
            continue  # Skip empty lines
        row = map(cast_values, row)
        data = dict(zip(header, row))
        data_rows.append(data)

    # Add/update hops
    for data in data_rows:
        hop_id = Hop.create_id(data['name'])
        try:
            hop = Hop.objects.get(pk=hop_id)
            hop.substitutes.remove()
            hop.aroma_tags.remove()
        except Hop.DoesNotExist:
            hop = Hop()
            pass

        for field in data:
            if field not in ["aromas", "substitutes"]:
                setattr(hop, field, data[field])

        hop.save()
        hops_by_name[hop.name] = hop

    # Update tags & substitutes
    for data in data_rows:
        hop = hops_by_name[data['name']]
        for tag_name in split_list(data['aromas']):
            tag = get_or_create_tag(tag_name)
            hop.aroma_tags.add(tag)

        substitutes = split_list(data['substitutes'])
        for substitute_name in substitutes:
            if substitute_name in hops_by_name:
                substitute = hops_by_name[substitute_name]
                hop.substitutes.add(substitute)
            else:
                print("Substitute hop {} not found".format(substitute_name))
Beispiel #2
0
def category_or_tag(request: HttpRequest, category_id: str) -> HttpResponse:
    categories = Hop.get_categories()
    if category_id in categories:
        return category(request, category_id)

    try:
        tag_obj = Tag.objects.get(pk=category_id)
        return tag(request, tag_obj)
    except Tag.DoesNotExist:
        pass

    raise Http404('Unknown hop category %s.' % category)
Beispiel #3
0
def category(request: HttpRequest, category_id: str) -> HttpResponse:
    categories = Hop.get_categories()
    hops_query = Hop.objects.filter(use=category_id, recipes_count__gt=0)

    hops = hops_query.order_by('name')
    most_popular = hops_query.order_by('-recipes_count')[:5]
    category_name = categories[category_id]

    meta = HopOverviewMeta((category_id, category_name)).get_meta()
    context = {
        'category_name': category_name,
        'hops': hops,
        'most_popular': most_popular,
        'meta': meta
    }

    return render(request, 'hop/category.html', context)
Beispiel #4
0
    def calculate_all_hop_metrics(self, calculator: HopMetricCalculator,
                                  hop: Hop) -> None:
        hop.recipes_count = calculator.calc_recipes_count(hop)

        use_counts = calculator.calc_hop_use_counts(hop)
        for use in use_counts:
            setattr(hop, 'recipes_use_%s_count' % use, use_counts[use])

        for metric in calculator.available_metrics:
            self.stdout.write('Calculate {} for hop {}'.format(
                metric.value, hop.name))
            (min, mean, max) = calculator.calc_metric(hop, metric)
            self.stdout.write(str((min, mean, max)))
            setattr(hop, "recipes_%s_min" % metric.value,
                    None if math.isnan(min) else min)
            setattr(hop, "recipes_%s_mean" % metric.value,
                    None if math.isnan(mean) else mean)
            setattr(hop, "recipes_%s_max" % metric.value,
                    None if math.isnan(max) else max)
Beispiel #5
0
def overview(request: HttpRequest) -> HttpResponse:
    hop_categories = {}
    categories = Hop.get_categories()
    for category in categories:
        most_popular = Hop.objects.filter(
            use=category).order_by('-recipes_count')[:5]
        hop_categories[category] = {
            'id': category,
            'name': categories[category],
            'hops': [],
            'most_popular': most_popular
        }

    hops = Hop.objects.filter(recipes_count__gt=0).order_by('name')
    for hop in hops:
        hop_categories[hop.use]['hops'].append(hop)

    meta = HopOverviewMeta().get_meta()
    context = {'categories': hop_categories.values(), 'meta': meta}

    return render(request, 'hop/overview.html', context)