brewery-website/beer/views.py
2024-06-21 11:33:47 -04:00

72 lines
1.7 KiB
Python

from django.shortcuts import render, get_object_or_404
from .models import UserProfile, Recipe, Batch, Fermentable, Hop
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 = Recipe(
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': Recipe.objects.all(),
}
return render(request, 'beer/home.html', context)
def view_recipe(request, recipe_id):
recipe = get_object_or_404(Recipe, pk=recipe_id)
context = {
'recipe': recipe,
}
return render(request, 'beer/recipe.html', context)
def update_ferm(request, ferm_id):
fermentable = get_object_or_404(Fermentable, pk=ferm_id)
context = {
'data': fermentable,
}
return render(request, 'beer/update_fermentable.html', context)
def update_hop(request, hop_id):
hop = get_object_or_404(Hop, pk=hop_id)
context = {
'data': hop,
}
return render(request, 'beer/update_hop.html', context)