Mercurial > public > sg101
comparison downloads/admin.py @ 660:0dd84cff2477
For issue #40, rename uploaded files when approved.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 14 May 2013 21:01:40 -0500 |
parents | ee87ea74d46b |
children | a75554eb6bae |
comparison
equal
deleted
inserted
replaced
659:8e6b8ffe5f34 | 660:0dd84cff2477 |
---|---|
1 """ | 1 """ |
2 This file contains the automatic admin site definitions for the downloads models. | 2 This file contains the automatic admin site definitions for the downloads models. |
3 """ | 3 """ |
4 import datetime | 4 import datetime |
5 import os.path | |
5 | 6 |
6 from django.contrib import admin | 7 from django.contrib import admin |
7 from django.conf import settings | |
8 | 8 |
9 from downloads.models import PendingDownload | 9 from downloads.models import PendingDownload |
10 from downloads.models import Download | 10 from downloads.models import Download |
11 from downloads.models import Category | 11 from downloads.models import Category |
12 from downloads.models import AllowedExtension | 12 from downloads.models import AllowedExtension |
13 from downloads.models import VoteRecord | 13 from downloads.models import VoteRecord |
14 # TODO: In Django 1.5 this will be available in django.utils.text: | |
15 from django.template.defaultfilters import slugify | |
16 | |
17 | |
18 def rename_download(dl): | |
19 """Rename the download's file to a slugified version of the title.""" | |
20 | |
21 head, tail = os.path.split(dl.file.name) | |
22 ext = os.path.splitext(tail)[1] | |
23 slug_name = slugify(dl.title) + ext | |
24 new_name = os.path.join(head, slug_name) | |
25 | |
26 head, tail = os.path.split(dl.file.path) | |
27 new_path = os.path.join(head, slug_name) | |
28 | |
29 os.rename(dl.file.path, new_path) | |
30 dl.file.name = new_name | |
14 | 31 |
15 | 32 |
16 class CategoryAdmin(admin.ModelAdmin): | 33 class CategoryAdmin(admin.ModelAdmin): |
17 list_display = ('title', 'slug', 'description', 'count') | 34 list_display = ('title', 'slug', 'description', 'count') |
18 prepopulated_fields = {'slug': ('title', )} | 35 prepopulated_fields = {'slug': ('title', )} |
28 | 45 |
29 actions = ('approve_downloads', ) | 46 actions = ('approve_downloads', ) |
30 | 47 |
31 def approve_downloads(self, request, qs): | 48 def approve_downloads(self, request, qs): |
32 for pending_dl in qs: | 49 for pending_dl in qs: |
50 # make a new Download from the existing PendingDownload | |
33 dl = Download( | 51 dl = Download( |
34 title=pending_dl.title, | 52 title=pending_dl.title, |
35 category=pending_dl.category, | 53 category=pending_dl.category, |
36 description=pending_dl.description, | 54 description=pending_dl.description, |
37 html=pending_dl.html, | 55 html=pending_dl.html, |
41 ip_address=pending_dl.ip_address, | 59 ip_address=pending_dl.ip_address, |
42 hits=0, | 60 hits=0, |
43 average_score=0.0, | 61 average_score=0.0, |
44 total_votes=0, | 62 total_votes=0, |
45 is_public=True) | 63 is_public=True) |
64 rename_download(dl) | |
46 dl.save() | 65 dl.save() |
47 | 66 |
48 # If we don't do this, the actual file will be deleted when | 67 # If we don't do this, the actual file will be deleted when |
49 # the pending download is deleted. | 68 # the pending download is deleted. |
50 pending_dl.file = None | 69 pending_dl.file = None |