-# coding: utf-8
+# -*- coding: utf-8
+
+import os
+import re
+import decimal
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe
from time import gmtime, strftime, localtime, mktime, time
from django.core.files import File
from django.core.files.storage import default_storage
-import os, re, decimal
+
from urlparse import urlparse
# filebrowser imports
"""
Change URL to PATH.
Value has to be an URL relative to MEDIA URL or a full URL (including MEDIA_URL).
-
+
Returns a PATH relative to MEDIA_ROOT.
"""
+ print "URL2PATH", repr(value)
mediaurl_re = re.compile(r'^(%s)' % (MEDIA_URL))
- value = mediaurl_re.sub('', value)
+ value = mediaurl_re.sub(u'', value)
return value
"""
Change PATH to URL.
Value has to be a PATH relative to MEDIA_ROOT.
-
+
Return an URL relative to MEDIA_ROOT.
"""
mediaroot_re = re.compile(r'^(%s)' % (MEDIA_ROOT))
"""
Construct the PATH to an Image version.
Value has to be server-path, relative to MEDIA_ROOT.
-
+
version_filename = filename + version_prefix + ext
Returns a path relative to MEDIA_ROOT.
"""
if os.path.isfile(os.path.join(MEDIA_ROOT, value)):
path, filename = os.path.split(value)
filename, ext = os.path.splitext(filename)
- version_filename = filename + "_" + version_prefix + ext
+ version_filename = filename + u"_" + version_prefix + ext
return os.path.join(VERSIONS_BASEDIR, path, version_filename)
else:
return None
def _url_join(*args):
- if args[0].startswith("http://"):
- url = "http://"
+ if args[0].startswith(u"http://"):
+ url = u"http://"
else:
- url = "/"
+ url = u"/"
for arg in args:
- arg = str(arg).replace("\\", "/")
- arg_split = arg.split("/")
+ arg = unicode(arg).replace(u"\\", u"/")
+ arg_split = arg.split(u"/")
for elem in arg_split:
- if elem != "" and elem != "http:":
- url = url + elem + "/"
+ if elem != u"" and elem != u"http:":
+ url = url + elem + u"/"
# remove trailing slash for filenames
if os.path.splitext(args[-1])[1]:
- url = url.rstrip("/")
+ url = url.rstrip(u"/")
return url
Get filterdate.
"""
- returnvalue = ''
dateYear = strftime("%Y", gmtime(dateTime))
dateMonth = strftime("%m", gmtime(dateTime))
dateDay = strftime("%d", gmtime(dateTime))
- if filterDate == 'today' and int(dateYear) == int(localtime()[0]) and int(dateMonth) == int(localtime()[1]) and int(dateDay) == int(localtime()[2]): returnvalue = 'true'
- elif filterDate == 'thismonth' and dateTime >= time() - 2592000: returnvalue = 'true'
- elif filterDate == 'thisyear' and int(dateYear) == int(localtime()[0]): returnvalue = 'true'
- elif filterDate == 'past7days' and dateTime >= time() - 604800: returnvalue = 'true'
- elif filterDate == '': returnvalue = 'true'
- return returnvalue
+
+ if filterDate == 'today' and int(dateYear) == int(localtime()[0]) and int(dateMonth) == int(localtime()[1]) and int(dateDay) == int(localtime()[2]):
+ return 'true'
+
+ if filterDate == 'thismonth' and dateTime >= time() - 2592000:
+ return 'true'
+
+ if filterDate == 'thisyear' and int(dateYear) == int(localtime()[0]):
+ return 'true'
+
+ if filterDate == 'past7days' and dateTime >= time() - 604800:
+ return 'true'
+
+ return 'true' if filterDate == '' else ''
def _get_settings_var():
"""
Get file type as defined in EXTENSIONS.
"""
-
file_extension = os.path.splitext(filename)[1].lower()
- file_type = ''
- for k, v in EXTENSIONS.iteritems():
- for extension in v:
- if file_extension == extension.lower():
- file_type = k
- return file_type
+ for ftype, ext_list in EXTENSIONS.iteritems():
+ if file_extension in map(str.lower, ext_list):
+ return ftype
+ return u''
def _is_selectable(filename, selecttype):
return select_types
-def _version_generator(value, version_prefix, force = None):
+def _version_generator(value, version_prefix, force=None):
"""
Generate Version for an Image.
value has to be a serverpath relative to MEDIA_ROOT.
from PIL import ImageFile
except ImportError:
import ImageFile
- ImageFile.MAXBLOCK = IMAGE_MAXBLOCK # default is 64k
+ ImageFile.MAXBLOCK = IMAGE_MAXBLOCK # default is 64k
try:
im = Image.open(os.path.join(MEDIA_ROOT, value))
os.chmod(version_dir, 0775)
version = scale_and_crop(im, VERSIONS[version_prefix]['width'], VERSIONS[version_prefix]['height'], VERSIONS[version_prefix]['opts'])
try:
- version.save(absolute_version_path, quality = 90, optimize = 1)
+ version.save(absolute_version_path, quality=90, optimize=1)
except IOError:
- version.save(absolute_version_path, quality = 90)
+ version.save(absolute_version_path, quality=90)
return version_path
except:
return None
r = min(xr / x, yr / y)
if r < 1.0 or (r > 1.0 and 'upscale' in opts):
- im = im.resize((int(x * r), int(y * r)), resample = Image.ANTIALIAS)
+ im = im.resize((int(x * r), int(y * r)), resample=Image.ANTIALIAS)
if 'crop' in opts:
x, y = [float(v) for v in im.size]
return value.replace(" ", "_").lower()
else:
return value
-
-
-