view downloads/admin.py @ 693:ad69236e8501

For issue #52, update many 3rd party Javascript libraries. Updated to jquery 1.10.2, jquery ui 1.10.3. This broke a lot of stuff. - Found a newer version of the jquery cycle all plugin (3.0.3). - Updated JPlayer to 2.4.0. - Updated to MarkItUp 1.1.14. This also required me to add multiline attributes set to true on various buttons in the markdown set. - As per a stackoverflow post, added some code to get multiline titles in a jQuery UI dialog. They removed that functionality but allow you to put it back. Tweaked the MarkItUp preview CSS to show blockquotes in italic. Did not update TinyMCE at this time. I'm not using the JQuery version and this version appears to work ok for now. What I should do is make a repo for MarkItUp and do a vendor branch thing so I don't have to futz around diffing directories to figure out if I'll lose changes when I update.
author Brian Neal <bgneal@gmail.com>
date Wed, 04 Sep 2013 19:55:20 -0500
parents a75554eb6bae
children
line wrap: on
line source
"""
This file contains the automatic admin site definitions for the downloads models.
"""
import datetime
import os.path

from django.contrib import admin

from downloads.models import PendingDownload
from downloads.models import Download
from downloads.models import Category
from downloads.models import AllowedExtension
from downloads.models import VoteRecord
from django.utils.text import slugify


def rename_download(dl):
    """Rename the download's file to a slugified version of the title."""

    head, tail = os.path.split(dl.file.name)
    ext = os.path.splitext(tail)[1]
    slug_name = slugify(dl.title) + ext
    new_name = os.path.join(head, slug_name)

    head, tail = os.path.split(dl.file.path)
    new_path = os.path.join(head, slug_name)

    os.rename(dl.file.path, new_path)
    dl.file.name = new_name


class CategoryAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug', 'description', 'count')
    prepopulated_fields = {'slug': ('title', )}
    readonly_fields = ('count', )


class PendingDownloadAdmin(admin.ModelAdmin):
    exclude = ('html', )
    list_display = ('title', 'user', 'category', 'date_added', 'ip_address', 'size')
    ordering = ('date_added', )
    raw_id_fields = ('user', )
    readonly_fields = ('update_date', )

    actions = ('approve_downloads', )

    def approve_downloads(self, request, qs):
        for pending_dl in qs:
            # make a new Download from the existing PendingDownload
            dl = Download(
                    title=pending_dl.title,
                    category=pending_dl.category,
                    description=pending_dl.description,
                    html=pending_dl.html,
                    file=pending_dl.file,
                    user=pending_dl.user,
                    date_added=datetime.datetime.now(),
                    ip_address=pending_dl.ip_address,
                    hits=0,
                    average_score=0.0,
                    total_votes=0,
                    is_public=True)
            rename_download(dl)
            dl.save()

            # If we don't do this, the actual file will be deleted when
            # the pending download is deleted.
            pending_dl.file = None
            pending_dl.delete()

    approve_downloads.short_description = "Approve selected downloads"


class DownloadAdmin(admin.ModelAdmin):
    exclude = ('html', )
    list_display = ('title', 'user', 'category', 'date_added', 'ip_address',
               'hits', 'average_score', 'size', 'is_public')
    list_filter = ('date_added', 'is_public', 'category')
    list_editable = ('is_public', )
    date_hierarchy = 'date_added'
    ordering = ('-date_added', )
    search_fields = ('title', 'description', 'user__username')
    raw_id_fields = ('user', )
    readonly_fields = ('update_date', )
    save_on_top = True


class VoteRecordAdmin(admin.ModelAdmin):
    list_display = ('user', 'download', 'vote_date')
    list_filter = ('user', 'download')
    date_hierarchy = 'vote_date'


admin.site.register(PendingDownload, PendingDownloadAdmin)
admin.site.register(Download, DownloadAdmin)
admin.site.register(Category, CategoryAdmin)
admin.site.register(AllowedExtension)
admin.site.register(VoteRecord, VoteRecordAdmin)