from django.shortcuts import render, get_object_or_404 from .models import UserProfile, BatchRecipe, Batch from .extras import get_batches import json import logging logger = logging.getLogger('django') def home(request): profile = get_object_or_404(UserProfile, user=request.user) api_user = profile.brewfather_api_user api_key = profile.brewfather_api_key batch_list = get_batches(api_user, api_key) for batch in batch_list: if Batch.objects.filter(brewfather_id=batch['_id']).first() is None: recipe_name = batch['recipe']['name'] recipe_obj = BatchRecipe( name=recipe_name, batch_recipe=True, recipe_json=json.dumps(batch['recipe']) ) recipe_obj.save() batch_obj = Batch( brewfather_id=batch['_id'], brewfather_num=batch['batchNo'], brewfather_name=batch['recipe']['name'], recipe=recipe_obj, ) batch_obj.save() context = { 'recipes': BatchRecipe.objects.all(), } return render(request, 'beer/home.html', context) def view_recipe(request, recipe_id): recipe = get_object_or_404(BatchRecipe, pk=recipe_id) context = { 'recipe': recipe, 'fermentable_weight': sum([x.quantity for x in recipe.fermentables]), 'hop_weight': sum([x.quantity for x in recipe.hops]), } return render(request, 'beer/recipe.html', context)