Skip to content
This repository has been archived by the owner on Oct 16, 2018. It is now read-only.

Commit

Permalink
Merge branch 'release/0.0.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rizmari Versfeld committed Nov 21, 2013
2 parents eea068c + 012b14f commit edbf4f6
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 50 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
@@ -1,6 +1,11 @@
Changelog
=========

0.0.8
-----
#. Correct calculation of download URL, particularly for temporary downloads.
#. Lots of PEP8 fixes.

0.0.7
-----
#. Temporary downloads adhere to `DOWNLOAD_SERVE_FROM` setting. They are always created on the local filesystem though. If 'REMOTE' is used the developer needs to sync these files with a remote filesystem.
Expand Down
7 changes: 5 additions & 2 deletions downloads/fields.py
Expand Up @@ -15,8 +15,11 @@ def __init__(self, *args, **kwargs):
super(ColourField, self).__init__(*args, **kwargs)

def formfield(self, *args, **kwargs):
kwargs['validators'] = [RegexValidator(r'^#[\da-f]{6}$',
_(u'Enter a hexadecimal-format colour.'), 'Invalid')]
kwargs['validators'] = [
RegexValidator(r'^#[\da-f]{6}$',
_(u'Enter a hexadecimal-format colour.'),
'Invalid')
]
return super(ColourField, self).formfield(*args, **kwargs)


Expand Down
27 changes: 17 additions & 10 deletions downloads/management/commands/adddownloads.py
Expand Up @@ -21,24 +21,30 @@ class Command(BaseCommand):
args = '<folder_path>'
help = 'Adds all files on <folder_path> as downloadable files'
option_list = BaseCommand.option_list + (
make_option('--category',
make_option(
'--category',
action='store',
type='string',
dest='category',
default=None,
help='Primary category for the downloads'),
make_option('-r',
help='Primary category for the downloads'
),
make_option(
'-r',
action='store_true',
dest='recursive',
help='Recursively add files'),
make_option('--publish',
help='Recursively add files'
),
make_option(
'--publish',
action='callback',
type='string',
callback=split_callback,
dest='sites',
default=False,
help='List of comma-separated domain names to publish to'),
)
help='List of comma-separated domain names to publish to'
),
)

def handle(self, *args, **options):
if len(args) > 0:
Expand All @@ -54,15 +60,16 @@ def handle(self, *args, **options):
sites = []
for site in options['sites']:
sites.append(re.escape(site))
sites = Site.objects.filter(
domain__regex=r'(' + '|'.join(sites) + ')')
sites = Site.objects.filter(domain__regex=
r'(' + '|'.join(sites) + ')')

count = 0
for root, dir, files in os.walk(args[0]):
for name in files:
self.stdout.write('Adding ' + root + '/' + name + '\n')
download = Download(title=name,
primary_category=category, state=state)
primary_category=category,
state=state)
download.save()
for site in sites:
download.sites.add(site)
Expand Down
10 changes: 4 additions & 6 deletions downloads/models.py
Expand Up @@ -78,8 +78,7 @@ def create_file(self, file_path, request):

def get_file(self, request):
file_name = self.make_file_name(request)
file_path = os.path.join(os.path.join(settings.MEDIA_ROOT,
TEMP_ROOT), file_name)
file_path = os.path.join(settings.MEDIA_ROOT, TEMP_ROOT, file_name)
# check if file exists
try:
f = open(file_path)
Expand Down Expand Up @@ -109,7 +108,7 @@ class TextOverlayTemporaryDownload(TemporaryDownloadAbstract):

def make_file_name(self, request):
return super(TextOverlayTemporaryDownload,
self).make_file_name(request, 'jpg')
self).make_file_name(request, 'jpg')

def draw_text(self, drawable, pos, text):
drawable.text(pos, text, font=self._font, fill=self._colour)
Expand All @@ -120,16 +119,15 @@ def create_file(self, file_path, request):
box = (self.x, self.y, self.width, self.height)
line_height = int(self.font_size * 0.85)
image = Image.open(os.path.join(settings.MEDIA_ROOT,
self.background_image.name)).copy()
self.background_image.name)).copy()
draw = ImageDraw.Draw(image)
# draw text with line breaking
height = 0
line = ''
for word in self.text.split(' '):
size = draw.textsize(line + word, font=self._font)
if size[0] > box[2]:
self.draw_text(draw,
(box[0], box[1] + height), line[0:-1])
self.draw_text(draw, (box[0], box[1] + height), line[0:-1])
line = word + ' '
height += line_height
else:
Expand Down
8 changes: 4 additions & 4 deletions downloads/templatetags/downloads_tags.py
Expand Up @@ -29,10 +29,10 @@ def __init__(self, category_dict, first=False, last=False):
def render(self, context):
extra = {
'category': self.category_dict.resolve(context),
'first': self.first if type(self.first) == bool
else self.first.resolve(context),
'last': self.last if type(self.last) == bool
else self.last.resolve(context),
'first': (self.first if type(self.first) == bool
else self.first.resolve(context)),
'last': (self.last if type(self.last) == bool
else self.last.resolve(context)),
}
return render_to_string(
'downloads/inclusion_tags/download_category.html',
Expand Down
14 changes: 6 additions & 8 deletions downloads/tests.py
Expand Up @@ -29,10 +29,10 @@ def make_download(self, file_path=None, title='some_title'):
if file_path is None:
# Just grab this actual file as a test file
file_path = os.path.join(settings.PROJECT_ROOT, 'downloads',
__file__)
__file__)
content_file = DjangoFile(open(file_path, 'r'), 'test_file.py')
dl = Download.objects.create(file=content_file, title=title,
image=content_file, state='published')
image=content_file, state='published')
# Must publish it to a site for it to become available
dl.sites.add(Site.objects.all()[0])
return dl
Expand All @@ -55,9 +55,7 @@ def test_header_is_being_set(self):
response = self.client.get(
reverse('download-request', kwargs={'slug': dl.slug})
)
self.assertEqual(response['X-Accel-Redirect'],
os.path.join(settings.MEDIA_URL, DOWNLOAD_FOLDER,
os.path.basename(dl.file.name)))
self.assertEqual(response['X-Accel-Redirect'], dl.file.url)

def test_duplicate_filenames(self):
"""Two files with the same name are uploaded"""
Expand All @@ -83,7 +81,7 @@ def test_signal_is_sent(self):
def test_serve_using_redirect(self):
dl = self.make_download()
settings.DOWNLOAD_SERVE_FROM = 'REMOTE'
r = self.client.get(reverse('download-request', kwargs={'slug': dl.slug}))
r = self.client.get(reverse('download-request',
kwargs={'slug': dl.slug}))
self.assertEqual(r.status_code, 302)
self.assertTrue(r['Location'].endswith(os.path.join(settings.MEDIA_URL, DOWNLOAD_FOLDER,
os.path.basename(dl.file.name))))
self.assertEqual('/%s' % r['Location'].split('/', 3)[3], dl.file.url)
3 changes: 2 additions & 1 deletion downloads/urls.py
Expand Up @@ -3,7 +3,8 @@
from downloads.views import object_list, download_request


urlpatterns = patterns('',
urlpatterns = patterns(
'',
# download url
url(
r'^$',
Expand Down
41 changes: 23 additions & 18 deletions downloads/views.py
@@ -1,5 +1,3 @@
import os.path

from mimetypes import guess_type

from django.http import HttpResponse, HttpResponseRedirect
Expand All @@ -13,7 +11,7 @@

from category.models import Category

from downloads.models import Download, DOWNLOAD_FOLDER, TemporaryDownloadAbstract
from downloads.models import Download
from downloads.signals import download_requested


Expand All @@ -35,7 +33,8 @@ def download_request(request, slug):

f, file_name = download.get_file(request)

# set this to 'REMOTE' if the request should be redirected to remote storage (like S3)
# set this to 'REMOTE' if the request should be redirected
# to remote storage (like S3)
serve_method = getattr(settings, 'DOWNLOAD_SERVE_FROM', 'LOCAL')

# files generated on the fly need to be served locally
Expand All @@ -46,18 +45,16 @@ def download_request(request, slug):
# check if it has encoding
if mime[1]:
response['Content-Encoding'] = mime[1]
response['Content-Disposition'] = 'attachment; \
filename="%s"' % smart_str(file_name)
response['Content-Disposition'] = ('attachment; filename="%s"'
% smart_str(file_name))
response['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response['Expires'] = '0'
response['Pragma'] = 'no-store, no-cache'
response[getattr(settings, 'DOWNLOAD_INTERNAL_REDIRECT_HEADER', 'X-Accel-Redirect')] = smart_str(
os.path.join(settings.MEDIA_URL, DOWNLOAD_FOLDER, os.path.basename(f.name))
)
response[getattr(settings, 'DOWNLOAD_INTERNAL_REDIRECT_HEADER',
'X-Accel-Redirect')] = smart_str(f.url)

else:
response = HttpResponseRedirect(smart_str(os.path.join(settings.MEDIA_URL,
DOWNLOAD_FOLDER, os.path.basename(f.name))))
response = HttpResponseRedirect(smart_str(f.url))

return response

Expand All @@ -68,14 +65,22 @@ def get_extra_context(self, *args, **kwargs):
dls = list(Download.permitted.all())

# create dictionary of categories
cat_dict = SortedDict((id, {'parent': parent, 'title': title,
'items': [], 'subcats': [], 'slug': slug, 'child_count': 0})
for (id, parent, title, slug)
in Category.objects.values_list('id', 'parent',
'title', 'slug'))
cat_dict = SortedDict((id, {'parent': parent,
'title': title,
'items': [],
'subcats': [],
'slug': slug,
'child_count': 0})
for (id, parent, title, slug) in
Category.objects.values_list('id', 'parent',
'title', 'slug'))
# add None key for downloads without a category
cat_dict[None] = {'parent': None, 'items': [],
'child_count': 0, 'subcats': []}
cat_dict[None] = {
'parent': None,
'items': [],
'child_count': 0,
'subcats': []
}

# add downloads to category item lists
for dl in dls:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -2,7 +2,7 @@

setup(
name='jmbo-downloads',
version='0.0.7',
version='0.0.8',
description='Downloads for Jmbo.',
long_description=(open('README.rst', 'r').read() +
open('AUTHORS.rst', 'r').read() +
Expand Down

0 comments on commit edbf4f6

Please sign in to comment.