Implement a homepage for yeast.

Dynamic batch info display.
This commit is contained in:
Chris GIACOFEI
2024-06-06 10:13:16 -04:00
parent 3b9bf53da9
commit 0981ad63bc
6 changed files with 166 additions and 5 deletions

View File

@ -17,7 +17,7 @@
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
{% block script %}{% endblock %}
<style> <style>
{% block style %}{% endblock %} {% block style %}{% endblock %}
body { body {
@ -115,5 +115,5 @@
<!-- Copyright --> <!-- Copyright -->
</footer> </footer>
</body> </body>
{% block endscript %}{% endblock %}
</html> </html>

View File

@ -0,0 +1,128 @@
{% extends "base.html" %}
{% load mathfilters %}
{% load funcs %}
{% block style %}
input, label {
display:block;
}
{% endblock %}
{% block title %}Yeast{% endblock %}
{% block jumbotron %}Yeast Bank{% endblock %}
{% block jumbotronsub %}{% endblock %}
{% block content %}
<div class="container" id="main">
<h3>Batches</h3>
<form action="{% url 'yeast:get_batch' %}" method="post">
{% csrf_token %}
<div class="container" style="border:1px solid #cecece;">
<div class="container m-2">
<label for="strain-select">Choose a strain:</label>
<select name="strain" id="strain-select">
<option value="">--Please choose an option--</option>
{% for strain in strains %}
{% if strain.batches_available|length > 0 %}
<option value="{{ strain.batches_available|get_value_in_qs:strain }}">{{ strain }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="container m-2">
<div class="row">
<div class="col-md-4">
<label for="batch-select">Choose a batch:</label>
<select name="batch" id="batch-select" size="10" style="min-width:100%;">
{% for batch in batches %}
{% if not batch.consumed %}
<option value="{{ batch.id }}">{{ batch.id }} {{ batch.production_date|date:"Y-m-d" }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="col-md">
{% for batch in batches %}
{% if not batch.consumed %}
<div id="data-{{ batch.id }}" style="display: none;" class="datatable">
<table class="table">
<tbody>
<tr>
{% if batch.source == 'SL' %}
<th>Yeast Source</th><td>{{ batch.get_source_display }} <a href="{{ batch.beer_url }}">#{{ batch.beer_num }} {{ batch.beer_name }}</a></td>
{% elif batch.source == 'ST' %}
<th>Batch Source</th><td>Purchased from Store</td>
{% endif %}
</tr>
<tr>
<th>Generation</th><td>{{ batch.generation }}</td>
</tr>
<tr>
<th>Production Date</th><td>{{ batch.production_date }}, {{ batch.age }} days old</td>
</tr>
<tr>
<th>Samples Remaining</th><td>{{ batch.remaining_samples|length }}</td>
</tr>
<tr>
<th>Sample Viability</th><td>
{% if batch.max_viability == batch.min_viability %}
{{ batch.max_viability }} %
{% else %}
{{ batch.min_viability }} - {{ batch.max_viability }} %
{% endif %}
</td>
</tr>
</tbody>
</table>
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
<div class="container m-2">
<input type="submit" value="Go to Batch Page">
</div>
</div>
</form>
</div> <!-- /container -->
{% endblock %}
{% block endscript %}
<script>
const batch = document.getElementById('batch-select');
const batchOpts = [...batch.children];
const elements = document.getElementsByClassName("datatable");
document.getElementById('strain-select').addEventListener(
'change',
function(e){
for(var i = 0; i < elements.length; i++) {
elements[i].style.display = 'none';
}
batch.innerHTML = batchOpts.filter(
o => e.target.value.includes(o.value)
).map(o => o.outerHTML).join('');
});
document.getElementById('batch-select').addEventListener(
'click',
function(e){
var batch_id = e.target.value;
for(var i = 0; i < elements.length; i++) {
elements[i].style.display = 'none';
}
document.getElementById('data-' + batch_id).style.display = 'block';
});
</script>
{% endblock %}

View File

View File

@ -0,0 +1,14 @@
from django import template
register = template.Library()
import logging
logger = logging.getLogger('django')
@register.filter
def get_value_in_qs(queryset, key):
batch_list = ','.join([str(x.id) for x in queryset if x.strain.id==key.id])
if batch_list:
return batch_list
return ''

View File

@ -2,7 +2,7 @@ from django.urls import include, path
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib import admin from django.contrib import admin
from yeast.views import YeastListView, BatchListView, home, sample, batch, batch_labels, addBatch, addStrain from yeast.views import YeastListView, BatchListView, home, sample, batch, batch_labels, addBatch, addStrain, get_batch
# app_name = 'yeast' # app_name = 'yeast'
@ -14,6 +14,7 @@ urlpatterns = [
path('batches/addstrain/', login_required(addStrain.as_view()), name='addstrain'), path('batches/addstrain/', login_required(addStrain.as_view()), name='addstrain'),
path('batches/<int:batch_id>/', batch, name='batch'), path('batches/<int:batch_id>/', batch, name='batch'),
path('batches/', BatchListView.as_view(), name='batches'), path('batches/', BatchListView.as_view(), name='batches'),
path('batch_lookup/', get_batch, name='get_batch'),
path('batch_labels/<int:batch_id>/', login_required(batch_labels), name='labels'), path('batch_labels/<int:batch_id>/', login_required(batch_labels), name='labels'),
path('', home, name='home'), path('', home, name='home'),
] ]

View File

@ -1,8 +1,10 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import ListView from django.views.generic import ListView
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView
from django.http import HttpResponse, HttpRequest from django.http import HttpResponse, HttpRequest
from django.urls import reverse 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 from yeast.models import Yeast, Batch, Strain
from config.extras import AveryLabel from config.extras import AveryLabel
@ -24,8 +26,24 @@ def sample(request, yeast_id):
sample_batch = get_object_or_404(Batch, pk=sample.batch_id) sample_batch = get_object_or_404(Batch, pk=sample.batch_id)
return render(request, 'yeast/sample.html', {'sample': sample, 'batch':sample_batch}) return render(request, 'yeast/sample.html', {'sample': sample, 'batch':sample_batch})
# @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): def home(request):
return render(request, 'yeast/home.html',{}) return render(request, 'yeast/home.html',{'batches': Batch.objects.all, 'strains': Strain.objects.all})
def batch(request, batch_id): def batch(request, batch_id):
""" """