annotate downloads/admin.py @ 821:71db8076dc3d

Bandmap WIP: geocoding integrated with add form. Add form works. Before submitting the form, client side JS makes a geocode request to Google and populates hidden lat/lon fields with the result. Successfully created a model instance on the server side. Still need to update admin dashboard, admin approval, and give out badges for adding bands to the map. Once that is done, then work on displaying the map with filtering.
author Brian Neal <bgneal@gmail.com>
date Tue, 23 Sep 2014 20:40:31 -0500
parents a75554eb6bae
children
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@685 14 from django.utils.text import slugify
bgneal@660 15
bgneal@660 16
bgneal@660 17 def rename_download(dl):
bgneal@660 18 """Rename the download's file to a slugified version of the title."""
bgneal@660 19
bgneal@660 20 head, tail = os.path.split(dl.file.name)
bgneal@660 21 ext = os.path.splitext(tail)[1]
bgneal@660 22 slug_name = slugify(dl.title) + ext
bgneal@660 23 new_name = os.path.join(head, slug_name)
bgneal@660 24
bgneal@660 25 head, tail = os.path.split(dl.file.path)
bgneal@660 26 new_path = os.path.join(head, slug_name)
bgneal@660 27
bgneal@660 28 os.rename(dl.file.path, new_path)
bgneal@660 29 dl.file.name = new_name
gremmie@1 30
bgneal@192 31
bgneal@192 32 class CategoryAdmin(admin.ModelAdmin):
bgneal@241 33 list_display = ('title', 'slug', 'description', 'count')
bgneal@241 34 prepopulated_fields = {'slug': ('title', )}
bgneal@192 35 readonly_fields = ('count', )
bgneal@192 36
bgneal@192 37
bgneal@204 38 class PendingDownloadAdmin(admin.ModelAdmin):
bgneal@204 39 exclude = ('html', )
bgneal@204 40 list_display = ('title', 'user', 'category', 'date_added', 'ip_address', 'size')
bgneal@204 41 ordering = ('date_added', )
bgneal@204 42 raw_id_fields = ('user', )
bgneal@277 43 readonly_fields = ('update_date', )
bgneal@204 44
bgneal@204 45 actions = ('approve_downloads', )
bgneal@204 46
bgneal@204 47 def approve_downloads(self, request, qs):
bgneal@204 48 for pending_dl in qs:
bgneal@660 49 # make a new Download from the existing PendingDownload
bgneal@204 50 dl = Download(
bgneal@204 51 title=pending_dl.title,
bgneal@204 52 category=pending_dl.category,
bgneal@204 53 description=pending_dl.description,
bgneal@204 54 html=pending_dl.html,
bgneal@204 55 file=pending_dl.file,
bgneal@204 56 user=pending_dl.user,
bgneal@204 57 date_added=datetime.datetime.now(),
bgneal@204 58 ip_address=pending_dl.ip_address,
bgneal@204 59 hits=0,
bgneal@204 60 average_score=0.0,
bgneal@204 61 total_votes=0,
bgneal@204 62 is_public=True)
bgneal@660 63 rename_download(dl)
bgneal@204 64 dl.save()
bgneal@204 65
bgneal@204 66 # If we don't do this, the actual file will be deleted when
bgneal@204 67 # the pending download is deleted.
bgneal@204 68 pending_dl.file = None
bgneal@204 69 pending_dl.delete()
bgneal@204 70
bgneal@204 71 approve_downloads.short_description = "Approve selected downloads"
bgneal@204 72
bgneal@204 73
gremmie@1 74 class DownloadAdmin(admin.ModelAdmin):
bgneal@192 75 exclude = ('html', )
bgneal@192 76 list_display = ('title', 'user', 'category', 'date_added', 'ip_address',
bgneal@192 77 'hits', 'average_score', 'size', 'is_public')
bgneal@413 78 list_filter = ('date_added', 'is_public', 'category')
bgneal@413 79 list_editable = ('is_public', )
bgneal@192 80 date_hierarchy = 'date_added'
bgneal@192 81 ordering = ('-date_added', )
bgneal@192 82 search_fields = ('title', 'description', 'user__username')
bgneal@192 83 raw_id_fields = ('user', )
bgneal@277 84 readonly_fields = ('update_date', )
bgneal@192 85 save_on_top = True
gremmie@1 86
gremmie@1 87
gremmie@1 88 class VoteRecordAdmin(admin.ModelAdmin):
bgneal@192 89 list_display = ('user', 'download', 'vote_date')
bgneal@192 90 list_filter = ('user', 'download')
bgneal@192 91 date_hierarchy = 'vote_date'
gremmie@1 92
gremmie@1 93
bgneal@204 94 admin.site.register(PendingDownload, PendingDownloadAdmin)
gremmie@1 95 admin.site.register(Download, DownloadAdmin)
bgneal@192 96 admin.site.register(Category, CategoryAdmin)
gremmie@1 97 admin.site.register(AllowedExtension)
gremmie@1 98 admin.site.register(VoteRecord, VoteRecordAdmin)
bgneal@8 99