Skip to content

Commit

Permalink
add site title and description to theming
Browse files Browse the repository at this point in the history
  • Loading branch information
wtayyeb committed May 8, 2017
1 parent bd50bfe commit cdaa804
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion theming/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
'''


VERSION = (0, 7, 7)
VERSION = (0, 7, 8)

__version__ = '.'.join((str(i) for i in VERSION))
24 changes: 24 additions & 0 deletions theming/migrations/0002_auto_20170508_0505.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('theming', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='sitetheme',
name='site_description',
field=models.CharField(default=b'', max_length=255, blank=True),
),
migrations.AddField(
model_name='sitetheme',
name='site_title',
field=models.CharField(default=b'', max_length=255, blank=True),
),
]
24 changes: 3 additions & 21 deletions theming/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
from django.contrib.sites.models import Site, SITE_CACHE
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _ # @UnusedImport

from . import confs # to set default confs @UnusedImport - do not remove
from .threadlocals import get_thread_variable


logger = logging.getLogger(__name__)



@python_2_unicode_compatible
class Theme(object):
_metadata_filename = 'metadata.json'
Expand All @@ -33,7 +29,6 @@ def __init__(self, slug, *args, **kwargs):
self._metadata = {}
self.metadata_ready = None


def read_metadata(self):
filename = os.path.join(settings.THEMING_ROOT, self.slug, self._metadata_filename)
try:
Expand All @@ -44,7 +39,6 @@ def read_metadata(self):
self._metadata = {}
self.metadata_ready = False


def __getattr__(self, key):
if key not in ('name', 'description', 'author', 'version'):
raise AttributeError
Expand All @@ -62,12 +56,10 @@ def __getattr__(self, key):

return val


def __str__(self, *args, **kwargs):
return '<Theme `%s`>' % self.slug



class ThemeManager(object):
def __init__(self, *args, **kwargs):
super(ThemeManager, self).__init__(*args, **kwargs)
Expand All @@ -77,7 +69,6 @@ def __init__(self, *args, **kwargs):

self.patch_settings_staticfiles_dirs()


def find_themes(self, force=False):
if self._themes is None or force:
self._themes = {}
Expand All @@ -88,15 +79,13 @@ def find_themes(self, force=False):

return self._themes


def get_themes_choice(self):
themes = self.find_themes()
choices = []
for theme in themes.values():
choices.append((theme.slug, theme.name))
return choices


def get_current_theme(self):
sitetheme = get_thread_variable('sitetheme')
if sitetheme:
Expand All @@ -105,20 +94,18 @@ def get_current_theme(self):
theme = self.get_theme(settings.THEMING_DEFAULT_THEME)
return theme


def get_theme(self, theme_slug):
self.find_themes()
return self._themes[theme_slug]


def patch_settings_staticfiles_dirs(self):
staticfiles_dirs = []
for theme_slug in self.find_themes():
real_path = os.path.join(settings.THEMING_ROOT, '%s/static' % theme_slug)
if os.path.isdir(real_path):
key = os.path.join(settings.THEMING_URL, theme_slug)
staticfiles_dirs.append((key, real_path))
if os.name == 'nt': # hack for windows
if os.name == 'nt': # hack for windows
staticfiles_dirs.append((key.replace('/', '\\'), real_path))
else:
logger.debug('theme `%s` not found.' % theme_slug)
Expand All @@ -134,30 +121,25 @@ def patch_settings_staticfiles_dirs(self):
thememanager = ThemeManager()



@python_2_unicode_compatible
class SiteTheme(models.Model):
site = models.OneToOneField(Site)
theme_slug = models.CharField(max_length=100, choices=thememanager.get_themes_choice())

site_title = models.CharField(max_length=255, default='', blank=True)
site_description = models.CharField(max_length=255, default='', blank=True)

@property
def theme(self):
return thememanager.get_theme(self.theme_slug)


def __str__(self):
theme = self.theme
return '%s : [%s] %s' % (self.site, theme.slug, theme.name)


def delete(self, using=None):
SITE_CACHE.pop(self.site.domain, None)
return super(SiteTheme, self).delete(using=using)


def save(self, *args, **kwargs):
SITE_CACHE.pop(self.site.domain, None)
return super(SiteTheme, self).save(*args, **kwargs)


12 changes: 6 additions & 6 deletions theming/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

from django.conf import settings
from django.template import TemplateDoesNotExist
from django.templatetags.static import static
from django.utils._os import safe_join

from .models import thememanager
from django.templatetags.static import static

from .threadlocals import get_thread_variable

try:
from django.core.exceptions import SuspiciousFileOperation
Expand All @@ -26,7 +26,6 @@
from django.template.loader import BaseLoader



class Loader(BaseLoader):
is_usable = True

Expand Down Expand Up @@ -63,17 +62,18 @@ def load_template_source(self, template_name, template_dirs=None):
error_msg = ("Your template directories configuration is empty. "
"Change it to point to at least one template directory.")
raise TemplateDoesNotExist(error_msg)
load_template_source.is_usable = True

load_template_source.is_usable = True


def context_processor(request):
''' theming template context processor '''
theme = thememanager.get_current_theme()
theme_url = static(os.path.join(settings.THEMING_URL, theme.slug)).replace('\\', '/')
sitetheme = get_thread_variable('sitetheme')
return {
'theme_url': theme_url,
'sitetheme': theme,
'site_title': sitetheme.site_title,
'site_description': sitetheme.site_description,
}


0 comments on commit cdaa804

Please sign in to comment.