Browse files

I think I got it working

  • Loading branch information...
1 parent 4f7ee26 commit bc8c8aeff372c4a78906d8bb5b7247808f8bc1f6 Mark Ransom committed Oct 22, 2011
Showing with 39 additions and 31 deletions.
  1. +25 −27 cms_themes/__init__.py
  2. +14 −4 cms_themes/models.py
View
52 cms_themes/__init__.py
@@ -5,38 +5,36 @@
from django.conf import settings
from django.contrib.sites.models import Site
+from cms.conf.patch import post_patch
PROJECT_DIR = settings.PROJECT_DIR
-CMS_TEMPLATES = getattr(settings, 'CMS_TEMPLATES', ())
-
if hasattr(settings, 'THEMES_DIR'):
THEMES_DIR = settings.THEMES_DIR
else:
THEMES_DIR = os.path.join(PROJECT_DIR, 'themes')
+ if not os.path.exists(THEMES_DIR):
+ os.makedirs(THEMES_DIR)
setattr(settings, 'THEMES_DIR', THEMES_DIR)
+if not hasattr(settings, 'DEFAULT_CMS_TEMPLATES'):
+ setattr(settings, 'DEFAULT_CMS_TEMPLATES', settings.CMS_TEMPLATES)
+
+def set_themes():
+ try:
+ site = Site.objects.get(id=settings.SITE_ID)
+ themes = [theme[0] for theme in site.theme_set.values_list('name')]
+ except:
+ themes = []
+ theme_templates = []
-class cms_templates_iterable(object):
- def __iter__(self):
- return iter(self.get_themes())
- def __len__(self):
- return len(self.get_themes())
- def __getitem__(self, index):
- return self.get_themes()[index]
-
- def get_themes(self):
- try:
- site = Site.objects.get(id=settings.SITE_ID)
- themes = [theme[0] for theme in site.theme_set.values_list('name')]
- except:
- themes = []
- theme_templates = []
-
- for theme_dir in os.listdir(THEMES_DIR):
- if theme_dir in themes or not len(themes):
- theme_full_path = os.path.join(THEMES_DIR, theme_dir)
- if 'templates' in os.listdir(theme_full_path):
- template_path = os.path.join(theme_full_path, 'templates')
- for template in os.listdir(template_path):
- theme_templates.append((os.path.join(theme_dir, 'templates', template), '%s (%s)' % (template, theme_dir)))
- return theme_templates
-setattr(settings, 'CMS_TEMPLATES', cms_templates_iterable())
+ for theme_dir in os.listdir(THEMES_DIR):
+ if theme_dir in themes or not themes:
+ theme_full_path = os.path.join(THEMES_DIR, theme_dir)
+ if 'templates' in os.listdir(theme_full_path):
+ template_path = os.path.join(theme_full_path, 'templates')
+ for template in os.listdir(template_path):
+ template_path = os.path.join('themes', theme_dir, 'templates', template)
+ theme_templates.append((template_path, '%s (%s)' % (template, theme_dir)))
+
+ setattr(settings, 'CMS_TEMPLATES', tuple(theme_templates) + settings.DEFAULT_CMS_TEMPLATES)
+
+set_themes()
View
18 cms_themes/models.py
@@ -1,9 +1,13 @@
+import tarfile
+import StringIO
+import shutil
+import os
+
from django.conf import settings
from django.db import models
from django.contrib.sites.models import Site
-from django.db.models.signals import post_save, pre_save
-import tarfile
-import StringIO
+from django.db.models.signals import post_save, pre_save, post_delete
+from cms_themes import set_themes
class Theme(models.Model):
sites = models.ManyToManyField(Site, null=True, blank=True)
@@ -17,9 +21,15 @@ class Meta:
def extract_theme(sender, **kwargs):
instance = kwargs['instance']
if not instance.id:
- print dir(instance.theme_file)
f = tarfile.open(fileobj=instance.theme_file, mode='r:gz')
f.extractall(settings.THEMES_DIR)
+ set_themes()
+def update_themes(sender, **kwargs):
+ instance = kwargs['instance']
+ shutil.rmtree(os.path.join(settings.THEMES_DIR, instance.name))
+ set_themes()
+
pre_save.connect(extract_theme, sender=Theme)
+post_delete.connect(update_themes, sender=Theme)

0 comments on commit bc8c8ae

Please sign in to comment.