Implement basic call to brewfather to pull data.
This commit is contained in:
parent
3d4bc5d2f5
commit
b1332dede9
@ -3,7 +3,7 @@ from django.urls import reverse
|
|||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
|
|
||||||
from beer.models import Batch, Recipe, BatchRecipe
|
from beer.models import Batch, BatchRecipe, BatchRecipe
|
||||||
from yeast.models import Yeast
|
from yeast.models import Yeast
|
||||||
|
|
||||||
from config.extras import BREWFATHER_APP_ROOT
|
from config.extras import BREWFATHER_APP_ROOT
|
||||||
@ -12,10 +12,6 @@ class SampleInline(admin.TabularInline):
|
|||||||
model = Yeast
|
model = Yeast
|
||||||
extra = 0
|
extra = 0
|
||||||
|
|
||||||
@admin.register(Recipe)
|
|
||||||
class RecipeAdmin(admin.ModelAdmin):
|
|
||||||
list_display = ['name']
|
|
||||||
|
|
||||||
@admin.register(BatchRecipe)
|
@admin.register(BatchRecipe)
|
||||||
class BatchRecipeAdmin(admin.ModelAdmin):
|
class BatchRecipeAdmin(admin.ModelAdmin):
|
||||||
list_display = ['name']
|
list_display = ['name']
|
||||||
|
81
beer/extras.py
Normal file
81
beer/extras.py
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
import base64
|
||||||
|
import json
|
||||||
|
from urllib.request import Request, urlopen
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger('django')
|
||||||
|
|
||||||
|
RECIPE_URL = 'https://api.brewfather.app/v2/recipes'
|
||||||
|
BATCH_URL = 'https://api.brewfather.app/v2/batches'
|
||||||
|
PULL_LIMIT = 50
|
||||||
|
|
||||||
|
def get_batches(api_user, api_key, batch=''):
|
||||||
|
auth_string = api_user + ':' + api_key
|
||||||
|
|
||||||
|
auth64 = base64.b64encode(auth_string.encode("utf-8"))
|
||||||
|
batch_array = []
|
||||||
|
|
||||||
|
if batch != '':
|
||||||
|
lastbatch = '&start_after=' + batch
|
||||||
|
else:
|
||||||
|
lastbatch = ''
|
||||||
|
|
||||||
|
query = '{batch_url}?limit={pull_limit}&complete=True&include=recipe,recipe.batchSize&status=Planning{last_batch}'.format(
|
||||||
|
batch_url=BATCH_URL,
|
||||||
|
pull_limit=PULL_LIMIT,
|
||||||
|
last_batch=lastbatch
|
||||||
|
)
|
||||||
|
|
||||||
|
req = Request(query)
|
||||||
|
req.add_header('authorization', 'Basic ' + auth64.decode())
|
||||||
|
content = urlopen(req)
|
||||||
|
|
||||||
|
data = json.load(content)
|
||||||
|
|
||||||
|
if len(data) == PULL_LIMIT:
|
||||||
|
last_id = data[-1]['_id']
|
||||||
|
data = data + get_batches(batch=last_id)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def pull_recipes(api_user, api_key):
|
||||||
|
batch_list = get_batches(api_user, api_key);
|
||||||
|
|
||||||
|
for batch in batch_list:
|
||||||
|
|
||||||
|
batch_id = batch['_id']
|
||||||
|
brewDate = batch['brewDate']
|
||||||
|
recipe = batch['recipe']
|
||||||
|
batch_name = '{} {}'.format(batch['name'], batch['batchNo'])
|
||||||
|
name = recipe['name']
|
||||||
|
batchSize = recipe['batchSize'] * 0.264172
|
||||||
|
strike_water = recipe['data']['mashWaterAmount'] * 0.264172
|
||||||
|
sparge_water = recipe['data']['spargeWaterAmount'] * 0.264172
|
||||||
|
fermentables = recipe['data']['mashFermentables']
|
||||||
|
fermentable_text = ''
|
||||||
|
|
||||||
|
for num, ferm in enumerate(fermentables):
|
||||||
|
logger.critical(ferm)
|
||||||
|
# var this_ferm = recipe.data.mashFermentables[key];
|
||||||
|
# console.log(this_ferm);
|
||||||
|
# var malt_string = this_ferm.name + '@@@' + this_ferm.grainCategory + '@@@' + this_ferm.amount * 2.20462 + '@@@ @@@' + this_ferm.color;
|
||||||
|
# fermentable_text = fermentable_text + "%%%" + malt_string;
|
||||||
|
# }
|
||||||
|
|
||||||
|
# /*for (j=0;j<fermentables.length;j++){
|
||||||
|
# console.log(fermentables[j]);
|
||||||
|
# var malt_string = fermentables[j].name + '@@@' + fermentables[j].grainCategory + '@@@' + fermentables[j].amount * 2.20462 + '@@@ @@@' + fermentables[j].color;
|
||||||
|
# fermentable_text = fermentable_text + "%%%" + malt_string;
|
||||||
|
# }*/
|
||||||
|
# row_data.push([name, batch_name, fermentable_text, id, strike_water, sparge_water, batchSize, brewDate]);
|
||||||
|
# }
|
||||||
|
|
||||||
|
# sheet = SpreadsheetApp.getActive().getSheetByName('Recipes');
|
||||||
|
|
||||||
|
# clearrange = sheet.getRange("A2:H");
|
||||||
|
# clearrange.clear();
|
||||||
|
# range = sheet.getRange(1, 1, row_data.length, row_data[0].length);
|
||||||
|
# range.setValues(row_data);
|
||||||
|
# clearrange.sort(8);
|
||||||
|
# }
|
@ -27,7 +27,7 @@ class Batch(CustomModel):
|
|||||||
brewfather_id = models.CharField(max_length=50)
|
brewfather_id = models.CharField(max_length=50)
|
||||||
brewfather_num = models.IntegerField(default=1)
|
brewfather_num = models.IntegerField(default=1)
|
||||||
brewfather_name = models.CharField(max_length=500, default='name')
|
brewfather_name = models.CharField(max_length=500, default='name')
|
||||||
recipe = models.ForeignKey('BatchRecipe', on_delete=models.CASCADE, default=1)
|
recipe = models.OneToOneField('BatchRecipe', on_delete=models.CASCADE, default=1)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def brewfather_url(self):
|
def brewfather_url(self):
|
||||||
@ -40,8 +40,6 @@ class Batch(CustomModel):
|
|||||||
class BatchRecipe(CustomModel):
|
class BatchRecipe(CustomModel):
|
||||||
""" Recipe to be stored with a batch."""
|
""" Recipe to be stored with a batch."""
|
||||||
name = models.CharField(max_length=50)
|
name = models.CharField(max_length=50)
|
||||||
|
batch_recipe = models.BooleanField(null=True)
|
||||||
|
recipe_json = models.TextField(null=True, blank=True)
|
||||||
class Recipe(CustomModel):
|
|
||||||
""" Recipes not attched to batches."""
|
|
||||||
name = models.CharField(max_length=50)
|
|
||||||
|
@ -2,9 +2,37 @@ from django.shortcuts import render, get_object_or_404
|
|||||||
from django.views.generic import ListView
|
from django.views.generic import ListView
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
from .models import Recipe
|
from .models import UserProfile, BatchRecipe, Batch
|
||||||
|
from .extras import get_batches
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
from config.extras import AveryLabel
|
from config.extras import AveryLabel
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger('django')
|
||||||
|
|
||||||
def home(request):
|
def home(request):
|
||||||
return render(request, 'beer/home.html',{'beer':Recipe.objects.all()})
|
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',{'batches':BatchRecipe.objects.all()})
|
||||||
|
Loading…
Reference in New Issue
Block a user