Clean up admin modules with mixins.
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import reverse
|
||||
from django.utils.html import format_html
|
||||
from django.apps import apps
|
||||
|
||||
from beer.models import Batch, Recipe, BatchRecipe
|
||||
from yeast.models import Yeast
|
||||
@ -15,12 +16,15 @@ class SampleInline(admin.TabularInline):
|
||||
model = Yeast
|
||||
extra = 0
|
||||
|
||||
@admin.register(Recipe)
|
||||
class RecipeAdmin(admin.ModelAdmin):
|
||||
list_display = ['name']
|
||||
|
||||
@admin.register(BatchRecipe)
|
||||
class BatchRecipeAdmin(admin.ModelAdmin):
|
||||
list_display = ['name']
|
||||
|
||||
@admin.register(Batch)
|
||||
class BeerBatchAdmin(admin.ModelAdmin):
|
||||
list_display = ['brewfather_id', 'batch_url']
|
||||
inlines = [
|
||||
@ -34,6 +38,10 @@ class BeerBatchAdmin(admin.ModelAdmin):
|
||||
return format_html("<a href='{root}/tabs/batches/batch/{batch_id}'>Brewfather App: {batch_id}</a>", batch_id=bf_id, root=BREWFATHER_APP_ROOT)
|
||||
|
||||
|
||||
admin.site.register(Batch, BeerBatchAdmin)
|
||||
admin.site.register(Recipe, RecipeAdmin)
|
||||
admin.site.register(BatchRecipe, BatchRecipeAdmin)
|
||||
app = apps.get_app_config('beer')
|
||||
for model_name, model in app.models.items():
|
||||
|
||||
try:
|
||||
admin.site.register(model)
|
||||
except admin.exceptions.AlreadyRegistered:
|
||||
logger.critical(model)
|
||||
|
@ -1,29 +1,73 @@
|
||||
from django.contrib import admin
|
||||
from django.utils.html import format_html
|
||||
|
||||
from equipment.models import Equipment, KegType, State
|
||||
from django.apps import apps
|
||||
from django import forms
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('django')
|
||||
|
||||
from equipment.models import Equipment, KegType, State, EquipmentMaintenanceLine, EquipmentMaintenance
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('django')
|
||||
|
||||
|
||||
class AdminCreateFormMixin:
|
||||
"""
|
||||
Mixin to easily use a different form for the create case (in comparison to "edit") in the django admin
|
||||
Logic copied from `django.contrib.auth.admin.UserAdmin`
|
||||
"""
|
||||
add_form = None
|
||||
|
||||
def get_form(self, request, obj=None, **kwargs):
|
||||
defaults = {}
|
||||
if obj is None:
|
||||
defaults['form'] = self.add_form
|
||||
defaults.update(kwargs)
|
||||
return super().get_form(request, obj, **defaults)
|
||||
|
||||
class MainenanceLineInline(admin.TabularInline):
|
||||
model = EquipmentMaintenanceLine
|
||||
extra = 1
|
||||
|
||||
class EquipmentInline(admin.TabularInline):
|
||||
model = Equipment
|
||||
extra = 4
|
||||
|
||||
class EquipmentAdmin(admin.ModelAdmin):
|
||||
readonly_fields = ('id',)
|
||||
list_display = ['id', 'state', 'equipment_type', 'keg_type']
|
||||
list_editable = ['state', 'equipment_type', 'keg_type']
|
||||
@admin.register(EquipmentMaintenance)
|
||||
class MaintenanceAdmin(admin.ModelAdmin):
|
||||
#list_display_links = ['equipment',]
|
||||
list_display = ['created_date','equipment', 'notes',]
|
||||
inlines = [
|
||||
MainenanceLineInline,
|
||||
]
|
||||
|
||||
@admin.register(Equipment)
|
||||
class EquipmentAdmin(admin.ModelAdmin):
|
||||
|
||||
readonly_fields = ('id',)
|
||||
list_display = ['id', 'state', 'equipment_type', 'keg_type', 'state']
|
||||
list_editable = ['state', 'equipment_type', 'keg_type', 'state']
|
||||
|
||||
@admin.register(KegType)
|
||||
class KegTypeAdmin(admin.ModelAdmin):
|
||||
list_display = ['name','manufacturer','size_gal',]
|
||||
inlines = [
|
||||
EquipmentInline,
|
||||
]
|
||||
|
||||
# class EquipmentAdmin(admin.ModelAdmin):
|
||||
# list_display = ['name','equipment_type','keg_type','state']
|
||||
|
||||
@admin.register(State)
|
||||
class StateAdmin(admin.ModelAdmin):
|
||||
list_display = ['name']
|
||||
|
||||
admin.site.register(Equipment, EquipmentAdmin)
|
||||
admin.site.register(KegType, KegTypeAdmin)
|
||||
admin.site.register(State, StateAdmin)
|
||||
|
||||
app = apps.get_app_config('equipment')
|
||||
for model_name, model in app.models.items():
|
||||
|
||||
try:
|
||||
admin.site.register(model)
|
||||
except admin.exceptions.AlreadyRegistered:
|
||||
logger.critical(model)
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Generated by Django 5.0.6 on 2024-06-05 12:12
|
||||
# Generated by Django 5.0.6 on 2024-06-05 13:35
|
||||
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
@ -55,6 +55,7 @@ class Migration(migrations.Migration):
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
|
||||
('name', models.CharField(max_length=100)),
|
||||
('EquipmentType', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='equipment.equipmenttype')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
@ -85,18 +86,6 @@ class Migration(migrations.Migration):
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='EquipmentMaintenance',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
|
||||
('equipment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='equipment.equipment')),
|
||||
('maintenance_type', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='equipment.maintenancetype')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='EquipmentTransactions',
|
||||
fields=[
|
||||
@ -110,4 +99,29 @@ class Migration(migrations.Migration):
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='EquipmentMaintenance',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
|
||||
('equipment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='equipment.equipment')),
|
||||
('notes', models.TextField(blank=True, max_length=500, null=True)),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='EquipmentMaintenanceLine',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
|
||||
('notes', models.TextField(blank=True, max_length=500, null=True)),
|
||||
('maintenance', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='equipment.equipmentmaintenance')),
|
||||
('maintenance_type', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='equipment.maintenancetype')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
|
@ -12,20 +12,24 @@ class CustomModel(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
class State(CustomModel):
|
||||
class EquipmentType(CustomModel):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class EquipmentType(CustomModel):
|
||||
class State(CustomModel):
|
||||
name = models.CharField(max_length=100)
|
||||
EquipmentType = models.ForeignKey(EquipmentType, on_delete=models.PROTECT, null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class KegType(CustomModel):
|
||||
name = models.CharField(max_length=100)
|
||||
manufacturer = models.CharField(max_length=100, blank=True)
|
||||
size_gal = models.DecimalField(max_digits=6, decimal_places=2)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@ -35,15 +39,32 @@ class Equipment(CustomModel):
|
||||
keg_type = models.ForeignKey(KegType, on_delete=models.PROTECT)
|
||||
state = models.ForeignKey(State, on_delete=models.PROTECT)
|
||||
|
||||
def __str__(self):
|
||||
return '{} {}'.format(self.name, self.id)
|
||||
|
||||
|
||||
class TransactionType(CustomModel):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class MaintenanceType(CustomModel):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class EquipmentMaintenance(CustomModel):
|
||||
equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE)
|
||||
notes = models.TextField(max_length=500, blank=True, null=True)
|
||||
|
||||
class EquipmentMaintenanceLine(CustomModel):
|
||||
maintenance = models.ForeignKey(EquipmentMaintenance, on_delete=models.CASCADE)
|
||||
maintenance_type = models.ForeignKey(MaintenanceType, on_delete=models.PROTECT)
|
||||
notes = models.TextField(max_length=500, blank=True, null=True)
|
||||
|
||||
class EquipmentTransactions(CustomModel):
|
||||
equipment = models.ForeignKey(Equipment, on_delete=models.CASCADE)
|
||||
|
@ -1,6 +1,7 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import reverse
|
||||
from django.utils.html import format_html
|
||||
from django.apps import apps
|
||||
from yeast.models import Yeast, Strain, Manufacturer, Storage, Batch
|
||||
from yeast.forms import YeastModelForm
|
||||
|
||||
@ -26,7 +27,8 @@ class StrainInline(admin.TabularInline):
|
||||
class ParentInline(admin.TabularInline):
|
||||
verbose_name = 'Parent Samples'
|
||||
model = Batch.parent.through
|
||||
|
||||
|
||||
@admin.register(Yeast)
|
||||
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']
|
||||
@ -35,11 +37,12 @@ class YeastAdmin(admin.ModelAdmin):
|
||||
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)
|
||||
|
||||
@admin.register(Strain)
|
||||
class StrainAdmin(admin.ModelAdmin):
|
||||
list_display = ['name', 'long_name', 'manufacturer', 'avilable_batches']
|
||||
inlines = [
|
||||
@ -60,12 +63,14 @@ class StrainAdmin(admin.ModelAdmin):
|
||||
|
||||
avilable_batches.short_description = 'Available Batches'
|
||||
|
||||
@admin.register(Storage)
|
||||
class StorageAdmin(admin.ModelAdmin):
|
||||
list_display = ['name', 'viability_loss', 'viability_interval']
|
||||
inlines = [
|
||||
SampleInline,
|
||||
]
|
||||
|
||||
@admin.register(Manufacturer)
|
||||
class ManufacturerAdmin(admin.ModelAdmin):
|
||||
list_display = ['name', 'url']
|
||||
inlines = [
|
||||
@ -76,7 +81,7 @@ class ManufacturerAdmin(admin.ModelAdmin):
|
||||
if obj.website:
|
||||
return format_html("<a href='{url}'>{url}</a>", url=obj.website)
|
||||
|
||||
|
||||
@admin.register(Batch)
|
||||
class BatchAdmin(admin.ModelAdmin):
|
||||
list_display = ['strain', 'consumed', 'source', 'parent_samples', 'production_date', 'avilable_samples', 'used_samples']
|
||||
form = YeastModelForm
|
||||
@ -108,7 +113,7 @@ class BatchAdmin(admin.ModelAdmin):
|
||||
return format_html(', '.join(urls))
|
||||
|
||||
used_samples.short_description = 'Used Samples'
|
||||
|
||||
|
||||
def avilable_samples(self, obj):
|
||||
related_objs = obj.remaining_samples
|
||||
|
||||
@ -121,8 +126,11 @@ class BatchAdmin(admin.ModelAdmin):
|
||||
|
||||
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)
|
||||
|
||||
app = apps.get_app_config('yeast')
|
||||
for model_name, model in app.models.items():
|
||||
|
||||
try:
|
||||
admin.site.register(model)
|
||||
except admin.exceptions.AlreadyRegistered:
|
||||
logger.critical(model)
|
||||
|
Reference in New Issue
Block a user