brewery-website/yeast/views.py

163 lines
4.8 KiB
Python

from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import ListView
from django.views.generic.edit import CreateView, FormView
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
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, PropogateSampleForm, \
PitchIntoBeerForm
import logging
logger = logging.getLogger('django')
class YeastListView(ListView):
model = Yeast
class BatchListView(ListView):
model = Propogation
def sample(request, yeast_id):
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:batch', 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):
propogation_id = int(request.POST.get('batch'))
re_url = reverse('yeast:batch', kwargs={'propogation_id': propogation_id})
return redirect(re_url)
def home(request):
context = {
'batches': Propogation.objects.all,
'strains': Strain.objects.all,
}
return render(request, 'yeast/home.html', context)
def batch(request, propogation_id):
"""
Display a batch of yeast samples.
``Propogation``
An instance of :model:`yeast.Propogation`.
``Template``
:template:`yeast/batch.html`
"""
batch = get_object_or_404(Propogation, pk=propogation_id)
return render(request, 'yeast/batch.html', {'batch': batch})
def batch_labels(request, propogation_id):
"""
Create label PDF for samples in a batch.
**Context**
``Batch``
An instance of :model:`yeast.Propogation`.
"""
skip_count = request.POST.get("skip_count", "")
samples = request.POST.getlist("samples", "")
batch = get_object_or_404(Propogation, pk=propogation_id)
to_print = list(filter(lambda d: str(
d.id) in samples, batch.yeast_set.all()))
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=samplelabels.pdf'
label_sheet = AveryLabel(18294, debug=False)
logger.critical(samples)
logger.critical(to_print)
labels = []
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)],
'blank': False,
'host': request.get_host(),
'template': 'yeast',
'ns': 'yeast'
})
label_sheet.render(labels, response, skip_count)
return response
class AddBatch(CreateView):
model = Propogation
form_class = BatchAddForm
def get_success_url(self):
id = self.object.id # gets id from created object
return reverse('yeast:batch', kwargs={'propogation_id': id})
class AddStrain(CreateView):
model = Strain
form_class = StrainAddForm
def get_success_url(self):
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)