Mercurial > public > sg101
comparison gpp/downloads/management/commands/dlwgetcat.py @ 527:645025056dfa
Adding some management commands to the downloads application to help manage
the SG101 MP3 compilations. Added a command to generate a HTML report of all
the downloads in a given category. Added a command that generates a bash
script that wgets all the files in a category.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 21 Dec 2011 01:08:21 +0000 |
parents | |
children | 311c926dd218 |
comparison
equal
deleted
inserted
replaced
526:dd97341788fa | 527:645025056dfa |
---|---|
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 current_site = Site.objects.get_current() | |
38 | |
39 # Create new destination names for the files since the uploaders often | |
40 # give the files terrible names. The new names will be slugified | |
41 # versions of the titles, with the same extension. | |
42 | |
43 for dl in downloads: | |
44 ext = os.path.splitext(dl.file.name)[1] | |
45 dl.dest_filename = slugify(dl.title) + ext | |
46 | |
47 # build a full URL to the download | |
48 dl.full_url = 'http://%s%s%s' % (current_site.domain, | |
49 settings.MEDIA_URL, dl.file.name) | |
50 | |
51 output = render_to_string('downloads/commands/wget_cat.html', { | |
52 'downloads': downloads, | |
53 }) | |
54 | |
55 # encode it ourselves since it can fail if you try to redirect output to | |
56 # a file and any of the content is not ASCII... | |
57 print output.encode('utf-8') |