From 94810ebf8b3782a3f53cba3db7d4d33fc413b978 Mon Sep 17 00:00:00 2001 From: Chris Giacofei Date: Fri, 7 Jun 2024 13:41:28 -0400 Subject: [PATCH 1/3] Activate virtual environment --- run.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/run.sh b/run.sh index a24d170..929bee6 100644 --- a/run.sh +++ b/run.sh @@ -1,3 +1,4 @@ #! /usr/bin/env bash +source .env/bin/activate python manage.py runserver 0.0.0.0:9595 -- 2.45.2 From 51f37642ebf7f33930b5c15f9633af1c6c6311c7 Mon Sep 17 00:00:00 2001 From: Chris Giacofei Date: Fri, 7 Jun 2024 13:51:15 -0400 Subject: [PATCH 2/3] Needed to fix more reference to batch. Everything seems to work now. --- run.sh | 0 ...{batch_list.html => propogation_list.html} | 0 yeast/views.py | 21 +++++-------------- 3 files changed, 5 insertions(+), 16 deletions(-) mode change 100644 => 100755 run.sh rename yeast/templates/yeast/{batch_list.html => propogation_list.html} (100%) diff --git a/run.sh b/run.sh old mode 100644 new mode 100755 diff --git a/yeast/templates/yeast/batch_list.html b/yeast/templates/yeast/propogation_list.html similarity index 100% rename from yeast/templates/yeast/batch_list.html rename to yeast/templates/yeast/propogation_list.html diff --git a/yeast/views.py b/yeast/views.py index fa9e5a2..ea1d6e0 100644 --- a/yeast/views.py +++ b/yeast/views.py @@ -6,7 +6,7 @@ from django.urls import reverse from django.http import JsonResponse from django.contrib.auth.decorators import login_required -from yeast.models import Yeast, Propogation, Strain +from yeast.models import Yeast, Propogation, Strain, Storage from config.extras import AveryLabel from yeast.forms import BatchAddForm, StrainAddForm @@ -24,21 +24,10 @@ def sample(request, yeast_id): sample = get_object_or_404(Yeast, pk=yeast_id) return render(request, 'yeast/sample.html', { 'sample': sample, - 'batch': get_object_or_404(Propogation, pk=sample.batch_id), + 'batch': get_object_or_404(Propogation, pk=sample.propogation_id), 'storage': list(Storage.objects.all()), }) -# @login_required -# def get_batches(request): - # strains = {} - # for strain in Strain.objects.all(): - # batches = [x.id for x in Batch.objects.filter(strain=strain)] - # if batches: - # strains[strain.id] = batches - - # return JsonResponse( - # {'data': [strains]}) - def get_batch(request): batch_id = int(request.POST.get('batch')) re_url = reverse('yeast:batch', kwargs={'batch_id': batch_id}) @@ -86,8 +75,8 @@ def batch_labels(request, batch_id): for sample in to_print: labels.append({ 'id': sample.id, - 'title': '{} {}'.format(sample.batch.strain.manufacturer.name, sample.batch.strain.name), - 'data': ['ID: {}'.format(sample.id), 'Date: {}'.format(sample.batch.production_date)], + 'title': '{} {}'.format(sample.propogation.strain.manufacturer.name, sample.propogation.strain.name), + 'data': ['ID: {}'.format(sample.id), 'Date: {}'.format(sample.propogation.production_date)], 'blank': False, 'host': request.get_host(), 'template': 'yeast', @@ -111,4 +100,4 @@ class addStrain(CreateView): def get_success_url(self): id = self.object.id #gets id from created object - return reverse('yeast:batches') \ No newline at end of file + return reverse('yeast:batches') -- 2.45.2 From 736dbe24fa5311bb2b5f64f41c5992ca2e73bb5e Mon Sep 17 00:00:00 2001 From: Chris Giacofei Date: Fri, 7 Jun 2024 17:26:33 -0400 Subject: [PATCH 3/3] Propogation and pitching are working closes #3 --- yeast/forms.py | 71 +++++++++++++++++++++++++++++-- yeast/templates/yeast/sample.html | 51 ++++++++++------------ yeast/urls.py | 3 +- yeast/views.py | 62 ++++++++++++++++++++++----- 4 files changed, 142 insertions(+), 45 deletions(-) diff --git a/yeast/forms.py b/yeast/forms.py index c1c6b20..34636c7 100644 --- a/yeast/forms.py +++ b/yeast/forms.py @@ -1,7 +1,15 @@ from django import forms from django.urls import reverse +from django.utils import timezone from django.http import HttpResponse, HttpResponseRedirect -from .models import Yeast, Propogation, Strain +from .models import Yeast, Propogation, Strain, Storage +from beer.models import Batch + + +class MyModelChoiceField(forms.ModelChoiceField): + def label_from_instance(self, obj): + return obj.name + import logging logger = logging.getLogger('django') @@ -31,7 +39,7 @@ class BatchAddForm(forms.ModelForm): class Meta: # specify model to be used model = Propogation - + # specify fields to be used fields = [ 'production_date', @@ -46,15 +54,70 @@ class BatchAddForm(forms.ModelForm): num_samples = forms.IntegerField() class StrainAddForm(forms.ModelForm): - + # create meta class class Meta: # specify model to be used model = Strain - + # specify fields to be used fields = [ 'name', 'long_name', 'manufacturer', ] + +class PitchIntoBeerForm(forms.Form): + batch = forms.ModelChoiceField(queryset=Batch.objects.all()) + starter = forms.BooleanField(required=False) + sample = forms.ModelChoiceField(queryset=Yeast.available) + + def pitch_sample(self): + sample = self.cleaned_data['sample'] + sample.pitched = True + sample.pitched_batch = self.cleaned_data['batch'] + sample.date_pitched = timezone.now() + sample.save() + +class PropogateSampleForm(forms.Form): + num = forms.IntegerField(min_value=1) + storage = forms.ModelChoiceField(queryset=Storage.objects.all()) + parent = forms.ModelChoiceField(queryset=Yeast.available) + strain = forms.ModelChoiceField(queryset=Strain.objects.all()) + + def create_propogation(self): + samples = self.cleaned_data['num'] + storage = self.cleaned_data['storage'] + parent = self.cleaned_data['parent'] + strain = self.cleaned_data['strain'] + + # send email using the self.cleaned_data dictionary + prop_obj = Propogation( + production_date=timezone.now(), + strain = strain, + source = 'PR', + notes = 'Auto generated from form.' + ) + + prop_obj.save() + prop_obj.parent.add(parent) + + for i in range(samples): + if storage.name == 'Deep Freeze': + cells = 40 + else: + cells = 100 + + yeast = Yeast( + propogation = prop_obj, + generation_num = parent.generation_num + 1, + storage = storage, + pitched = False, + cellcount = cells, + notes = 'Auto generated from form.' + ) + + yeast.save() + + return prop_obj + diff --git a/yeast/templates/yeast/sample.html b/yeast/templates/yeast/sample.html index 0d40cb2..797319a 100644 --- a/yeast/templates/yeast/sample.html +++ b/yeast/templates/yeast/sample.html @@ -3,7 +3,7 @@ {% block title %}Yeast Samples{% endblock %} {% block style %} -.table td.fit, +.table td.fit, .table th.fit { white-space: nowrap; width: 1%; @@ -22,12 +22,12 @@ {% endif %} - Production Date{{ sample.batch.production_date }} + Production Date{{ sample.propogation.production_date }} Storage - {{ sample.storage }} + {{ sample.storage }} {% if sample.pitched %} - (Pitched #{{ sample.pitched_batch.brewfather_num }}: {{ sample.pitched_batch.brewfather_name }}) + (Pitched #{{ sample.pitched_batch.brewfather_num }}: {{ sample.pitched_batch.brewfather_name }}) {% endif %} @@ -64,24 +64,20 @@

-
+ + {% csrf_token %} + + Propogate Yeast Sample
- - -
New samples will be automatically created
+ {{ propogate_form.num }} +
New samples will be automatically created
- - -
How will samples be stored?
+ {{ propogate_form.storage }} +
How will samples be stored?
- +
@@ -89,23 +85,20 @@

-
+ + {% csrf_token %} + Pitch Yeast Sample
- - -
Select batch of beer sample is pitched into.
+ + {{ pitch_form.batch }} +
Select batch of beer sample is pitched into.
- - + {{ pitch_form.starter }} +
- +
diff --git a/yeast/urls.py b/yeast/urls.py index 4ae718e..c208417 100644 --- a/yeast/urls.py +++ b/yeast/urls.py @@ -2,7 +2,7 @@ from django.urls import include, path from django.contrib.auth.decorators import login_required from django.contrib import admin -from yeast.views import YeastListView, BatchListView, home, sample, batch, batch_labels, addBatch, addStrain, get_batch +from yeast.views import YeastListView, BatchListView, home, sample, batch, batch_labels, addBatch, addStrain, get_batch, NewPropogation # app_name = 'yeast' @@ -16,5 +16,6 @@ urlpatterns = [ path('batches/', BatchListView.as_view(), name='batches'), path('batch_lookup/', get_batch, name='get_batch'), path('batch_labels//', login_required(batch_labels), name='labels'), + path('progation/add/', NewPropogation, name='propogate'), path('', home, name='home'), ] diff --git a/yeast/views.py b/yeast/views.py index ea1d6e0..2fd3453 100644 --- a/yeast/views.py +++ b/yeast/views.py @@ -1,14 +1,15 @@ from django.shortcuts import render, get_object_or_404, redirect from django.views.generic import ListView -from django.views.generic.edit import CreateView -from django.http import HttpResponse, HttpRequest +from django.views.generic.edit import CreateView, FormView +from django.views import View +from django.http import HttpResponse, HttpRequest, HttpResponseRedirect from django.urls import reverse from django.http import JsonResponse from django.contrib.auth.decorators import login_required from yeast.models import Yeast, Propogation, Strain, Storage from config.extras import AveryLabel -from yeast.forms import BatchAddForm, StrainAddForm +from yeast.forms import BatchAddForm, StrainAddForm, PropogateSampleForm, PitchIntoBeerForm import logging @@ -21,12 +22,39 @@ class BatchListView(ListView): model = Propogation def sample(request, yeast_id): - sample = get_object_or_404(Yeast, pk=yeast_id) - return render(request, 'yeast/sample.html', { - 'sample': sample, - 'batch': get_object_or_404(Propogation, pk=sample.propogation_id), - 'storage': list(Storage.objects.all()), - }) + if request.method == 'POST': + if 'pitch' in request.POST: + + pitch_form = PitchIntoBeerForm(request.POST) + + if pitch_form.is_valid(): + logger.critical(pitch_form.cleaned_data) + sample = get_object_or_404(Yeast, pk=yeast_id) + pitch_form.pitch_sample() + return HttpResponseRedirect(reverse('yeast:yeast', kwargs={'yeast_id': sample.id})) + + elif 'propogate' in request.POST: + propogate_form = PropogateSampleForm(request.POST) + if propogate_form.is_valid(): + new_prop = propogate_form.create_propogation() + return HttpResponseRedirect(reverse('yeast:batches', kwargs={'propogation_id': new_prop.id})) + else: + + return redirect('yeast:samples', kwargs={'yeast_id':yeast_id}) + else: + propogate_form = PropogateSampleForm(initial={'sample': get_object_or_404(Yeast, pk=yeast_id)}) + pitch_form = PitchIntoBeerForm() + + sample = get_object_or_404(Yeast, pk=yeast_id) + + return render(request, 'yeast/sample.html', { + 'sample': sample, + 'batch': get_object_or_404(Propogation, pk=sample.propogation_id), + 'storage': list(Storage.objects.all()), + 'propogate_form': propogate_form, + 'pitch_form': pitch_form, + }) + def get_batch(request): batch_id = int(request.POST.get('batch')) @@ -46,9 +74,11 @@ def batch(request, batch_id): ``Template`` :template:`yeast/batch.html` """ + batch = get_object_or_404(Propogation, pk=batch_id) return render(request, 'yeast/batch.html', {'batch': batch}) + def batch_labels(request, batch_id): """ Create label PDF for samples in a batch. @@ -75,8 +105,8 @@ def batch_labels(request, batch_id): for sample in to_print: labels.append({ 'id': sample.id, - 'title': '{} {}'.format(sample.propogation.strain.manufacturer.name, sample.propogation.strain.name), - 'data': ['ID: {}'.format(sample.id), 'Date: {}'.format(sample.propogation.production_date)], + 'title': '{} {}'.format(sample.batch.strain.manufacturer.name, sample.batch.strain.name), + 'data': ['ID: {}'.format(sample.id), 'Date: {}'.format(sample.batch.production_date)], 'blank': False, 'host': request.get_host(), 'template': 'yeast', @@ -101,3 +131,13 @@ class addStrain(CreateView): def get_success_url(self): id = self.object.id #gets id from created object return reverse('yeast:batches') + +@login_required +class NewPropogation(FormView): + # ~ template_name = "contact.html" + form_class = PropogateSampleForm + success_url = "/" + + def form_valid(self, form): + form.create_propogation() + return super().form_valid(form) -- 2.45.2