129 lines
3.3 KiB
Python
129 lines
3.3 KiB
Python
import logging
|
|
from django import forms
|
|
from django.urls import reverse
|
|
from django.utils import timezone
|
|
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
|
|
|
|
|
|
logger = logging.getLogger('django')
|
|
|
|
|
|
class DateInput(forms.DateInput):
|
|
input_type = 'date'
|
|
|
|
|
|
class YeastModelForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Yeast
|
|
fields = '__all__'
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
forms.ModelForm.__init__(self, *args, **kwargs)
|
|
|
|
self.fields['parent'].queryset = Yeast.objects.all()
|
|
|
|
|
|
# creating a form
|
|
class BatchAddForm(forms.ModelForm):
|
|
def __init__(self, *args, **kwargs):
|
|
super(BatchAddForm, self).__init__(*args, **kwargs)
|
|
href_string = '<a href="{}">Add a new strain</a>'
|
|
self.fields['strain'].help_text = href_string.format(
|
|
reverse('yeast:addstrain'))
|
|
|
|
# create meta class
|
|
class Meta:
|
|
# specify model to be used
|
|
model = Propogation
|
|
|
|
# specify fields to be used
|
|
fields = [
|
|
'production_date',
|
|
'strain',
|
|
'source',
|
|
]
|
|
|
|
widgets = {
|
|
'production_date': DateInput(),
|
|
}
|
|
|
|
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
|