# -*- coding: utf-8 -*-
from django.contrib import admin
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin

from coin.configuration.models import Configuration
from coin.configuration.forms import ConfigurationForm

"""
Implementation note : When creating child admin class, you have to inherit
ConfigurationAdminFormMixin. This make use of ConfigurationForm form that
filter offersubscription select input to avoid selecting wrong subscription.
"""

class ParentConfigurationAdmin(PolymorphicParentModelAdmin):
    base_model = Configuration
    polymorphic_list = True
    list_display = ('model_name','configuration_type_name', 'offersubscription', 'offer_subscription_member')

    def offer_subscription_member(self, config):
        return config.offersubscription.member
    offer_subscription_member.short_description = 'Membre'

    def get_child_models(self):
        """
        Renvoi la liste des modèles enfants de Configuration
        ex :((VPNConfiguration, VPNConfigurationAdmin),
            (ADSLConfiguration, ADSLConfigurationAdmin))
        """
        return (tuple((x.base_model, x) for x in PolymorphicChildModelAdmin.__subclasses__()))

class ConfigurationAdminFormMixin(object):
    form = ConfigurationForm

admin.site.register(Configuration, ParentConfigurationAdmin)