mise-en-production
[auf_framonde.git] / project / cmsplugin_contact_plus / utils.py
1 import glob
2 import os
3 import threading
4
5 from django.conf import settings
6
7 localdata = threading.local()
8 localdata.TEMPLATES = tuple()
9 TEMPLATES = localdata.TEMPLATES
10
11
12 def autodiscover_templates():
13 '''
14 Autodiscovers cmsplugin_contact_plus templates the way
15 'django.template.loaders.filesystem.Loader' and
16 'django.template.loaders.app_directories.Loader' work.
17 '''
18 def sorted_templates(templates):
19 '''
20 Sorts templates
21 '''
22 TEMPLATES = sorted(templates, key=lambda template: template[1])
23 return TEMPLATES
24
25 # obviously, cache for better performance
26 global TEMPLATES
27 if TEMPLATES:
28 return TEMPLATES
29
30 # override templates from settings
31 override_dir = getattr(settings, 'CMSPLUGIN_CONTACT_PLUS_TEMPLATES', None)
32 if override_dir:
33 return sorted_templates(override_dir)
34
35 templates = []
36 # templates = [
37 # ('cmsplugin_contact_plus/hello.html', 'hello.html'),
38 # ]
39
40 dirs_to_scan = []
41 if 'django.template.loaders.app_directories.Loader' in settings.TEMPLATE_LOADERS:
42 for app in settings.INSTALLED_APPS:
43 _ = __import__(app)
44 dir = os.path.dirname(_.__file__)
45 if not dir in dirs_to_scan:
46 # append 'templates' for app directories
47 dirs_to_scan.append(os.path.join(dir, 'templates'))
48
49 if 'django.template.loaders.filesystem.Loader' in settings.TEMPLATE_LOADERS:
50 for dir in settings.TEMPLATE_DIRS:
51 if not dir in dirs_to_scan:
52 # filesystem loader assumes our templates in 'templates'
53 # already
54 dirs_to_scan.append(dir)
55
56 for dir in dirs_to_scan:
57 found = glob.glob(os.path.join(dir, 'cmsplugin_contact_plus/*.html'))
58 for file in found:
59 dir, file = os.path.split(file)
60 key, value = os.path.join(dir.split('/')[-1], file), file
61 f = False
62 for _, template in templates:
63 if template == file:
64 f = True
65 if not f:
66 templates.append((key, value,))
67 # print os.path.basename(file)
68
69 return sorted_templates(templates)