initial commit.
This commit is contained in:
128
yeast/admin.py
Normal file
128
yeast/admin.py
Normal file
@ -0,0 +1,128 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import reverse
|
||||
from django.utils.html import format_html
|
||||
from yeast.models import Yeast, Strain, Manufacturer, Storage, Batch
|
||||
from yeast.forms import YeastModelForm
|
||||
|
||||
import beer
|
||||
|
||||
from config.extras import BREWFATHER_APP_ROOT
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('django')
|
||||
|
||||
class BatchInline(admin.TabularInline):
|
||||
model = Batch
|
||||
extra = 0
|
||||
|
||||
class SampleInline(admin.TabularInline):
|
||||
model = Yeast
|
||||
extra = 0
|
||||
|
||||
class StrainInline(admin.TabularInline):
|
||||
model = Strain
|
||||
extra = 5
|
||||
|
||||
class ParentInline(admin.TabularInline):
|
||||
verbose_name = 'Parent Samples'
|
||||
model = Batch.parent.through
|
||||
|
||||
class YeastAdmin(admin.ModelAdmin):
|
||||
list_display = [ 'batch', 'url', 'lot_number', 'age', 'storage', 'viability', 'generation_num', 'cellcount', 'pitched', 'date_pitched', 'pitched_batch']
|
||||
list_editable = ['pitched', 'date_pitched', 'pitched_batch', 'lot_number']
|
||||
|
||||
def batch_url(self, obj):
|
||||
if obj.pitched_batch:
|
||||
bf_id = obj.pitched_batch.brewfather_id
|
||||
return format_html("<a href='https://web.brewfather.app/tabs/batches/batch/{batch_id}'>{batch_id}</a>", batch_id=bf_id)
|
||||
|
||||
def url(self, obj):
|
||||
if obj.data_web:
|
||||
return format_html("<a href='{url}'>{url}</a>", url=obj.data_web)
|
||||
|
||||
class StrainAdmin(admin.ModelAdmin):
|
||||
list_display = ['name', 'long_name', 'manufacturer', 'avilable_batches']
|
||||
inlines = [
|
||||
BatchInline,
|
||||
]
|
||||
list_editable = ['long_name', 'manufacturer']
|
||||
|
||||
def avilable_batches(self, obj):
|
||||
related_objs = [x for x in obj.batch_set.all() if not x.consumed]
|
||||
|
||||
urls = []
|
||||
|
||||
for related_obj in related_objs:
|
||||
url_text = '{}: {}'.format(related_obj.get_source_display(), related_obj.production_date.strftime("%Y-%m-%d"))
|
||||
url = reverse('admin:yeast_batch_change', args=[related_obj.id]) # replace 'myapp' with your app name
|
||||
urls.append('<a href="{}">{}</a>'.format(url, url_text))
|
||||
return format_html(', '.join(urls))
|
||||
|
||||
avilable_batches.short_description = 'Available Batches'
|
||||
|
||||
class StorageAdmin(admin.ModelAdmin):
|
||||
list_display = ['name', 'viability_loss', 'viability_interval']
|
||||
inlines = [
|
||||
SampleInline,
|
||||
]
|
||||
|
||||
class ManufacturerAdmin(admin.ModelAdmin):
|
||||
list_display = ['name', 'url']
|
||||
inlines = [
|
||||
StrainInline,
|
||||
]
|
||||
|
||||
def url(self, obj):
|
||||
if obj.website:
|
||||
return format_html("<a href='{url}'>{url}</a>", url=obj.website)
|
||||
|
||||
|
||||
class BatchAdmin(admin.ModelAdmin):
|
||||
list_display = ['strain', 'consumed', 'source', 'parent_samples', 'production_date', 'avilable_samples', 'used_samples']
|
||||
form = YeastModelForm
|
||||
filter_horizontal = ['parent']
|
||||
inlines = [
|
||||
#ParentInline,
|
||||
SampleInline,
|
||||
]
|
||||
|
||||
def save_related(self, request, form, formsets, change):
|
||||
super(BatchAdmin, self).save_related(request, form, formsets, change)
|
||||
if form.instance.source_batch:
|
||||
relate_samples = [x for x in Yeast.objects.all() if x.pitched_batch==form.instance.source_batch]
|
||||
for sample in relate_samples:
|
||||
logger.critical(sample)
|
||||
form.instance.parent.add(sample)
|
||||
|
||||
def parent_samples(self, obj):
|
||||
obj.parent.all()
|
||||
|
||||
def used_samples(self, obj):
|
||||
related_objs = list(set(obj.yeast_set.all()) - set(obj.remaining_samples))
|
||||
|
||||
urls = []
|
||||
|
||||
for related_obj in related_objs:
|
||||
url = reverse('admin:yeast_yeast_change', args=[related_obj.id])
|
||||
urls.append('<a href="{}">{}</a>'.format(url, related_obj))
|
||||
return format_html(', '.join(urls))
|
||||
|
||||
used_samples.short_description = 'Used Samples'
|
||||
|
||||
def avilable_samples(self, obj):
|
||||
related_objs = obj.remaining_samples
|
||||
|
||||
urls = []
|
||||
|
||||
for related_obj in related_objs:
|
||||
url = reverse('admin:yeast_yeast_change', args=[related_obj.id]) # replace 'myapp' with your app name
|
||||
urls.append('<a href="{}">{}</a>'.format(url, related_obj))
|
||||
return format_html(', '.join(urls))
|
||||
|
||||
avilable_samples.short_description = 'Available Samples'
|
||||
|
||||
admin.site.register(Yeast, YeastAdmin)
|
||||
admin.site.register(Strain, StrainAdmin)
|
||||
admin.site.register(Manufacturer, ManufacturerAdmin)
|
||||
admin.site.register(Storage, StorageAdmin)
|
||||
admin.site.register(Batch, BatchAdmin)
|
Reference in New Issue
Block a user