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 @@
@@ -89,23 +85,20 @@
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)