def load_styles(): with open(os.path.join(root, "../management/commands/data/styles.json")) as fin: styles = json.load(fin) i = 0 for kwargs in styles: kwargs = _clean_data(kwargs) # print(str(kwargs)) for grv in ["og_min", "og_max", "fg_min", "fg_max"]: if kwargs.get(grv): kwargs[grv] = BeerGravity(sg=kwargs[grv]) for clr in ["color_min", "color_max"]: if kwargs.get(clr): kwargs[clr] = BeerColor(srm=kwargs[clr]) if not models.Style.objects.filter(name=kwargs["name"]).count(): tags = None if kwargs.get("tags"): tags = [ models.Tag(name=s.strip()) for s in kwargs.get("tags", "").split(",") ] for t in tags: t.save() del kwargs["tags"] style = models.Style(**kwargs) style.save() if tags is not None: style.tags.set(tags) style.save() i += 1 print((f"Successfully loaded {i} styles to DB"))
def get_og(self): if self.og_min is not None and self.og_max is not None: return (self.og_min + self.og_max) / 2.0 elif self.og_min is not None: return self.og_min elif self.og_max is not None: return self.og_max else: return BeerGravity(0.0)
def get_gravity(self): eff = float(self.mash_efficiency) grain_sugars = self.get_grain_sugars().kg * eff other_sugars = self.get_other_sugars().kg * 100.0 grain_gravity = grain_sugars / ( self.get_initial_volume().l - grain_sugars / 145.0 + grain_sugars / 100.0 ) other_gravity = other_sugars / ( self.get_initial_volume().l - other_sugars / 145.0 + other_sugars / 100.0 ) # print(f"{self.name} [{self.get_initial_volume().l}] - Grain gravity: {grain_gravity}, Other gravity: {other_gravity}, {eff}") return BeerGravity(plato=(grain_gravity + other_gravity))
def style(): style_input = { "category_id": "3A", "category": "Czech Lager", "name": "Czech Pale Lager", "og_min": BeerGravity(sg=1.028), "og_max": BeerGravity(sg=1.044), "fg_min": BeerGravity(sg=1.008), "fg_max": BeerGravity(sg=1.014), "ibu_min": 20, "ibu_max": 35, "color_min": BeerColor(srm=3), "color_max": BeerColor(srm=6), "alcohol_min": 3.0, "alcohol_max": 4.1, "ferm_type": "Lager", "desc_aroma": "Light to moderate bready-rich malt combined with light to moderate spicy or herbal hop bouquet, the balance between the malt and hops may vary. Faint hint of caramel is acceptable. Light (but never intrusive) diacetyl and light, fruity hop-derived esters are acceptable, but need not be present. No sulfur.", "desc_appe": "Light gold to deep gold color. Brilliant to very clear, with a long-lasting, creamy white head. ", "desc_flavor": "Medium-low to medium bready-rich malt flavor with a rounded, hoppy finish. Low to medium-high spicy or herbal hop flavor. Bitterness is prominent but never harsh. Flavorful and refreshing. Diacetyl or fruity esters are acceptable at low levels, but need not be present and should never be overbearing.", "desc_mouth": "Medium-light to medium body. Moderate carbonation. ", "desc_overall": "A lighter-bodied, rich, refreshing, hoppy, bitter pale Czech lager having the familiar flavors of the stronger Czech Premium Pale Lager (Pilsner-type) beer but in a lower alcohol, lighter-bodied, and slightly less intense format.", "desc_comment": "The Czech name of the style is svìtlé výèepní pivo. ", "desc_ingre": "Soft water with low sulfate and carbonate content, Saazer-type hops, Czech Pilsner malt, Czech lager yeast. Low ion water provides a distinctively soft, rounded hop profile despite high hopping rates. ", "desc_history": "Josef Groll initially brewed two types of beer in 1842–3, a výèepní and a ležák, with the smaller beer having twice the production, Evan Rail speculates that these were probably 10 °P and 12 °P beers, but that the výèepní could have been weaker. This is the most consumed type of beer in the Czech Republic at present.", "desc_style_comp": "A lighter-bodied, lower-intensity, refreshing, everyday version of Czech Premium Pale Lager.", "commercial_exam": "Bøezòák Svìtlé výèepní pivo, Notch Session Pils, Pivovar Kout na Šumavì Koutská 10°, Únìtické pivo 10°", "active": True } return Style.objects.create(**style_input)
def get_actuall_mash_efficiency(self): try: vol = self.wort_volume.l + self.boil_loss.l grain_sugars = self.recipe.get_grain_sugars().kg * 100.0 other_sugars = self.recipe.get_other_sugars().kg * 100.0 grain_gravity = grain_sugars / ( vol - grain_sugars / 145.0 + grain_sugars / 100.0 ) other_gravity = other_sugars / ( vol - other_sugars / 145.0 + other_sugars / 100.0 ) max_gravity = BeerGravity(plato=(grain_gravity + other_gravity)) return ( (self.initial_gravity.plato * vol) / (max_gravity.plato * self.recipe.get_primary_volume().l) ) * 100.0 except AttributeError: return None
def get_final_gravity(self): return BeerGravity(plato=self.get_gravity().plato * (1 - self.get_max_attenuation()))
import pytest from rest_framework.test import APIClient from model_bakery import baker from model_bakery.generators import random_gen from measurement.measures import Volume, Mass, Temperature from brivo.utils.measures import BeerColor, BeerGravity from brivo.users.models import User, UserProfile, UserBrewery from brivo.brewery.models import Style from brivo.users.tests.factories import UserFactory baker.generators.add('brivo.brewery.fields.BeerColorField', lambda: BeerColor(srm=10)) baker.generators.add('brivo.brewery.fields.BeerGravityField', lambda: BeerGravity(plato=10)) baker.generators.add('brivo.brewery.fields.VolumeField', lambda: Volume(l=10)) baker.generators.add('brivo.brewery.fields.MassField', lambda: Mass(kg=1)) baker.generators.add('brivo.brewery.fields.TemperatureField', lambda: Temperature(c=10)) baker.generators.add('modelcluster.fields.ParentalKey', random_gen.gen_related) ROOT_DIR = Path(__file__).resolve(strict=True).parent @pytest.fixture(autouse=True) def media_storage(settings, tmpdir): settings.MEDIA_ROOT = tmpdir.strpath @pytest.fixture
def handle(self, *args, **options): self.stdout.write("Loading Countries to DB") with open(os.path.join(root, "data/countries.json")) as fin: countries = json.load(fin) i = 0 for kwargs in countries: if not models.Country.objects.filter( code=kwargs["code"]).count(): country = models.Country(**kwargs) country.save() i += 1 else: self.stdout.write( self.style.WARNING( f"Country {kwargs['code']} already in DB")) self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} countries to DB")) self.stdout.write("Loading Fermentable to DB") with open(os.path.join(root, "data/fermentables.json")) as fin: fermentables = json.load(fin) i = 0 for kwargs in fermentables: kwargs = _clean_data(kwargs) # self.stdout.write(str(kwargs)) if kwargs.get("country"): try: kwargs["country"] = models.Country.objects.get( code=kwargs.get("country")) except Exception as err: if kwargs.get("country", ""): raise self.stdout.write( self.style.WARNING( f"Fermentable {kwargs['name']} has no country") ) del kwargs["country"] # if not models.Fermentable.objects.filter(name=kwargs["name"]).count(): for clr in ["color"]: if kwargs.get(clr): kwargs[clr] = BeerColor(srm=kwargs[clr]) fermentable = models.Fermentable(**kwargs) fermentable.save() i += 1 # self.stdout.write(self.style.WARNING(f"Fermentable {kwargs['name']} already in DB")) self.stdout.write( self.style.SUCCESS( f"Successfully loaded {i} fermentables to DB")) with open(os.path.join(root, "data/styles.json")) as fin: styles = json.load(fin) i = 0 for kwargs in styles: kwargs = _clean_data(kwargs) # self.stdout.write(str(kwargs)) for grv in ["og_min", "og_max", "fg_min", "fg_max"]: if kwargs.get(grv): kwargs[grv] = BeerGravity(sg=kwargs[grv]) for clr in ["color_min", "color_max"]: if kwargs.get(clr): kwargs[clr] = BeerColor(srm=kwargs[clr]) if not models.Style.objects.filter( name=kwargs["name"]).count(): tags = None if kwargs.get("tags"): tags = [ models.Tag(name=s.strip()) for s in kwargs.get("tags", "").split(",") ] for t in tags: t.save() del kwargs["tags"] style = models.Style(**kwargs) style.save() if tags is not None: style.tags.set(tags) style.save() i += 1 self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} styles to DB")) with open(os.path.join(root, "data/hops.json")) as fin: hops_substitutes = {} hops = json.load(fin) i = 0 for kwargs in hops: kwargs = _clean_data(kwargs) # self.stdout.write(str(kwargs)) if kwargs.get("country"): try: kwargs["country"] = models.Country.objects.get( name=kwargs.get("country")) except Exception as err: if kwargs.get("country", ""): raise self.stdout.write( self.style.WARNING( f"Hop {kwargs['name']} has no country")) del kwargs["country"] if not models.Hop.objects.filter(name=kwargs["name"]).count(): if kwargs.get("substitute"): substitute = [ s.strip() for s in kwargs.get("substitute", "").split(",") ] del kwargs["substitute"] kwargs["alpha_acids"] = (kwargs["alpha_max"] + kwargs["alpha_min"]) / 2.0 hop = models.Hop(**kwargs) hop.save() if len(substitute) > 0: hops_substitutes[hop.id] = substitute i += 1 for pk, subs in hops_substitutes.items(): hop = models.Hop.objects.get(pk=pk) hop_subs = models.Hop.objects.filter(name__in=subs) if hop_subs.count() != len(subs): not_found = set(subs) - set([h.name for h in hop_subs]) self.stdout.write( f"TODO: Could not find all substitue Hops: {pk}, {subs}, {not_found}" ) hop.substitute.set(hop_subs) # self.stdout.write(self.style.WARNING(f"Hop {kwargs['name']} already in DB")) self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} hops to DB")) with open(os.path.join(root, "data/yeasts.json")) as fin: self.stdout.write(self.style.WARNING(f"Loading {i} yeasts to DB")) # yeasts_styles = {} yeasts = json.load(fin) i = 0 for kwargs in yeasts: kwargs = _clean_data(kwargs) for temp in ["temp_min", "temp_max"]: if kwargs.get(temp): kwargs[temp] = Temperature(celsius=kwargs[temp]) if not models.Yeast.objects.filter( name=kwargs["name"]).count(): # if kwargs.get("styles"): # styles = [s.strip().rstrip("s") for s in kwargs.get("styles", "").split(",")] # del kwargs["styles"] print(kwargs) yeast = models.Yeast(**kwargs) yeast.save() # if len(styles) > 0: # yeasts_styles[yeast.id] = styles i += 1 # for pk, stls in yeasts_styles.items(): # yeast = models.Yeast.objects.get(pk=pk) # yeast_stls = models.Style.objects.filter(name__in=stls) # if yeast_stls.count() != len(stls): # not_found = set(stls) - set([h.name for h in yeast_stls]) # self.stdout.write(f"TODO: Could not find all styles for Yeasts: {pk}, {stls}, {not_found}") # yeast.styles.set(yeast_stls) # self.stdout.write(self.style.WARNING(f"Yeast {kwargs['name']} already in DB")) self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} yeasts to DB")) with open(os.path.join(root, "data/extras.json")) as fin: extras = json.load(fin) i = 0 for kwargs in extras: kwargs = _clean_data(kwargs) if not models.Extra.objects.filter( name=kwargs["name"]).count(): extra = models.Extra(**kwargs) extra.save() i += 1 self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} extras to DB"))