2009-01-16 16:32:31 +01:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.admin import widgets
|
|
|
|
from unittest import TestCase
|
|
|
|
from django.test import TestCase as DjangoTestCase
|
|
|
|
import models
|
|
|
|
|
|
|
|
class AdminFormfieldForDBFieldTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for correct behavior of ModelAdmin.formfield_for_dbfield
|
|
|
|
"""
|
|
|
|
|
|
|
|
def assertFormfield(self, model, fieldname, widgetclass, **admin_overrides):
|
|
|
|
"""
|
|
|
|
Helper to call formfield_for_dbfield for a given model and field name
|
|
|
|
and verify that the returned formfield is appropriate.
|
|
|
|
"""
|
|
|
|
# Override any settings on the model admin
|
|
|
|
class MyModelAdmin(admin.ModelAdmin): pass
|
|
|
|
for k in admin_overrides:
|
|
|
|
setattr(MyModelAdmin, k, admin_overrides[k])
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
# Construct the admin, and ask it for a formfield
|
|
|
|
ma = MyModelAdmin(model, admin.site)
|
|
|
|
ff = ma.formfield_for_dbfield(model._meta.get_field(fieldname), request=None)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
# "unwrap" the widget wrapper, if needed
|
|
|
|
if isinstance(ff.widget, widgets.RelatedFieldWidgetWrapper):
|
|
|
|
widget = ff.widget.widget
|
|
|
|
else:
|
|
|
|
widget = ff.widget
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
# Check that we got a field of the right type
|
|
|
|
self.assert_(
|
2009-01-16 22:18:21 +01:00
|
|
|
isinstance(widget, widgetclass),
|
2009-01-16 16:32:31 +01:00
|
|
|
"Wrong widget for %s.%s: expected %s, got %s" % \
|
|
|
|
(model.__class__.__name__, fieldname, widgetclass, type(widget))
|
|
|
|
)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
# Return the formfield so that other tests can continue
|
|
|
|
return ff
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testDateField(self):
|
2009-01-16 22:18:21 +01:00
|
|
|
self.assertFormfield(models.Event, 'start_date', widgets.AdminDateWidget)
|
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testDateTimeField(self):
|
|
|
|
self.assertFormfield(models.Member, 'birthdate', widgets.AdminSplitDateTime)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testTimeField(self):
|
|
|
|
self.assertFormfield(models.Event, 'start_time', widgets.AdminTimeWidget)
|
|
|
|
|
|
|
|
def testTextField(self):
|
|
|
|
self.assertFormfield(models.Event, 'description', widgets.AdminTextareaWidget)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testURLField(self):
|
|
|
|
self.assertFormfield(models.Event, 'link', widgets.AdminURLFieldWidget)
|
|
|
|
|
|
|
|
def testIntegerField(self):
|
|
|
|
self.assertFormfield(models.Event, 'min_age', widgets.AdminIntegerFieldWidget)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testCharField(self):
|
|
|
|
self.assertFormfield(models.Member, 'name', widgets.AdminTextInputWidget)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testFileField(self):
|
|
|
|
self.assertFormfield(models.Album, 'cover_art', widgets.AdminFileWidget)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testForeignKey(self):
|
|
|
|
self.assertFormfield(models.Event, 'band', forms.Select)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testRawIDForeignKey(self):
|
|
|
|
self.assertFormfield(models.Event, 'band', widgets.ForeignKeyRawIdWidget,
|
|
|
|
raw_id_fields=['band'])
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testRadioFieldsForeignKey(self):
|
2009-01-16 22:18:21 +01:00
|
|
|
ff = self.assertFormfield(models.Event, 'band', widgets.AdminRadioSelect,
|
2009-01-16 16:32:31 +01:00
|
|
|
radio_fields={'band':admin.VERTICAL})
|
|
|
|
self.assertEqual(ff.empty_label, None)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testManyToMany(self):
|
|
|
|
self.assertFormfield(models.Band, 'members', forms.SelectMultiple)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testRawIDManyTOMany(self):
|
|
|
|
self.assertFormfield(models.Band, 'members', widgets.ManyToManyRawIdWidget,
|
|
|
|
raw_id_fields=['members'])
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testFilteredManyToMany(self):
|
|
|
|
self.assertFormfield(models.Band, 'members', widgets.FilteredSelectMultiple,
|
|
|
|
filter_vertical=['members'])
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testFormfieldOverrides(self):
|
2009-01-16 22:18:21 +01:00
|
|
|
self.assertFormfield(models.Event, 'start_date', forms.TextInput,
|
2009-01-16 16:32:31 +01:00
|
|
|
formfield_overrides={'widget': forms.TextInput})
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testFieldWithChoices(self):
|
|
|
|
self.assertFormfield(models.Member, 'gender', forms.Select)
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testChoicesWithRadioFields(self):
|
2009-01-16 22:18:21 +01:00
|
|
|
self.assertFormfield(models.Member, 'gender', widgets.AdminRadioSelect,
|
2009-01-16 16:32:31 +01:00
|
|
|
radio_fields={'gender':admin.VERTICAL})
|
|
|
|
|
|
|
|
|
|
|
|
class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
|
|
|
|
fixtures = ["admin-widgets-users.xml"]
|
2009-01-16 22:18:21 +01:00
|
|
|
|
2009-01-16 16:32:31 +01:00
|
|
|
def testFilterChoicesByRequestUser(self):
|
|
|
|
"""
|
|
|
|
Ensure the user can only see their own cars in the foreign key dropdown.
|
|
|
|
"""
|
|
|
|
self.client.login(username="super", password="secret")
|
|
|
|
response = self.client.get("/widget_admin/admin_widgets/cartire/add/")
|
|
|
|
self.assert_("BMW M3" not in response.content)
|
|
|
|
self.assert_("Volkswagon Passat" in response.content)
|