Source

django-swingcms / swingcms / loading.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.db import models



# precalculate frequently used structures that are available only at runtime, extending AppCache
class SwingCache(models.loading.AppCache):

    __shared_state_integration = dict(
        _get_contents_cache = [],
        _get_contents_names_cache = [],
        _get_models_names_cache = [],
        _get_models_actions_cache = {},
        _get_field_names_cache = set(),
        _get_datetime_field_names_cache = set()
    )

    def __init__(self, *args, **kwargs):

        super(SwingCache, self).__init__(*args, **kwargs)
        self.__dict__.update(self.__shared_state_integration)

    def get_contents(self):

        if self._get_contents_cache:
            return self._get_contents_cache

        from cms.models import BaseContent # avoid circular import

        self._populate()
        self._get_contents_cache = [m for m in self.get_models() if issubclass(m, BaseContent)]
        return self._get_contents_cache

    def get_contents_names(self):

        if self._get_contents_names_cache:
            return self._get_contents_names_cache
        self._populate()
        self._get_models_names_cache = [m.__name__.lower() for m in self.get_contents()]
        return self._get_models_names_cache

    def get_models_names(self):

        if self._get_models_names_cache:
            return self._get_models_names_cache
        self._populate()
        self._get_models_names_cache = [m.__name__.lower() for m in self.get_models()]
        return self._get_models_names_cache

    def get_field_names(self):

        if self._get_field_names_cache:
            return self._get_field_names_cache

        self._populate()

        field_names = set()
        for c in self.get_contents():
            for f in c._meta.fields:
                if not isinstance(f, models.AutoField):
                    field_names.update((f.name,))
        self._get_field_names_cache =  sorted(field_names)

        return self._get_field_names_cache

    def get_datetime_field_names(self):

        if self._get_datetime_field_names_cache:
            return self._get_datetime_field_names_cache

        self._populate()

        datetime_field_names = set()
        for c in self.get_contents():
            for f in c._meta.fields:
                if isinstance(f, models.DateTimeField):
                    datetime_field_names.update((f.name,))
        self._get_datetime_field_names_cache =  sorted(datetime_field_names)

        return self._get_datetime_field_names_cache

    @property
    def APP_MODELS(self):
        self._populate()
        return self.app_models

    @property
    def MODELS(self):
        return self.get_models()

    @property
    def CONTENTS(self):
        return self.get_contents()

    @property
    def CONTENTS_NAMES(self):
        return self.get_contents_names()

    @property
    def MODELS_NAMES(self):
        return self.get_models_names()

    @property
    def FIELD_NAMES(self):
        return self.get_field_names()

    @property
    def DATETIME_FIELD_NAMES(self):
        return self.get_datetime_field_names()

swingcache = SwingCache()