""" This module holds the managers and querysets for the ``theme`` app. """

from django.db import models
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned

from mtp.apps.sync.managers import SyncableQuerySet


class ThemeQuerySet(SyncableQuerySet):
    """The QuerySet used by the ``Theme`` model."""

    def get_default(self):
        """Return the current default theme."""

        try:
            default_theme = self.get(default=True)
        except MultipleObjectsReturned:
            default_theme = self.filter(default=True).first()
        except ObjectDoesNotExist:
            default_theme = self.first()  # Return None if no theme exists

        return default_theme


class ThemeManager(models.Manager.from_queryset(ThemeQuerySet)):
    """Manager for the ``mtp.apps.theme.models.Theme`` model."""

    pass
