annotate downloads/admin.py @ 666:ddc189ff96bb

For issue #46, mangle part of current time into avatar name to bust browser caches.
author Brian Neal <bgneal@gmail.com>
date Sat, 25 May 2013 00:22:40 -0500
parents 0dd84cff2477
children a75554eb6bae
rev   line source
gremmie@1 1 """
gremmie@1 2 This file contains the automatic admin site definitions for the downloads models.
gremmie@1 3 """
bgneal@204 4 import datetime
bgneal@660 5 import os.path
bgneal@204 6
gremmie@1 7 from django.contrib import admin
bgneal@6 8
bgneal@204 9 from downloads.models import PendingDownload
gremmie@1 10 from downloads.models import Download
gremmie@1 11 from downloads.models import Category
gremmie@1 12 from downloads.models import AllowedExtension
gremmie@1 13 from downloads.models import VoteRecord
bgneal@660 14 # TODO: In Django 1.5 this will be available in django.utils.text:
bgneal@660 15 from django.template.defaultfilters import slugify
bgneal@660 16
bgneal@660 17
bgneal@660 18 def rename_download(dl):
bgneal@660 19 """Rename the download's file to a slugified version of the title."""
bgneal@660 20
bgneal@660 21 head, tail = os.path.split(dl.file.name)
bgneal@660 22 ext = os.path.splitext(tail)[1]
bgneal@660 23 slug_name = slugify(dl.title) + ext
bgneal@660 24 new_name = os.path.join(head, slug_name)
bgneal@660 25
bgneal@660 26 head, tail = os.path.split(dl.file.path)
bgneal@660 27 new_path = os.path.join(head, slug_name)
bgneal@660 28
bgneal@660 29 os.rename(dl.file.path, new_path)
bgneal@660 30 dl.file.name = new_name
gremmie@1 31
bgneal@192 32
bgneal@192 33 class CategoryAdmin(admin.ModelAdmin):
bgneal@241 34 list_display = ('title', 'slug', 'description', 'count')
bgneal@241 35 prepopulated_fields = {'slug': ('title', )}
bgneal@192 36 readonly_fields = ('count', )
bgneal@192 37
bgneal@192 38
bgneal@204 39 class PendingDownloadAdmin(admin.ModelAdmin):
bgneal@204 40 exclude = ('html', )
bgneal@204 41 list_display = ('title', 'user', 'category', 'date_added', 'ip_address', 'size')
bgneal@204 42 ordering = ('date_added', )
bgneal@204 43 raw_id_fields = ('user', )
bgneal@277 44 readonly_fields = ('update_date', )
bgneal@204 45
bgneal@204 46 actions = ('approve_downloads', )
bgneal@204 47
bgneal@204 48 def approve_downloads(self, request, qs):
bgneal@204 49 for pending_dl in qs:
bgneal@660 50 # make a new Download from the existing PendingDownload
bgneal@204 51 dl = Download(
bgneal@204 52 title=pending_dl.title,
bgneal@204 53 category=pending_dl.category,
bgneal@204 54 description=pending_dl.description,
bgneal@204 55 html=pending_dl.html,
bgneal@204 56 file=pending_dl.file,
bgneal@204 57 user=pending_dl.user,
bgneal@204 58 date_added=datetime.datetime.now(),
bgneal@204 59 ip_address=pending_dl.ip_address,
bgneal@204 60 hits=0,
bgneal@204 61 average_score=0.0,
bgneal@204 62 total_votes=0,
bgneal@204 63 is_public=True)
bgneal@660 64 rename_download(dl)
bgneal@204 65 dl.save()
bgneal@204 66
bgneal@204 67 # If we don't do this, the actual file will be deleted when
bgneal@204 68 # the pending download is deleted.
bgneal@204 69 pending_dl.file = None
bgneal@204 70 pending_dl.delete()
bgneal@204 71
bgneal@204 72 approve_downloads.short_description = "Approve selected downloads"
bgneal@204 73
bgneal@204 74
gremmie@1 75 class DownloadAdmin(admin.ModelAdmin):
bgneal@192 76 exclude = ('html', )
bgneal@192 77 list_display = ('title', 'user', 'category', 'date_added', 'ip_address',
bgneal@192 78 'hits', 'average_score', 'size', 'is_public')
bgneal@413 79 list_filter = ('date_added', 'is_public', 'category')
bgneal@413 80 list_editable = ('is_public', )
bgneal@192 81 date_hierarchy = 'date_added'
bgneal@192 82 ordering = ('-date_added', )
bgneal@192 83 search_fields = ('title', 'description', 'user__username')
bgneal@192 84 raw_id_fields = ('user', )
bgneal@277 85 readonly_fields = ('update_date', )
bgneal@192 86 save_on_top = True
gremmie@1 87
gremmie@1 88
gremmie@1 89 class VoteRecordAdmin(admin.ModelAdmin):
bgneal@192 90 list_display = ('user', 'download', 'vote_date')
bgneal@192 91 list_filter = ('user', 'download')
bgneal@192 92 date_hierarchy = 'vote_date'
gremmie@1 93
gremmie@1 94
bgneal@204 95 admin.site.register(PendingDownload, PendingDownloadAdmin)
gremmie@1 96 admin.site.register(Download, DownloadAdmin)
bgneal@192 97 admin.site.register(Category, CategoryAdmin)
gremmie@1 98 admin.site.register(AllowedExtension)
gremmie@1 99 admin.site.register(VoteRecord, VoteRecordAdmin)
bgneal@8 100