from django.apps import AppConfig
from django.db.models.signals import post_save, post_delete
from django.conf import settings

class {{ package.name }}Config(AppConfig):
    name = '{{ package.name | case_package }}'

    def ready(self):
        import drf_nest.signals
        from {{ package.name | case_package }}.models import {% for cls in package.classes[:-1] %}{{ cls.name | case_class }}, {% endfor %}{{ package.classes[-1].name | case_class }}
        from {{ package.name | case_package }}.serializers import {% for cls in package.classes[:-1] %}{{ cls.name | case_class }}Serializer, {% endfor %}{{ package.classes[-1].name | case_class }}Serializer

        exchange_prefix = settings.MQ_FRAMEWORK['EXCHANGE_PREFIX'] + self.name

        {% for cls in package.classes %}{% if 'notifiable' in cls.stereotypes %}
        exchange_header_list = ()
        post_save.connect(  drf_nest.signals.notify_extra_args( serializer={{ cls.name | case_class }}Serializer, 
                                                                exchange_prefix=exchange_prefix + ".{{ cls.name | case_class }}", 
                                                                exchange_header_list=exchange_header_list)(drf_nest.signals.notify_save_instance), 
                                                                sender={{ cls.name | case_class }}, weak=False){% endif %}{% endfor %}

        try:
            from {{ package.name | case_package }}.signals import init_signals
            init_signals()
        except ImportError:
            pass

