2008-12-17 01:42:18 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-04-19 04:29:55 +08:00
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
from django.core.files.storage import FileSystemStorage
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib import admin
|
2009-03-24 04:22:56 +08:00
|
|
|
from django.core.mail import EmailMessage
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
class Section(models.Model):
|
|
|
|
"""
|
2008-08-09 02:07:33 +08:00
|
|
|
A simple section that links to articles, to test linking to related items
|
2008-07-19 07:54:34 +08:00
|
|
|
in admin views.
|
|
|
|
"""
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class Article(models.Model):
|
|
|
|
"""
|
|
|
|
A simple article to test admin views. Test backwards compatibility.
|
|
|
|
"""
|
2008-08-09 02:07:33 +08:00
|
|
|
title = models.CharField(max_length=100)
|
2008-07-19 07:54:34 +08:00
|
|
|
content = models.TextField()
|
|
|
|
date = models.DateTimeField()
|
|
|
|
section = models.ForeignKey(Section)
|
|
|
|
|
2008-08-09 02:07:33 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
2009-03-10 19:19:26 +08:00
|
|
|
|
2008-10-08 22:47:01 +08:00
|
|
|
def model_year(self):
|
|
|
|
return self.date.year
|
|
|
|
model_year.admin_order_field = 'date'
|
|
|
|
|
2008-12-16 23:04:47 +08:00
|
|
|
class Book(models.Model):
|
|
|
|
"""
|
|
|
|
A simple book that has chapters.
|
|
|
|
"""
|
2008-12-17 01:42:18 +08:00
|
|
|
name = models.CharField(max_length=100, verbose_name=u'¿Name?')
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Promo(models.Model):
|
|
|
|
name = models.CharField(max_length=100, verbose_name=u'¿Name?')
|
|
|
|
book = models.ForeignKey(Book)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
2008-12-16 23:04:47 +08:00
|
|
|
|
|
|
|
class Chapter(models.Model):
|
2008-12-17 01:42:18 +08:00
|
|
|
title = models.CharField(max_length=100, verbose_name=u'¿Title?')
|
2008-12-16 23:04:47 +08:00
|
|
|
content = models.TextField()
|
|
|
|
book = models.ForeignKey(Book)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
2008-12-17 01:42:18 +08:00
|
|
|
class Meta:
|
|
|
|
verbose_name = u'¿Chapter?'
|
|
|
|
|
|
|
|
class ChapterXtra1(models.Model):
|
|
|
|
chap = models.OneToOneField(Chapter, verbose_name=u'¿Chap?')
|
2009-03-10 19:19:26 +08:00
|
|
|
xtra = models.CharField(max_length=100, verbose_name=u'¿Xtra?')
|
2008-12-17 01:42:18 +08:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u'¿Xtra1: %s' % self.xtra
|
|
|
|
|
|
|
|
class ChapterXtra2(models.Model):
|
|
|
|
chap = models.OneToOneField(Chapter, verbose_name=u'¿Chap?')
|
2009-03-10 19:19:26 +08:00
|
|
|
xtra = models.CharField(max_length=100, verbose_name=u'¿Xtra?')
|
2008-12-17 01:42:18 +08:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u'¿Xtra2: %s' % self.xtra
|
|
|
|
|
2008-10-08 22:47:01 +08:00
|
|
|
def callable_year(dt_value):
|
|
|
|
return dt_value.year
|
|
|
|
callable_year.admin_order_field = 'date'
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-08-10 04:52:40 +08:00
|
|
|
class ArticleInline(admin.TabularInline):
|
|
|
|
model = Article
|
|
|
|
|
2008-12-16 23:04:47 +08:00
|
|
|
class ChapterInline(admin.TabularInline):
|
|
|
|
model = Chapter
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class ArticleAdmin(admin.ModelAdmin):
|
2008-10-08 22:47:01 +08:00
|
|
|
list_display = ('content', 'date', callable_year, 'model_year', 'modeladmin_year')
|
2008-07-19 07:54:34 +08:00
|
|
|
list_filter = ('date',)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def changelist_view(self, request):
|
|
|
|
"Test that extra_context works"
|
|
|
|
return super(ArticleAdmin, self).changelist_view(
|
|
|
|
request, extra_context={
|
|
|
|
'extra_var': 'Hello!'
|
|
|
|
}
|
|
|
|
)
|
2009-03-10 19:19:26 +08:00
|
|
|
|
2008-10-08 22:47:01 +08:00
|
|
|
def modeladmin_year(self, obj):
|
|
|
|
return obj.date.year
|
|
|
|
modeladmin_year.admin_order_field = 'date'
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
class CustomArticle(models.Model):
|
|
|
|
content = models.TextField()
|
|
|
|
date = models.DateTimeField()
|
|
|
|
|
|
|
|
class CustomArticleAdmin(admin.ModelAdmin):
|
|
|
|
"""
|
|
|
|
Tests various hooks for using custom templates and contexts.
|
|
|
|
"""
|
|
|
|
change_list_template = 'custom_admin/change_list.html'
|
|
|
|
change_form_template = 'custom_admin/change_form.html'
|
|
|
|
object_history_template = 'custom_admin/object_history.html'
|
|
|
|
delete_confirmation_template = 'custom_admin/delete_confirmation.html'
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def changelist_view(self, request):
|
|
|
|
"Test that extra_context works"
|
|
|
|
return super(CustomArticleAdmin, self).changelist_view(
|
|
|
|
request, extra_context={
|
|
|
|
'extra_var': 'Hello!'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
class ModelWithStringPrimaryKey(models.Model):
|
|
|
|
id = models.CharField(max_length=255, primary_key=True)
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.id
|
2008-08-09 02:07:33 +08:00
|
|
|
|
2008-10-22 03:03:21 +08:00
|
|
|
class Color(models.Model):
|
|
|
|
value = models.CharField(max_length=10)
|
2009-03-10 19:19:26 +08:00
|
|
|
warm = models.BooleanField()
|
2008-10-22 03:03:21 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
class Thing(models.Model):
|
|
|
|
title = models.CharField(max_length=20)
|
|
|
|
color = models.ForeignKey(Color, limit_choices_to={'warm': True})
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
class ThingAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = ('color',)
|
|
|
|
|
2009-04-01 22:14:20 +08:00
|
|
|
class Fabric(models.Model):
|
|
|
|
NG_CHOICES = (
|
|
|
|
('Textured', (
|
|
|
|
('x', 'Horizontal'),
|
|
|
|
('y', 'Vertical'),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
('plain', 'Smooth'),
|
|
|
|
)
|
|
|
|
surface = models.CharField(max_length=20, choices=NG_CHOICES)
|
|
|
|
|
|
|
|
class FabricAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('surface',)
|
|
|
|
list_filter = ('surface',)
|
|
|
|
|
2009-03-18 04:51:47 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
GENDER_CHOICES = (
|
|
|
|
(1, "Male"),
|
|
|
|
(2, "Female"),
|
|
|
|
)
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
gender = models.IntegerField(choices=GENDER_CHOICES)
|
|
|
|
alive = models.BooleanField()
|
2009-03-30 02:52:38 +08:00
|
|
|
|
2009-03-18 04:51:47 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
2009-03-30 02:52:38 +08:00
|
|
|
|
2009-03-18 04:51:47 +08:00
|
|
|
class Meta:
|
|
|
|
ordering = ["id"]
|
|
|
|
|
|
|
|
class PersonAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('name', 'gender', 'alive')
|
|
|
|
list_editable = ('gender', 'alive')
|
|
|
|
list_filter = ('gender',)
|
2009-04-11 15:41:59 +08:00
|
|
|
search_fields = (u'name',)
|
2009-03-18 04:51:47 +08:00
|
|
|
ordering = ["id"]
|
|
|
|
|
2009-03-10 19:19:26 +08:00
|
|
|
class Persona(models.Model):
|
|
|
|
"""
|
|
|
|
A simple persona associated with accounts, to test inlining of related
|
|
|
|
accounts which inherit from a common accounts class.
|
|
|
|
"""
|
|
|
|
name = models.CharField(blank=False, max_length=80)
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Account(models.Model):
|
|
|
|
"""
|
|
|
|
A simple, generic account encapsulating the information shared by all
|
|
|
|
types of accounts.
|
|
|
|
"""
|
|
|
|
username = models.CharField(blank=False, max_length=80)
|
|
|
|
persona = models.ForeignKey(Persona, related_name="accounts")
|
|
|
|
servicename = u'generic service'
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return "%s: %s" % (self.servicename, self.username)
|
|
|
|
|
|
|
|
class FooAccount(Account):
|
|
|
|
"""A service-specific account of type Foo."""
|
|
|
|
servicename = u'foo'
|
|
|
|
|
|
|
|
class BarAccount(Account):
|
|
|
|
"""A service-specific account of type Bar."""
|
|
|
|
servicename = u'bar'
|
|
|
|
|
|
|
|
class FooAccountAdmin(admin.StackedInline):
|
|
|
|
model = FooAccount
|
|
|
|
extra = 1
|
|
|
|
|
|
|
|
class BarAccountAdmin(admin.StackedInline):
|
|
|
|
model = BarAccount
|
|
|
|
extra = 1
|
|
|
|
|
|
|
|
class PersonaAdmin(admin.ModelAdmin):
|
|
|
|
inlines = (
|
|
|
|
FooAccountAdmin,
|
|
|
|
BarAccountAdmin
|
|
|
|
)
|
|
|
|
|
2009-03-24 04:22:56 +08:00
|
|
|
class Subscriber(models.Model):
|
|
|
|
name = models.CharField(blank=False, max_length=80)
|
|
|
|
email = models.EmailField(blank=False, max_length=175)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return "%s (%s)" % (self.name, self.email)
|
|
|
|
|
|
|
|
class SubscriberAdmin(admin.ModelAdmin):
|
2009-04-07 04:23:33 +08:00
|
|
|
actions = ['mail_admin']
|
2009-03-24 04:22:56 +08:00
|
|
|
|
|
|
|
def mail_admin(self, request, selected):
|
|
|
|
EmailMessage(
|
|
|
|
'Greetings from a ModelAdmin action',
|
|
|
|
'This is the test email from a admin action',
|
|
|
|
'from@example.com',
|
|
|
|
['to@example.com']
|
|
|
|
).send()
|
|
|
|
|
|
|
|
class ExternalSubscriber(Subscriber):
|
|
|
|
pass
|
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
class OldSubscriber(Subscriber):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def external_mail(modeladmin, request, selected):
|
2009-03-24 04:22:56 +08:00
|
|
|
EmailMessage(
|
|
|
|
'Greetings from a function action',
|
|
|
|
'This is the test email from a function action',
|
|
|
|
'from@example.com',
|
|
|
|
['to@example.com']
|
|
|
|
).send()
|
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
def redirect_to(modeladmin, request, selected):
|
2009-03-24 04:22:56 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
return HttpResponseRedirect('/some-where-else/')
|
|
|
|
|
|
|
|
class ExternalSubscriberAdmin(admin.ModelAdmin):
|
|
|
|
actions = [external_mail, redirect_to]
|
2009-03-18 04:51:47 +08:00
|
|
|
|
2009-03-30 02:52:38 +08:00
|
|
|
class Media(models.Model):
|
|
|
|
name = models.CharField(max_length=60)
|
|
|
|
|
|
|
|
class Podcast(Media):
|
|
|
|
release_date = models.DateField()
|
|
|
|
|
|
|
|
class PodcastAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('name', 'release_date')
|
|
|
|
list_editable = ('release_date',)
|
|
|
|
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2009-04-19 05:03:29 +08:00
|
|
|
class Vodcast(Media):
|
|
|
|
media = models.OneToOneField(Media, primary_key=True, parent_link=True)
|
|
|
|
released = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
class VodcastAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('name', 'released')
|
|
|
|
list_editable = ('released',)
|
|
|
|
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2009-03-31 08:03:34 +08:00
|
|
|
class Parent(models.Model):
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
|
|
|
|
class Child(models.Model):
|
|
|
|
parent = models.ForeignKey(Parent, editable=False)
|
|
|
|
name = models.CharField(max_length=30, blank=True)
|
|
|
|
|
|
|
|
class ChildInline(admin.StackedInline):
|
|
|
|
model = Child
|
|
|
|
|
|
|
|
class ParentAdmin(admin.ModelAdmin):
|
|
|
|
model = Parent
|
|
|
|
inlines = [ChildInline]
|
|
|
|
|
2009-04-01 11:11:58 +08:00
|
|
|
class EmptyModel(models.Model):
|
|
|
|
def __unicode__(self):
|
|
|
|
return "Primary key = %s" % self.id
|
|
|
|
|
|
|
|
class EmptyModelAdmin(admin.ModelAdmin):
|
|
|
|
def queryset(self, request):
|
|
|
|
return super(EmptyModelAdmin, self).queryset(request).filter(pk__gt=1)
|
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
class OldSubscriberAdmin(admin.ModelAdmin):
|
|
|
|
actions = None
|
|
|
|
|
2009-04-19 04:29:55 +08:00
|
|
|
temp_storage = FileSystemStorage(tempfile.mkdtemp())
|
|
|
|
UPLOAD_TO = os.path.join(temp_storage.location, 'test_upload')
|
|
|
|
|
|
|
|
class Gallery(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class Picture(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
image = models.FileField(storage=temp_storage, upload_to='test_upload')
|
|
|
|
gallery = models.ForeignKey(Gallery, related_name="pictures")
|
|
|
|
|
|
|
|
class PictureInline(admin.TabularInline):
|
|
|
|
model = Picture
|
|
|
|
extra = 1
|
|
|
|
|
|
|
|
class GalleryAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [PictureInline]
|
|
|
|
|
|
|
|
class PictureAdmin(admin.ModelAdmin):
|
|
|
|
pass
|
|
|
|
|
2009-05-07 19:56:10 +08:00
|
|
|
|
|
|
|
class Language(models.Model):
|
|
|
|
iso = models.CharField(max_length=5, primary_key=True)
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
english_name = models.CharField(max_length=50)
|
|
|
|
shortlist = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('iso',)
|
|
|
|
|
|
|
|
class LanguageAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ['iso', 'shortlist', 'english_name', 'name']
|
|
|
|
list_editable = ['shortlist']
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
admin.site.register(Article, ArticleAdmin)
|
|
|
|
admin.site.register(CustomArticle, CustomArticleAdmin)
|
2009-04-02 08:01:15 +08:00
|
|
|
admin.site.register(Section, save_as=True, inlines=[ArticleInline])
|
2008-07-19 07:54:34 +08:00
|
|
|
admin.site.register(ModelWithStringPrimaryKey)
|
2008-10-22 03:03:21 +08:00
|
|
|
admin.site.register(Color)
|
|
|
|
admin.site.register(Thing, ThingAdmin)
|
2009-03-18 04:51:47 +08:00
|
|
|
admin.site.register(Person, PersonAdmin)
|
2009-03-10 19:19:26 +08:00
|
|
|
admin.site.register(Persona, PersonaAdmin)
|
2009-03-24 04:22:56 +08:00
|
|
|
admin.site.register(Subscriber, SubscriberAdmin)
|
|
|
|
admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin)
|
2009-04-07 04:23:33 +08:00
|
|
|
admin.site.register(OldSubscriber, OldSubscriberAdmin)
|
2009-03-30 02:52:38 +08:00
|
|
|
admin.site.register(Podcast, PodcastAdmin)
|
2009-04-19 05:03:29 +08:00
|
|
|
admin.site.register(Vodcast, VodcastAdmin)
|
2009-03-31 08:03:34 +08:00
|
|
|
admin.site.register(Parent, ParentAdmin)
|
2009-04-01 11:11:58 +08:00
|
|
|
admin.site.register(EmptyModel, EmptyModelAdmin)
|
2009-04-01 22:14:20 +08:00
|
|
|
admin.site.register(Fabric, FabricAdmin)
|
2009-04-19 04:29:55 +08:00
|
|
|
admin.site.register(Gallery, GalleryAdmin)
|
|
|
|
admin.site.register(Picture, PictureAdmin)
|
2009-05-07 19:56:10 +08:00
|
|
|
admin.site.register(Language, LanguageAdmin)
|
2008-12-17 01:42:18 +08:00
|
|
|
|
|
|
|
# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
|
|
|
|
# That way we cover all four cases:
|
|
|
|
# related ForeignKey object registered in admin
|
|
|
|
# related ForeignKey object not registered in admin
|
|
|
|
# related OneToOne object registered in admin
|
|
|
|
# related OneToOne object not registered in admin
|
|
|
|
# when deleting Book so as exercise all four troublesome (w.r.t escaping
|
|
|
|
# and calling force_unicode to avoid problems on Python 2.3) paths through
|
|
|
|
# contrib.admin.util's get_deleted_objects function.
|
|
|
|
admin.site.register(Book, inlines=[ChapterInline])
|
|
|
|
admin.site.register(Promo)
|
|
|
|
admin.site.register(ChapterXtra1)
|