refactor admin into several files
This commit is contained in:
parent
d9a04655ed
commit
a3cf1cc590
7 changed files with 340 additions and 184 deletions
|
@ -1,445 +0,0 @@
|
|||
from django.contrib import admin
|
||||
from django.utils.html import format_html
|
||||
from adminsortable2.admin import SortableAdminMixin
|
||||
from import_export.admin import ImportExportModelAdmin
|
||||
from import_export import resources
|
||||
from import_export.fields import Field
|
||||
from import_export.widgets import ForeignKeyWidget
|
||||
|
||||
from .models import (
|
||||
Category,
|
||||
CloudProvider,
|
||||
ComputePlan,
|
||||
ComputePlanGroup,
|
||||
ComputePlanPrice,
|
||||
ConsultingPartner,
|
||||
ExternalLink,
|
||||
ExternalLinkOffering,
|
||||
Lead,
|
||||
Plan,
|
||||
ProgressiveDiscountModel,
|
||||
DiscountTier,
|
||||
ReusableText,
|
||||
Service,
|
||||
ServiceOffering,
|
||||
StoragePlan,
|
||||
StoragePlanPrice,
|
||||
VSHNAppCatBaseFee,
|
||||
VSHNAppCatPrice,
|
||||
VSHNAppCatUnitRate,
|
||||
WebsiteFaq,
|
||||
)
|
||||
|
||||
|
||||
class PlanInline(admin.StackedInline):
|
||||
model = Plan
|
||||
extra = 1
|
||||
fieldsets = (
|
||||
(None, {"fields": ("name", "description", "pricing", "plan_description")}),
|
||||
)
|
||||
|
||||
|
||||
class ExternalLinkOfferingInline(admin.TabularInline):
|
||||
model = ExternalLinkOffering
|
||||
extra = 1
|
||||
fields = ("description", "url", "order")
|
||||
ordering = ("order", "description")
|
||||
|
||||
|
||||
class OfferingInline(admin.StackedInline):
|
||||
model = ServiceOffering
|
||||
extra = 1
|
||||
fieldsets = (
|
||||
(
|
||||
None,
|
||||
{
|
||||
"fields": (
|
||||
"description",
|
||||
"service",
|
||||
"cloud_provider",
|
||||
"offer_description",
|
||||
)
|
||||
},
|
||||
),
|
||||
)
|
||||
show_change_link = True
|
||||
|
||||
|
||||
@admin.register(ReusableText)
|
||||
class ReusableTextAdmin(admin.ModelAdmin):
|
||||
list_display = ("name",)
|
||||
search_fields = ("name", "text")
|
||||
ordering = ("name",)
|
||||
|
||||
|
||||
@admin.register(Category)
|
||||
class CategoryAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "slug", "parent", "order")
|
||||
list_filter = ("parent",)
|
||||
search_fields = ("name", "description")
|
||||
prepopulated_fields = {"slug": ("name",)}
|
||||
ordering = ("order", "name")
|
||||
|
||||
|
||||
class ComputePlanItemInline(admin.TabularInline):
|
||||
model = ComputePlan
|
||||
extra = 1
|
||||
fields = ("name", "vcpus", "ram", "active", "valid_from", "valid_to")
|
||||
|
||||
|
||||
@admin.register(CloudProvider)
|
||||
class CloudProviderAdmin(SortableAdminMixin, admin.ModelAdmin):
|
||||
list_display = (
|
||||
"name",
|
||||
"slug",
|
||||
"logo_preview",
|
||||
"disable_listing",
|
||||
"is_featured",
|
||||
"order",
|
||||
)
|
||||
search_fields = ("name", "description")
|
||||
prepopulated_fields = {"slug": ("name",)}
|
||||
inlines = [OfferingInline]
|
||||
ordering = ("order",)
|
||||
|
||||
def logo_preview(self, obj):
|
||||
if obj.logo:
|
||||
return format_html(
|
||||
'<img src="{}" style="max-height: 50px;"/>', obj.logo.url
|
||||
)
|
||||
return "No logo"
|
||||
|
||||
logo_preview.short_description = "Logo"
|
||||
|
||||
|
||||
class ExternalLinkInline(admin.TabularInline):
|
||||
model = ExternalLink
|
||||
extra = 1
|
||||
fields = ("description", "url", "order")
|
||||
ordering = ("order", "description")
|
||||
|
||||
|
||||
@admin.register(Service)
|
||||
class ServiceAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"name",
|
||||
"logo_preview",
|
||||
"category_list",
|
||||
"is_featured",
|
||||
"is_coming_soon",
|
||||
"disable_listing",
|
||||
)
|
||||
list_filter = ("categories",)
|
||||
search_fields = ("name", "description", "slug")
|
||||
prepopulated_fields = {"slug": ("name",)}
|
||||
filter_horizontal = ("categories",)
|
||||
inlines = [ExternalLinkInline, OfferingInline]
|
||||
|
||||
def logo_preview(self, obj):
|
||||
if obj.logo:
|
||||
return format_html(
|
||||
'<img src="{}" style="max-height: 50px;"/>', obj.logo.url
|
||||
)
|
||||
return "No logo"
|
||||
|
||||
logo_preview.short_description = "Logo"
|
||||
|
||||
def category_list(self, obj):
|
||||
return ", ".join([cat.name for cat in obj.categories.all()])
|
||||
|
||||
category_list.short_description = "Categories"
|
||||
|
||||
def partner_list(self, obj):
|
||||
return ", ".join([partner.name for partner in obj.consulting_partners.all()])
|
||||
|
||||
partner_list.short_description = "Consulting Partners"
|
||||
|
||||
|
||||
@admin.register(ServiceOffering)
|
||||
class ServiceOfferingAdmin(admin.ModelAdmin):
|
||||
list_display = ("service", "cloud_provider")
|
||||
list_filter = ("service", "cloud_provider")
|
||||
search_fields = ("service__name", "cloud_provider__name", "description")
|
||||
inlines = [ExternalLinkOfferingInline, PlanInline]
|
||||
|
||||
|
||||
@admin.register(ConsultingPartner)
|
||||
class ConsultingPartnerAdmin(SortableAdminMixin, admin.ModelAdmin):
|
||||
list_display = (
|
||||
"name",
|
||||
"website",
|
||||
"logo_preview",
|
||||
"disable_listing",
|
||||
"is_featured",
|
||||
"order",
|
||||
)
|
||||
search_fields = ("name", "description")
|
||||
prepopulated_fields = {"slug": ("name",)}
|
||||
filter_horizontal = ("services", "cloud_providers")
|
||||
ordering = ("order",)
|
||||
|
||||
def logo_preview(self, obj):
|
||||
if obj.logo:
|
||||
return format_html(
|
||||
'<img src="{}" style="max-height: 50px;"/>', obj.logo.url
|
||||
)
|
||||
return "No logo"
|
||||
|
||||
logo_preview.short_description = "Logo"
|
||||
|
||||
|
||||
@admin.register(Lead)
|
||||
class LeadAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "company", "created_at", "odoo_lead_id")
|
||||
search_fields = ("name", "company")
|
||||
|
||||
|
||||
@admin.register(WebsiteFaq)
|
||||
class WebsiteFaqAdmin(SortableAdminMixin, admin.ModelAdmin):
|
||||
list_display = ("question", "order")
|
||||
search_fields = ("question", "answer")
|
||||
ordering = ("order",)
|
||||
|
||||
|
||||
class ComputePlanPriceInline(admin.TabularInline):
|
||||
model = ComputePlanPrice
|
||||
extra = 1
|
||||
fields = ("currency", "amount")
|
||||
|
||||
|
||||
@admin.register(ComputePlanGroup)
|
||||
class ComputePlanGroupAdmin(SortableAdminMixin, admin.ModelAdmin):
|
||||
list_display = ("name", "node_label", "compute_plans_count")
|
||||
search_fields = ("name", "description", "node_label")
|
||||
ordering = ("order",)
|
||||
|
||||
def compute_plans_count(self, obj):
|
||||
return obj.compute_plans.count()
|
||||
|
||||
compute_plans_count.short_description = "Compute Plans"
|
||||
|
||||
|
||||
class ComputePlanResource(resources.ModelResource):
|
||||
cloud_provider = Field(
|
||||
column_name="cloud_provider",
|
||||
attribute="cloud_provider",
|
||||
widget=ForeignKeyWidget(CloudProvider, "name"),
|
||||
)
|
||||
group = Field(
|
||||
column_name="group",
|
||||
attribute="group",
|
||||
widget=ForeignKeyWidget(ComputePlanGroup, "name"),
|
||||
)
|
||||
prices = Field(column_name="prices", attribute=None)
|
||||
|
||||
class Meta:
|
||||
model = ComputePlan
|
||||
skip_unchanged = True
|
||||
report_skipped = False
|
||||
import_id_fields = ["name"]
|
||||
fields = (
|
||||
"name",
|
||||
"vcpus",
|
||||
"ram",
|
||||
"cpu_mem_ratio",
|
||||
"cloud_provider",
|
||||
"group",
|
||||
"active",
|
||||
"term",
|
||||
"valid_from",
|
||||
"valid_to",
|
||||
"prices",
|
||||
)
|
||||
|
||||
def dehydrate_prices(self, compute_plan):
|
||||
prices = compute_plan.prices.all()
|
||||
if not prices:
|
||||
return ""
|
||||
return "|".join([f"{p.currency} {p.amount}" for p in prices])
|
||||
|
||||
def save_m2m(self, instance, row, *args, **kwargs):
|
||||
super().save_m2m(instance, row, *args, **kwargs)
|
||||
|
||||
# Handle prices
|
||||
if "prices" in row and row["prices"]:
|
||||
# Clear existing prices first
|
||||
instance.prices.all().delete()
|
||||
|
||||
# Create new prices
|
||||
price_entries = row["prices"].split("|")
|
||||
for entry in price_entries:
|
||||
if " " in entry:
|
||||
currency, amount = entry.split(" ")
|
||||
ComputePlanPrice.objects.create(
|
||||
compute_plan=instance, currency=currency, amount=amount
|
||||
)
|
||||
|
||||
|
||||
@admin.register(ComputePlan)
|
||||
class ComputePlansAdmin(ImportExportModelAdmin):
|
||||
resource_class = ComputePlanResource
|
||||
list_display = (
|
||||
"name",
|
||||
"cloud_provider",
|
||||
"group",
|
||||
"vcpus",
|
||||
"ram",
|
||||
"term",
|
||||
"display_prices",
|
||||
"active",
|
||||
)
|
||||
search_fields = ("name", "cloud_provider__name", "group__name")
|
||||
list_filter = ("active", "cloud_provider", "group")
|
||||
ordering = ("name",)
|
||||
inlines = [ComputePlanPriceInline]
|
||||
|
||||
def display_prices(self, obj):
|
||||
prices = obj.prices.all()
|
||||
if not prices:
|
||||
return "No prices set"
|
||||
return format_html("<br>".join([f"{p.amount} {p.currency}" for p in prices]))
|
||||
|
||||
display_prices.short_description = "Prices (Amount Currency)"
|
||||
|
||||
|
||||
class VSHNAppCatBaseFeeInline(admin.TabularInline):
|
||||
model = VSHNAppCatBaseFee
|
||||
extra = 1
|
||||
fields = ("currency", "amount")
|
||||
|
||||
|
||||
class VSHNAppCatUnitRateInline(admin.TabularInline):
|
||||
model = VSHNAppCatUnitRate
|
||||
extra = 1
|
||||
fields = ("currency", "service_level", "amount")
|
||||
|
||||
|
||||
class DiscountTierInline(admin.TabularInline):
|
||||
model = DiscountTier
|
||||
extra = 1
|
||||
fields = ("min_units", "max_units", "discount_percent")
|
||||
ordering = ("min_units",)
|
||||
|
||||
|
||||
@admin.register(ProgressiveDiscountModel)
|
||||
class ProgressiveDiscountModelAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "description", "active")
|
||||
search_fields = ("name", "description")
|
||||
inlines = [DiscountTierInline]
|
||||
|
||||
|
||||
@admin.register(VSHNAppCatPrice)
|
||||
class VSHNAppCatPriceAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"service",
|
||||
"variable_unit",
|
||||
"term",
|
||||
"discount_model",
|
||||
"admin_display_base_fees",
|
||||
"admin_display_unit_rates",
|
||||
)
|
||||
list_filter = ("variable_unit", "service", "discount_model")
|
||||
search_fields = ("service__name",)
|
||||
inlines = [VSHNAppCatBaseFeeInline, VSHNAppCatUnitRateInline]
|
||||
|
||||
def admin_display_base_fees(self, obj):
|
||||
fees = obj.base_fees.all()
|
||||
if not fees:
|
||||
return "No base fees"
|
||||
return format_html(
|
||||
"<br>".join([f"{fee.amount} {fee.currency}" for fee in fees])
|
||||
)
|
||||
|
||||
admin_display_base_fees.short_description = "Base Fees"
|
||||
|
||||
def admin_display_unit_rates(self, obj):
|
||||
rates = obj.unit_rates.all()
|
||||
if not rates:
|
||||
return "No unit rates"
|
||||
return format_html(
|
||||
"<br>".join(
|
||||
[
|
||||
f"{rate.amount} {rate.currency} ({rate.get_service_level_display()})"
|
||||
for rate in rates
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
admin_display_unit_rates.short_description = "Unit Rates"
|
||||
|
||||
|
||||
class StoragePlanPriceInline(admin.TabularInline):
|
||||
model = StoragePlanPrice
|
||||
extra = 1
|
||||
fields = ("currency", "amount")
|
||||
|
||||
|
||||
class StoragePlanResource(resources.ModelResource):
|
||||
cloud_provider = Field(
|
||||
column_name="cloud_provider",
|
||||
attribute="cloud_provider",
|
||||
widget=ForeignKeyWidget(CloudProvider, "name"),
|
||||
)
|
||||
prices = Field(column_name="prices", attribute=None)
|
||||
|
||||
class Meta:
|
||||
model = StoragePlan
|
||||
skip_unchanged = True
|
||||
report_skipped = False
|
||||
import_id_fields = ["name"]
|
||||
fields = (
|
||||
"name",
|
||||
"cloud_provider",
|
||||
"term",
|
||||
"unit",
|
||||
"valid_from",
|
||||
"valid_to",
|
||||
"prices",
|
||||
)
|
||||
|
||||
def dehydrate_prices(self, storage_plan):
|
||||
prices = storage_plan.prices.all()
|
||||
if not prices:
|
||||
return ""
|
||||
return "|".join([f"{p.currency} {p.amount}" for p in prices])
|
||||
|
||||
def save_m2m(self, instance, row, *args, **kwargs):
|
||||
super().save_m2m(instance, row, *args, **kwargs)
|
||||
|
||||
# Handle prices
|
||||
if "prices" in row and row["prices"]:
|
||||
# Clear existing prices first
|
||||
instance.prices.all().delete()
|
||||
|
||||
# Create new prices
|
||||
price_entries = row["prices"].split("|")
|
||||
for entry in price_entries:
|
||||
if " " in entry:
|
||||
currency, amount = entry.split(" ")
|
||||
StoragePlanPrice.objects.create(
|
||||
storage_plan=instance, currency=currency, amount=amount
|
||||
)
|
||||
|
||||
|
||||
@admin.register(StoragePlan)
|
||||
class StoragePlanAdmin(ImportExportModelAdmin):
|
||||
resource_class = StoragePlanResource
|
||||
list_display = (
|
||||
"name",
|
||||
"cloud_provider",
|
||||
"term",
|
||||
"unit",
|
||||
"display_prices",
|
||||
)
|
||||
search_fields = ("name", "cloud_provider__name")
|
||||
list_filter = ("cloud_provider",)
|
||||
ordering = ("name",)
|
||||
inlines = [StoragePlanPriceInline]
|
||||
|
||||
def display_prices(self, obj):
|
||||
prices = obj.prices.all()
|
||||
if not prices:
|
||||
return "No prices set"
|
||||
return format_html("<br>".join([f"{p.amount} {p.currency}" for p in prices]))
|
||||
|
||||
display_prices.short_description = "Prices (Amount Currency)"
|
Loading…
Add table
Add a link
Reference in a new issue