from django.db.utils import OperationalError
from django.contrib.contenttypes.models import ContentType

PLUGIN_NAME = 'Carousel'
DESCRIPTION = 'This is a homepage element that renders a carousel.'
AUTHOR = 'Martin Paul Eve'


def install():
    import core.models as core_models
    import journal.models as journal_models
    import press.models as press_models

    # check whether this homepage element has already been installed for all journals
    journals = journal_models.Journal.objects.all()

    for journal in journals:
        content_type = ContentType.objects.get_for_model(journal)
        element, created = core_models.HomepageElement.objects.get_or_create(
            name=PLUGIN_NAME,
            configure_url='carousel_settings',
            template_path='journal/homepage_elements/carousel.html',
            content_type=content_type,
            object_id=journal.pk,
            has_config=True,
            defaults={'available_to_press': True})

        element.save()

    presses = press_models.Press.objects.all()

    for press in presses:
        content_type = ContentType.objects.get_for_model(press)
        element, created = core_models.HomepageElement.objects.get_or_create(
            name=PLUGIN_NAME,
            configure_url='carousel_settings',
            template_path='journal/homepage_elements/carousel.html',
            content_type=content_type,
            object_id=press.pk,
            has_config=True,
            defaults={'available_to_press': True})

        element.save()


def hook_registry():
    try:
        install()
        return {
            'yield_homepage_element_context': {
                'module': 'core.homepage_elements.carousel.hooks',
                'function': 'yield_homepage_element_context',
                'name': PLUGIN_NAME,
            }
        }
    except OperationalError:
        # if we get here the database hasn't yet been created
        return {}
    except BaseException:        # if we get here, well, something has gone very wrong
        return {}
