Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BeerPurchaseInline in BeerAdmin, RatingInline in BeerPurchaseAdmin #65

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions backend/beers/admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
from django.contrib import admin
from import_export.admin import ImportExportActionModelAdmin

from purchases.models import BeerPurchase
from .models import Beer, BeerStyle, Brewery, Hop


class BeerPurchaseInline(admin.StackedInline):
model = BeerPurchase
extra = 0
fk_name = 'beer'
fields = (
'sold_to',
'packaging',
'price',
'volume_ml',
'purchased_at',
'image'
)

def has_view_or_change_permission(self, request, obj=None):
return False


class BeerAdmin(ImportExportActionModelAdmin):
list_display = ('id', 'name', 'brewery', 'style', 'percentage')
list_select_related = ('brewery', 'style')
search_fields = ('name', 'brewery__name', 'style__name')
inlines = [BeerPurchaseInline]

def formfield_for_foreignkey(self, db_field, request, **kwargs):
form_field = super().formfield_for_foreignkey(db_field, request, **kwargs)
Expand Down
35 changes: 35 additions & 0 deletions backend/purchases/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
from django.contrib import admin
from django.forms import BaseModelFormSet, BaseModelForm

from purchases.models import BeerPurchase
from ratings.models import Rating


class RatingInline(admin.StackedInline):
model = Rating
extra = 0
fk_name = 'beer_purchase'
fields = (
# beer is derived from parent BeerPurchase
'added_by',
'room',
'color',
'foam',
'smell',
'taste',
'opinion',
'note',
'is_published'
)

def has_view_or_change_permission(self, request, obj=None):
return False


@admin.register(BeerPurchase)
Expand All @@ -14,6 +37,18 @@ class BeerPurchaseAdmin(admin.ModelAdmin):
'beer__name', 'beer__style__name',
'beer__brewery__name', 'sold_to__username'
)
inlines = [RatingInline]

def save_formset(self, request, form: BaseModelForm, formset: BaseModelFormSet, change: bool) -> None:
obj: BeerPurchase = form.instance

for form in formset.extra_forms:
# derive rated beer from parent BeerPurchase model,
# so that we do not need to select it manually
if isinstance(form.instance, Rating):
form.instance.beer = obj.beer

super().save_formset(request, form, formset, change)

def formfield_for_foreignkey(self, db_field, request, **kwargs):
form_field = super().formfield_for_foreignkey(db_field, request, **kwargs)
Expand Down
Loading