models.py 1.74 KB
Newer Older
1 2 3 4
"""
Django models supporting the Comprehensive Theming subsystem
"""
from django.contrib.sites.models import Site
5
from django.db import models
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21


class SiteTheme(models.Model):
    """
    This is where the information about the site's theme gets stored to the db.

    `site` field is foreignkey to django Site model
    `theme_dir_name` contains directory name having Site's theme
    """
    site = models.ForeignKey(Site, related_name='themes')
    theme_dir_name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.theme_dir_name

    @staticmethod
22
    def get_theme(site, default=None):
23 24 25 26 27 28
        """
        Get SiteTheme object for given site, returns default site theme if it can not
        find a theme for the given site and `DEFAULT_SITE_THEME` setting has a proper value.

        Args:
            site (django.contrib.sites.models.Site): site object related to the current site.
29 30
            default (openedx.core.djangoapps.models.SiteTheme): site theme object to return in case there is no theme
                associated for the given site.
31 32

        Returns:
33
            SiteTheme object for given site or a default site passed in as the argument.
34 35 36
        """

        theme = site.themes.first()
37
        return theme or default
38

39 40 41 42 43 44 45 46 47 48 49 50 51
    @staticmethod
    def has_theme(site):
        """
        Returns True if given site has an associated site theme in database, returns False otherwise.
        Note: DEFAULT_SITE_THEME is not considered as an associated site.

        Args:
            site (django.contrib.sites.models.Site): site object related to the current site.

        Returns:
            True if given site has an associated site theme in database, returns False otherwise.
        """
        return site.themes.exists()