bgneal@527: """ bgneal@527: dlwgetcat - a management command to produce a bash script that wgets all the bgneal@527: files in a given category. bgneal@527: bgneal@527: """ bgneal@527: import os.path bgneal@527: bgneal@527: from django.core.management.base import LabelCommand, CommandError bgneal@527: from django.template.loader import render_to_string bgneal@685: from django.utils.text import slugify bgneal@527: from django.contrib.sites.models import Site bgneal@527: from django.conf import settings bgneal@527: bgneal@527: from downloads.models import Category, Download bgneal@527: bgneal@527: bgneal@527: class Command(LabelCommand): bgneal@527: help = ("Produce on standard output a bash script that wgets all the files" bgneal@527: " in a category. The files are downloaded with a slugified name.") bgneal@684: bgneal@527: args = "category-slug" bgneal@527: bgneal@527: def handle_label(self, slug, **options): bgneal@527: """ bgneal@527: Render a template using the downloads in a given category and send it to bgneal@527: stdout. bgneal@527: bgneal@527: """ bgneal@527: try: bgneal@527: category = Category.objects.get(slug=slug) bgneal@527: except Category.DoesNotExist: bgneal@527: raise CommandError("category slug '%s' does not exist" % slug) bgneal@527: bgneal@527: downloads = Download.public_objects.filter(category=category).order_by( bgneal@527: 'title').select_related() bgneal@527: bgneal@527: # Create new destination names for the files since the uploaders often bgneal@527: # give the files terrible names. The new names will be slugified bgneal@527: # versions of the titles, with the same extension. bgneal@527: bgneal@527: for dl in downloads: bgneal@527: ext = os.path.splitext(dl.file.name)[1] bgneal@527: dl.dest_filename = slugify(dl.title) + ext bgneal@527: bgneal@527: output = render_to_string('downloads/commands/wget_cat.html', { bgneal@527: 'downloads': downloads, bgneal@528: 'domain': Site.objects.get_current().domain, bgneal@528: 'MEDIA_URL': settings.MEDIA_URL, bgneal@527: }) bgneal@527: bgneal@874: self.stdout.write(output)