from django.apps import AppConfig
from django.db.models import signals


def call_on_class_prepared(sender, **kwargs):
    """
    Calls the function only if it is defined in the class being prepared
    """
    try:
        sender.on_class_prepared()
    except AttributeError:
        pass


class GamesConfig(AppConfig):
    name = 'games'

    def ready(self):
        """
        Add signals to the application
        """
        from .models import Game
        from .signals import game_saved
        signals.post_save.connect(game_saved, sender='games.Game')

    def __init__(self, app_name, app_module):
        super(GamesConfig, self).__init__(app_name, app_module)
        signals.class_prepared.connect(call_on_class_prepared)
