bookmedia admin: added book count and book list
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 9 Feb 2011 10:45:53 +0000 (11:45 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 9 Feb 2011 10:45:53 +0000 (11:45 +0100)
apps/catalogue/admin.py
apps/catalogue/models.py

index dd07437..3751574 100644 (file)
@@ -3,6 +3,7 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.contrib import admin
+from django import forms
 
 from newtagging.admin import TaggableModelAdmin
 from catalogue.models import Tag, Book, Fragment, BookMedia
@@ -46,13 +47,22 @@ class FragmentAdmin(TaggableModelAdmin):
     ordering = ('book', 'anchor',)
 
 
+
+class BookMediaAdminForm(forms.ModelForm):
+    books = forms.CharField(required=False)
+
+    class Meta:
+        model = BookMedia
+
+
 class MediaAdmin(admin.ModelAdmin):
-    #tag_model = BookMedia
+    form = BookMediaAdminForm
 
-    list_display = ('name', 'type', 'uploaded_at')
+    list_display = ('name', 'type', 'book_count', 'uploaded_at')
     ordering = ('name', 'type')
     search_fields = ('name',)
-    fields = ('type', 'name', 'file',)
+    fields = ('type', 'name', 'file', 'books')
+    readonly_fields = ('books',)
 
 
 
index 8411f47..b78b593 100644 (file)
@@ -171,11 +171,19 @@ def book_upload_path(ext=None, maxlen=100):
 
 class BookMedia(models.Model):
     type        = models.CharField(_('type'), choices=MEDIA_FORMATS, max_length="100")
-    name        = models.CharField(_('name'), max_length="100", blank=True)
-    file        = models.FileField(_('file'), upload_to=book_upload_path(), blank=True)
+    name        = models.CharField(_('name'), max_length="100")
+    file        = models.FileField(_('file'), upload_to=book_upload_path())
     uploaded_at = models.DateTimeField(_('creation date'), auto_now_add=True, editable=False)
     extra_info  = JSONField(_('extra information'), default='{}')
 
+    def book_count(self):
+        return self.book_set.count()
+    book_count.short_description = _('book count')
+
+    def books(self):
+        return mark_safe('<br/>'.join("<a href='%s'>%s</a>" % (reverse('admin:catalogue_book_change', args=[b.id]), b.title) for b in self.book_set.all()))
+    books.short_description = _('books')
+
     def __unicode__(self):
         return "%s (%s)" % (self.name, self.file.name.split("/")[-1])