# HG changeset patch # User Brian Neal # Date 1324429701 0 # Node ID 645025056dfa7a4893bfcba72736f584d2858790 # Parent dd97341788fa9a21bba43e0c522b11601d24826d 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. diff -r dd97341788fa -r 645025056dfa gpp/downloads/management/commands/dlcatreport.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/downloads/management/commands/dlcatreport.py Wed Dec 21 01:08:21 2011 +0000 @@ -0,0 +1,38 @@ +""" +dlcatreport - a management command to produce a HTML report of all the downloads +in a given category. + +""" +from django.core.management.base import LabelCommand, CommandError +from django.template.loader import render_to_string + +from downloads.models import Category, Download + + +class Command(LabelCommand): + help = "Produce on standard output a report of all downloads in a category." + args = "category-slug" + + def handle_label(self, slug, **options): + """ + Render a template using the downloads in a given category and send it to + stdout. + + """ + try: + category = Category.objects.get(slug=slug) + except Category.DoesNotExist: + raise CommandError("category slug '%s' does not exist" % slug) + + downloads = Download.public_objects.filter(category=category).order_by( + 'title').select_related() + + report = render_to_string('downloads/commands/category_report.html', { + 'category': category, + 'downloads': downloads, + }) + + # encode it ourselves since it can fail if you try to redirect output to + # a file and any of the content is not ASCII... + print report.encode('utf-8') + diff -r dd97341788fa -r 645025056dfa gpp/downloads/management/commands/dlwgetcat.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/downloads/management/commands/dlwgetcat.py Wed Dec 21 01:08:21 2011 +0000 @@ -0,0 +1,57 @@ +""" +dlwgetcat - a management command to produce a bash script that wgets all the +files in a given category. + +""" +import os.path + +from django.core.management.base import LabelCommand, CommandError +from django.template.loader import render_to_string +from django.template.defaultfilters import slugify +from django.contrib.sites.models import Site +from django.conf import settings + +from downloads.models import Category, Download + + +class Command(LabelCommand): + help = ("Produce on standard output a bash script that wgets all the files" + " in a category. The files are downloaded with a slugified name.") + + args = "category-slug" + + def handle_label(self, slug, **options): + """ + Render a template using the downloads in a given category and send it to + stdout. + + """ + try: + category = Category.objects.get(slug=slug) + except Category.DoesNotExist: + raise CommandError("category slug '%s' does not exist" % slug) + + downloads = Download.public_objects.filter(category=category).order_by( + 'title').select_related() + + current_site = Site.objects.get_current() + + # Create new destination names for the files since the uploaders often + # give the files terrible names. The new names will be slugified + # versions of the titles, with the same extension. + + for dl in downloads: + ext = os.path.splitext(dl.file.name)[1] + dl.dest_filename = slugify(dl.title) + ext + + # build a full URL to the download + dl.full_url = 'http://%s%s%s' % (current_site.domain, + settings.MEDIA_URL, dl.file.name) + + output = render_to_string('downloads/commands/wget_cat.html', { + 'downloads': downloads, + }) + + # encode it ourselves since it can fail if you try to redirect output to + # a file and any of the content is not ASCII... + print output.encode('utf-8') diff -r dd97341788fa -r 645025056dfa gpp/templates/downloads/commands/category_report.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/downloads/commands/category_report.html Wed Dec 21 01:08:21 2011 +0000 @@ -0,0 +1,14 @@ + + + + Download Report: {{ category.title }} + + +

Download Report: {{ category.title }}

+ {% for dl in downloads %} +

{{ dl.title }}

+

Uploaded by {{ dl.user.username }}.

+
{{ dl.html|safe }}
+ {% endfor %} + + diff -r dd97341788fa -r 645025056dfa gpp/templates/downloads/commands/wget_cat.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/downloads/commands/wget_cat.html Wed Dec 21 01:08:21 2011 +0000 @@ -0,0 +1,5 @@ +#!/bin/bash + +{% for dl in downloads %} +wget -O {{ dl.dest_filename }} {{ dl.full_url }} +{% endfor %}