2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models
|
2008-08-09 04:59:02 +08:00
|
|
|
from django.core.files.storage import default_storage
|
2009-01-16 23:32:31 +08:00
|
|
|
from django.contrib.auth.models import User
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2009-04-09 04:25:56 +08:00
|
|
|
class MyFileField(models.FileField):
|
|
|
|
pass
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class Member(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2009-01-16 23:32:31 +08:00
|
|
|
birthdate = models.DateTimeField(blank=True, null=True)
|
|
|
|
gender = models.CharField(max_length=1, blank=True, choices=[('M','Male'), ('F', 'Female')])
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Band(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
members = models.ManyToManyField(Member)
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Album(models.Model):
|
|
|
|
band = models.ForeignKey(Band)
|
|
|
|
name = models.CharField(max_length=100)
|
2008-08-09 07:06:28 +08:00
|
|
|
cover_art = models.FileField(upload_to='albums')
|
2009-04-09 04:25:56 +08:00
|
|
|
backside_art = MyFileField(upload_to='albums_back', null=True)
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2008-11-15 10:20:00 +08:00
|
|
|
class HiddenInventoryManager(models.Manager):
|
|
|
|
def get_query_set(self):
|
2009-01-17 05:18:21 +08:00
|
|
|
return super(HiddenInventoryManager, self).get_query_set().filter(hidden=False)
|
2008-11-15 10:20:00 +08:00
|
|
|
|
2008-09-02 06:43:38 +08:00
|
|
|
class Inventory(models.Model):
|
|
|
|
barcode = models.PositiveIntegerField(unique=True)
|
|
|
|
parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True)
|
|
|
|
name = models.CharField(blank=False, max_length=20)
|
2008-11-15 10:20:00 +08:00
|
|
|
hidden = models.BooleanField(default=False)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2008-11-15 10:20:00 +08:00
|
|
|
# see #9258
|
|
|
|
default_manager = models.Manager()
|
|
|
|
objects = HiddenInventoryManager()
|
2008-09-02 06:43:38 +08:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2009-01-16 23:32:31 +08:00
|
|
|
class Event(models.Model):
|
|
|
|
band = models.ForeignKey(Band)
|
2009-01-17 05:18:21 +08:00
|
|
|
start_date = models.DateField(blank=True, null=True)
|
2009-01-16 23:32:31 +08:00
|
|
|
start_time = models.TimeField(blank=True, null=True)
|
|
|
|
description = models.TextField(blank=True)
|
|
|
|
link = models.URLField(blank=True)
|
|
|
|
min_age = models.IntegerField(blank=True, null=True)
|
|
|
|
|
|
|
|
class Car(models.Model):
|
|
|
|
owner = models.ForeignKey(User)
|
|
|
|
make = models.CharField(max_length=30)
|
|
|
|
model = models.CharField(max_length=30)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2009-01-16 23:32:31 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s %s" % (self.make, self.model)
|
|
|
|
|
|
|
|
class CarTire(models.Model):
|
|
|
|
"""
|
|
|
|
A single car tire. This to test that a user can only select their own cars.
|
|
|
|
"""
|
|
|
|
car = models.ForeignKey(Car)
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
__test__ = {'WIDGETS_TESTS': """
|
|
|
|
>>> from datetime import datetime
|
|
|
|
>>> from django.utils.html import escape, conditional_escape
|
2008-08-10 09:07:40 +08:00
|
|
|
>>> from django.core.files.uploadedfile import SimpleUploadedFile
|
2008-07-19 07:54:34 +08:00
|
|
|
>>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
|
|
|
|
>>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget
|
|
|
|
>>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
|
2009-12-31 06:11:48 +08:00
|
|
|
>>> from django.utils.translation import activate, deactivate
|
|
|
|
>>> from django.conf import settings
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
Calling conditional_escape on the output of widget.render will simulate what
|
|
|
|
happens in the template. This is easier than setting up a template and context
|
|
|
|
for each test.
|
|
|
|
|
|
|
|
Make sure that the Admin widgets render properly, that is, without their extra
|
|
|
|
HTML escaped.
|
|
|
|
|
|
|
|
>>> w = FilteredSelectMultiple('test', False)
|
|
|
|
>>> print conditional_escape(w.render('test', 'test'))
|
2010-02-18 02:54:02 +08:00
|
|
|
<select multiple="multiple" name="test" class="selectfilter">
|
2008-07-19 07:54:34 +08:00
|
|
|
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
|
|
|
|
<BLANKLINE>
|
|
|
|
|
2010-02-18 02:54:02 +08:00
|
|
|
>>> w = FilteredSelectMultiple('test', True)
|
|
|
|
>>> print conditional_escape(w.render('test', 'test'))
|
|
|
|
<select multiple="multiple" name="test" class="selectfilterstacked">
|
|
|
|
</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>
|
|
|
|
<BLANKLINE>
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
>>> w = AdminSplitDateTime()
|
|
|
|
>>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
|
|
|
|
<p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
|
2009-12-31 06:11:48 +08:00
|
|
|
>>> activate('de-at')
|
|
|
|
>>> settings.USE_L10N = True
|
|
|
|
>>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
|
|
|
|
<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
|
|
|
|
>>> deactivate()
|
|
|
|
>>> settings.USE_L10N = False
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
>>> band = Band.objects.create(pk=1, name='Linkin Park')
|
2008-08-09 04:59:02 +08:00
|
|
|
>>> album = band.album_set.create(name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg')
|
|
|
|
|
|
|
|
>>> w = AdminFileWidget()
|
|
|
|
>>> print conditional_escape(w.render('test', album.cover_art))
|
|
|
|
Currently: <a target="_blank" href="%(STORAGE_URL)salbums/hybrid_theory.jpg">albums\hybrid_theory.jpg</a> <br />Change: <input type="file" name="test" />
|
2008-08-10 09:07:40 +08:00
|
|
|
>>> print conditional_escape(w.render('test', SimpleUploadedFile('test', 'content')))
|
|
|
|
<input type="file" name="test" />
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
>>> rel = Album._meta.get_field('band').rel
|
|
|
|
>>> w = ForeignKeyRawIdWidget(rel)
|
|
|
|
>>> print conditional_escape(w.render('test', band.pk, attrs={}))
|
2008-09-02 14:10:14 +08:00
|
|
|
<input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Linkin Park</strong>
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
>>> m1 = Member.objects.create(pk=1, name='Chester')
|
|
|
|
>>> m2 = Member.objects.create(pk=2, name='Mike')
|
|
|
|
>>> band.members.add(m1, m2)
|
|
|
|
|
|
|
|
>>> rel = Band._meta.get_field('members').rel
|
|
|
|
>>> w = ManyToManyRawIdWidget(rel)
|
|
|
|
>>> print conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={}))
|
|
|
|
<input type="text" name="test" value="1,2" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>
|
|
|
|
>>> w._has_changed(None, None)
|
|
|
|
False
|
|
|
|
>>> w._has_changed([], None)
|
|
|
|
False
|
|
|
|
>>> w._has_changed(None, [u'1'])
|
|
|
|
True
|
|
|
|
>>> w._has_changed([1, 2], [u'1', u'2'])
|
|
|
|
False
|
|
|
|
>>> w._has_changed([1, 2], [u'1'])
|
|
|
|
True
|
|
|
|
>>> w._has_changed([1, 2], [u'1', u'3'])
|
|
|
|
True
|
|
|
|
|
2008-09-02 06:43:38 +08:00
|
|
|
# Check that ForeignKeyRawIdWidget works with fields which aren't related to
|
|
|
|
# the model's primary key.
|
|
|
|
>>> apple = Inventory.objects.create(barcode=86, name='Apple')
|
|
|
|
>>> pear = Inventory.objects.create(barcode=22, name='Pear')
|
|
|
|
>>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)
|
|
|
|
>>> rel = Inventory._meta.get_field('parent').rel
|
|
|
|
>>> w = ForeignKeyRawIdWidget(rel)
|
|
|
|
>>> print w.render('test', core.parent_id, attrs={})
|
2008-09-02 14:10:14 +08:00
|
|
|
<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Apple</strong>
|
2008-11-15 10:20:00 +08:00
|
|
|
|
|
|
|
# see #9258
|
2009-01-17 05:18:21 +08:00
|
|
|
>>> hidden = Inventory.objects.create(barcode=93, name='Hidden', hidden=True)
|
2008-11-15 10:20:00 +08:00
|
|
|
>>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden)
|
2009-01-17 05:18:21 +08:00
|
|
|
>>> print w.render('test', child_of_hidden.parent_id, attrs={})
|
2008-11-15 10:20:00 +08:00
|
|
|
<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Hidden</strong>
|
2008-07-19 07:54:34 +08:00
|
|
|
""" % {
|
|
|
|
'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
|
2008-08-09 04:59:02 +08:00
|
|
|
'STORAGE_URL': default_storage.url(''),
|
2008-07-19 07:54:34 +08:00
|
|
|
}}
|