gremmie@1: """ gremmie@1: This file contains the automatic admin site definitions for the downloads models. gremmie@1: """ bgneal@204: import datetime bgneal@660: import os.path bgneal@204: gremmie@1: from django.contrib import admin bgneal@6: bgneal@204: from downloads.models import PendingDownload gremmie@1: from downloads.models import Download gremmie@1: from downloads.models import Category gremmie@1: from downloads.models import AllowedExtension gremmie@1: from downloads.models import VoteRecord bgneal@685: from django.utils.text import slugify bgneal@660: bgneal@660: bgneal@660: def rename_download(dl): bgneal@660: """Rename the download's file to a slugified version of the title.""" bgneal@660: bgneal@660: head, tail = os.path.split(dl.file.name) bgneal@660: ext = os.path.splitext(tail)[1] bgneal@660: slug_name = slugify(dl.title) + ext bgneal@660: new_name = os.path.join(head, slug_name) bgneal@660: bgneal@660: head, tail = os.path.split(dl.file.path) bgneal@660: new_path = os.path.join(head, slug_name) bgneal@660: bgneal@660: os.rename(dl.file.path, new_path) bgneal@660: dl.file.name = new_name gremmie@1: bgneal@192: bgneal@192: class CategoryAdmin(admin.ModelAdmin): bgneal@241: list_display = ('title', 'slug', 'description', 'count') bgneal@241: prepopulated_fields = {'slug': ('title', )} bgneal@192: readonly_fields = ('count', ) bgneal@192: bgneal@192: bgneal@204: class PendingDownloadAdmin(admin.ModelAdmin): bgneal@204: exclude = ('html', ) bgneal@204: list_display = ('title', 'user', 'category', 'date_added', 'ip_address', 'size') bgneal@204: ordering = ('date_added', ) bgneal@204: raw_id_fields = ('user', ) bgneal@277: readonly_fields = ('update_date', ) bgneal@204: bgneal@204: actions = ('approve_downloads', ) bgneal@204: bgneal@204: def approve_downloads(self, request, qs): bgneal@204: for pending_dl in qs: bgneal@660: # make a new Download from the existing PendingDownload bgneal@204: dl = Download( bgneal@204: title=pending_dl.title, bgneal@204: category=pending_dl.category, bgneal@204: description=pending_dl.description, bgneal@204: html=pending_dl.html, bgneal@204: file=pending_dl.file, bgneal@204: user=pending_dl.user, bgneal@204: date_added=datetime.datetime.now(), bgneal@204: ip_address=pending_dl.ip_address, bgneal@204: hits=0, bgneal@204: average_score=0.0, bgneal@204: total_votes=0, bgneal@204: is_public=True) bgneal@660: rename_download(dl) bgneal@204: dl.save() bgneal@204: bgneal@204: # If we don't do this, the actual file will be deleted when bgneal@204: # the pending download is deleted. bgneal@204: pending_dl.file = None bgneal@204: pending_dl.delete() bgneal@204: bgneal@204: approve_downloads.short_description = "Approve selected downloads" bgneal@204: bgneal@204: gremmie@1: class DownloadAdmin(admin.ModelAdmin): bgneal@192: exclude = ('html', ) bgneal@192: list_display = ('title', 'user', 'category', 'date_added', 'ip_address', bgneal@192: 'hits', 'average_score', 'size', 'is_public') bgneal@413: list_filter = ('date_added', 'is_public', 'category') bgneal@413: list_editable = ('is_public', ) bgneal@192: date_hierarchy = 'date_added' bgneal@192: ordering = ('-date_added', ) bgneal@192: search_fields = ('title', 'description', 'user__username') bgneal@192: raw_id_fields = ('user', ) bgneal@277: readonly_fields = ('update_date', ) bgneal@192: save_on_top = True gremmie@1: gremmie@1: gremmie@1: class VoteRecordAdmin(admin.ModelAdmin): bgneal@192: list_display = ('user', 'download', 'vote_date') bgneal@192: list_filter = ('user', 'download') bgneal@192: date_hierarchy = 'vote_date' gremmie@1: gremmie@1: bgneal@204: admin.site.register(PendingDownload, PendingDownloadAdmin) gremmie@1: admin.site.register(Download, DownloadAdmin) bgneal@192: admin.site.register(Category, CategoryAdmin) gremmie@1: admin.site.register(AllowedExtension) gremmie@1: admin.site.register(VoteRecord, VoteRecordAdmin) bgneal@8: