symposion_app/registrasion/admin.py

231 lines
6 KiB
Python
Raw Normal View History

2016-01-22 05:01:30 +00:00
from django.contrib import admin
from django.core.exceptions import ObjectDoesNotExist
2016-01-22 05:01:30 +00:00
from django.utils.translation import ugettext_lazy as _
import nested_admin
from registrasion.models import conditions
from registrasion.models import inventory
2016-01-22 05:01:30 +00:00
class EffectsDisplayMixin(object):
def effects(self, obj):
return list(obj.effects())
2016-01-22 05:01:30 +00:00
# Inventory admin
2016-01-22 05:01:30 +00:00
class ProductInline(admin.TabularInline):
model = inventory.Product
2016-01-22 05:01:30 +00:00
@admin.register(inventory.Category)
2016-01-22 05:01:30 +00:00
class CategoryAdmin(admin.ModelAdmin):
model = inventory.Category
fields = ("name", "description", "required", "render_type",
"limit_per_user", "order",)
list_display = ("name", "description")
2016-01-22 05:01:30 +00:00
inlines = [
ProductInline,
]
@admin.register(inventory.Product)
class ProductAdmin(admin.ModelAdmin):
model = inventory.Product
list_display = ("name", "category", "description")
list_filter = ("category", )
2016-01-22 05:01:30 +00:00
# Discounts
class DiscountForProductInline(admin.TabularInline):
model = conditions.DiscountForProduct
2016-01-22 05:01:30 +00:00
verbose_name = _("Product included in discount")
verbose_name_plural = _("Products included in discount")
class DiscountForCategoryInline(admin.TabularInline):
model = conditions.DiscountForCategory
2016-01-22 05:01:30 +00:00
verbose_name = _("Category included in discount")
verbose_name_plural = _("Categories included in discount")
@admin.register(conditions.TimeOrStockLimitDiscount)
class TimeOrStockLimitDiscountAdmin(admin.ModelAdmin, EffectsDisplayMixin):
list_display = (
"description",
"start_time",
"end_time",
"limit",
"effects",
)
ordering = ("start_time", "end_time", "limit")
inlines = [
DiscountForProductInline,
DiscountForCategoryInline,
]
@admin.register(conditions.IncludedProductDiscount)
class IncludedProductDiscountAdmin(admin.ModelAdmin, EffectsDisplayMixin):
def enablers(self, obj):
return list(obj.enabling_products.all())
list_display = ("description", "enablers", "effects")
2016-01-22 05:01:30 +00:00
inlines = [
DiscountForProductInline,
DiscountForCategoryInline,
]
2017-04-22 08:39:07 +00:00
@admin.register(conditions.SpeakerDiscount)
class SpeakerDiscountAdmin(admin.ModelAdmin, EffectsDisplayMixin):
fields = ("description", "is_presenter", "is_copresenter", "proposal_kind")
list_display = ("description", "is_presenter", "is_copresenter", "effects")
ordering = ("-is_presenter", "-is_copresenter")
inlines = [
DiscountForProductInline,
DiscountForCategoryInline,
]
2016-01-22 05:01:30 +00:00
@admin.register(conditions.GroupMemberDiscount)
class GroupMemberDiscountAdmin(admin.ModelAdmin, EffectsDisplayMixin):
fields = ("description", "group")
list_display = ("description", "effects")
inlines = [
DiscountForProductInline,
DiscountForCategoryInline,
]
2016-01-22 05:01:30 +00:00
# Vouchers
class VoucherDiscountInline(nested_admin.NestedStackedInline):
model = conditions.VoucherDiscount
2016-01-22 05:01:30 +00:00
verbose_name = _("Discount")
# TODO work out why we're allowed to add more than one?
max_num = 1
extra = 1
inlines = [
DiscountForProductInline,
DiscountForCategoryInline,
]
class VoucherFlagInline(nested_admin.NestedStackedInline):
model = conditions.VoucherFlag
2016-01-22 05:01:30 +00:00
verbose_name = _("Product and category enabled by voucher")
verbose_name_plural = _("Products and categories enabled by voucher")
# TODO work out why we're allowed to add more than one?
max_num = 1
extra = 1
@admin.register(inventory.Voucher)
2016-01-22 05:01:30 +00:00
class VoucherAdmin(nested_admin.NestedAdmin):
def effects(self, obj):
''' List the effects of the voucher in the admin. '''
out = []
try:
discount_effects = obj.voucherdiscount.effects()
except ObjectDoesNotExist:
discount_effects = None
try:
enabling_effects = obj.voucherflag.effects()
except ObjectDoesNotExist:
enabling_effects = None
if discount_effects:
out.append("Discounts: " + str(list(discount_effects)))
if enabling_effects:
out.append("Enables: " + str(list(enabling_effects)))
return "\n".join(out)
model = inventory.Voucher
list_display = ("recipient", "code", "effects")
2016-01-22 05:01:30 +00:00
inlines = [
VoucherDiscountInline,
VoucherFlagInline,
2016-01-22 05:01:30 +00:00
]
# Enabling conditions
@admin.register(conditions.ProductFlag)
class ProductFlagAdmin(
nested_admin.NestedAdmin,
EffectsDisplayMixin):
def enablers(self, obj):
return list(obj.enabling_products.all())
model = conditions.ProductFlag
2016-04-12 01:43:38 +00:00
fields = ("description", "enabling_products", "condition", "products",
"categories"),
list_display = ("description", "enablers", "effects")
# Enabling conditions
@admin.register(conditions.CategoryFlag)
class CategoryFlagAdmin(
nested_admin.NestedAdmin,
EffectsDisplayMixin):
model = conditions.CategoryFlag
2016-04-12 01:43:38 +00:00
fields = ("description", "enabling_category", "condition", "products",
"categories"),
list_display = ("description", "enabling_category", "effects")
ordering = ("enabling_category",)
@admin.register(conditions.SpeakerFlag)
class SpeakerFlagAdmin(nested_admin.NestedAdmin, EffectsDisplayMixin):
model = conditions.SpeakerFlag
fields = ("description", "is_presenter", "is_copresenter", "proposal_kind",
"products", "categories")
list_display = ("description", "is_presenter", "is_copresenter", "effects")
ordering = ("-is_presenter", "-is_copresenter")
@admin.register(conditions.GroupMemberFlag)
class GroupMemberFlagAdmin(admin.ModelAdmin, EffectsDisplayMixin):
fields = ("description", "group", "products", "categories")
list_display = ("description", "effects")
@admin.register(conditions.TimeOrStockLimitFlag)
class TimeOrStockLimitFlagAdmin(admin.ModelAdmin, EffectsDisplayMixin):
list_display = (
"description",
"start_time",
"end_time",
"limit",
"effects",
)
ordering = ("start_time", "end_time", "limit")