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.urls import reverse from django.http import JsonResponse from django.contrib.auth.decorators import login_required from yeast.models import Yeast, Batch, Strain, Storage 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) return render(request, 'yeast/sample.html', { 'sample': sample, 'batch': get_object_or_404(Batch, pk=sample.batch_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}) return redirect(re_url) def home(request): return render(request, 'yeast/home.html',{'batches': Batch.objects.all, 'strains': Strain.objects.all}) def batch(request, batch_id): """ Display a batch of yeast samples. ``Batch`` An instance of :model:`yeast.Batch`. ``Template`` :template:`yeast/batch.html` """ 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. **Context** ``Batch`` An instance of :model:`yeast.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')