from django.shortcuts import render, get_object_or_404 from django.views.generic import ListView from django.views.generic.edit import CreateView from django.http import HttpResponse, HttpRequest from django.urls import reverse from yeast.models import Yeast, Batch, Strain from config.extras import AveryLabel from yeast.forms import BatchAddForm, StrainAddForm import logging logger = logging.getLogger('django') class YeastListView(ListView): model = Yeast class BatchListView(ListView): model = Batch def sample(request, yeast_id): sample = get_object_or_404(Yeast, pk=yeast_id) sample_batch = get_object_or_404(Batch, pk=sample.batch_id) return render(request, 'yeast/sample.html', {'sample': sample, 'batch':sample_batch}) def home(request): return render(request, 'yeast/home.html',{}) def batch(request, batch_id): batch = get_object_or_404(Batch, 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 """ skip_count = request.POST.get("skip_count", "") samples = request.POST.getlist("samples", "") batch = get_object_or_404(Batch, pk=batch_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' labelSheet = 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.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', 'ns': 'yeast' }) labelSheet.render(labels, response, skip_count) return response class addBatch(CreateView): model = Batch form_class = BatchAddForm def get_success_url(self): id = self.object.id #gets id from created object return reverse('yeast:batches', kwargs={'batch_id': id}) class addStrain(CreateView): model = Strain form_class = StrainAddForm def get_success_url(self): id = self.object.id #gets id from created object return reverse('yeast:batches')