Mercurial > public > sg101
comparison downloads/management/commands/dlwgetcat.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 (2012-05-05) |
parents | gpp/downloads/management/commands/dlwgetcat.py@311c926dd218 |
children | 161b56849114 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 dlwgetcat - a management command to produce a bash script that wgets all the | |
3 files in a given category. | |
4 | |
5 """ | |
6 import os.path | |
7 | |
8 from django.core.management.base import LabelCommand, CommandError | |
9 from django.template.loader import render_to_string | |
10 from django.template.defaultfilters import slugify | |
11 from django.contrib.sites.models import Site | |
12 from django.conf import settings | |
13 | |
14 from downloads.models import Category, Download | |
15 | |
16 | |
17 class Command(LabelCommand): | |
18 help = ("Produce on standard output a bash script that wgets all the files" | |
19 " in a category. The files are downloaded with a slugified name.") | |
20 | |
21 args = "category-slug" | |
22 | |
23 def handle_label(self, slug, **options): | |
24 """ | |
25 Render a template using the downloads in a given category and send it to | |
26 stdout. | |
27 | |
28 """ | |
29 try: | |
30 category = Category.objects.get(slug=slug) | |
31 except Category.DoesNotExist: | |
32 raise CommandError("category slug '%s' does not exist" % slug) | |
33 | |
34 downloads = Download.public_objects.filter(category=category).order_by( | |
35 'title').select_related() | |
36 | |
37 # Create new destination names for the files since the uploaders often | |
38 # give the files terrible names. The new names will be slugified | |
39 # versions of the titles, with the same extension. | |
40 | |
41 for dl in downloads: | |
42 ext = os.path.splitext(dl.file.name)[1] | |
43 dl.dest_filename = slugify(dl.title) + ext | |
44 | |
45 output = render_to_string('downloads/commands/wget_cat.html', { | |
46 'downloads': downloads, | |
47 'domain': Site.objects.get_current().domain, | |
48 'MEDIA_URL': settings.MEDIA_URL, | |
49 }) | |
50 | |
51 # encode it ourselves since it can fail if you try to redirect output to | |
52 # a file and any of the content is not ASCII... | |
53 print output.encode('utf-8') |