brewery-website/beer/views.py

39 lines
1.2 KiB
Python

from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView
from django.http import HttpResponse
from .models import UserProfile, BatchRecipe, Batch
from .extras import get_batches
import json
from config.extras import AveryLabel
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()
return render(request, 'beer/home.html',{'recipes':BatchRecipe.objects.all()})