1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
import os
import logging
from django.conf import settings as django_settings
def get_skin_dirs():
#todo: handle case of multiple skin directories
d = os.path.dirname
n = os.path.normpath
j = os.path.join
f = os.path.isfile
skin_dirs = []
skin_dirs.append( n(j(d(d(__file__)), 'skins')) )
if hasattr(django_settings, 'ASKBOT_EXTRA_SKIN_DIRS'):
skin_dirs += django_settings.ASKBOT_EXTRA_SKIN_DIRS
return skin_dirs
def get_skin_choices():
#todo: expand this to handle custom skin directories
skin_list = ['default']
for directory in get_skin_dirs():
items = os.listdir(directory)
for i in items:
item_path = os.path.join(directory, i)
if not os.path.isdir(item_path):
continue
if i == 'common':
continue
if i not in skin_list:
skin_list.append(i)
return [(i,i) for i in skin_list]
def get_media_url(url):
"""returns url prefixed with the skin name
of the first skin that contains the file
directories are searched in this order:
askbot_settings.ASKBOT_DEFAULT_SKIN, then 'default', then 'commmon'
if file is not found - returns None
and logs an error message
"""
url = unicode(url)
while url[0] == '/': url = url[1:]
#todo: handles case of multiple skin directories
#if file is in upfiles directory, then give that
url_copy = url
if url_copy.startswith(django_settings.ASKBOT_UPLOADED_FILES_URL):
file_path = url_copy.replace(
django_settings.ASKBOT_UPLOADED_FILES_URL,
'',
1
)
file_path = os.path.join(
django_settings.ASKBOT_FILE_UPLOAD_DIR,
file_path
)
if os.path.isfile(file_path):
url_copy = os.path.normpath(
'///' + url_copy
).replace(
'\\', '/'
).replace(
'///', '/'
)
return url_copy
else:
logging.critical('missing media resource %s' % url)
#2) if it does not exist - look in skins
#purpose of this try statement is to determine
#which skin is currently used
try:
#this import statement must be hidden here
#because at startup time this branch will fail
#due to an import error
from askbot.conf import settings as askbot_settings
use_skin = askbot_settings.ASKBOT_DEFAULT_SKIN
resource_revision = askbot_settings.MEDIA_RESOURCE_REVISION
except ImportError:
use_skin = 'default'
resource_revision = None
skins = get_skin_dirs()[0]
#see if file exists, if not, try skins 'default', then 'common'
file_path = os.path.join(skins, use_skin, 'media', url)
if not os.path.isfile(file_path):
file_path = os.path.join(skins, 'default', 'media', url)
if os.path.isfile(file_path):
use_skin = 'default'
else:
file_path = os.path.join(skins, 'common', 'media', url)
if os.path.isfile(file_path):
use_skin = 'common'
else:
log_message = 'missing media resource %s in skin %s' \
% (url, use_skin)
logging.critical(log_message)
use_skin = ''
return None
url = use_skin + '/media/' + url
url = '///' + django_settings.ASKBOT_URL + 'm/' + url
url = os.path.normpath(url).replace(
'\\', '/'
).replace(
'///', '/'
)
if resource_revision:
url += '?v=%d' % resource_revision
return url
|