2014-04-16 03:01:55 +08:00
|
|
|
import gettext
|
|
|
|
import os
|
2016-12-28 06:00:56 +08:00
|
|
|
import re
|
2015-01-28 20:35:27 +08:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from importlib import import_module
|
2016-10-08 09:06:49 +08:00
|
|
|
|
|
|
|
import pytz
|
2013-11-03 07:57:35 +08:00
|
|
|
|
2009-01-16 23:32:31 +08:00
|
|
|
from django import forms
|
2010-10-15 10:35:51 +08:00
|
|
|
from django.conf import settings
|
2009-01-16 23:32:31 +08:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.admin import widgets
|
2016-02-07 10:24:36 +08:00
|
|
|
from django.contrib.admin.tests import AdminSeleniumTestCase
|
2015-02-23 08:53:57 +08:00
|
|
|
from django.contrib.auth.models import User
|
2010-10-15 10:35:51 +08:00
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2020-04-13 19:04:26 +08:00
|
|
|
from django.db.models import (
|
|
|
|
CharField, DateField, DateTimeField, ManyToManyField, UUIDField,
|
|
|
|
)
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase, override_settings
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import reverse
|
2016-12-29 23:27:49 +08:00
|
|
|
from django.utils import translation
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2016-12-09 00:00:14 +08:00
|
|
|
from .models import (
|
|
|
|
Advisor, Album, Band, Bee, Car, Company, Event, Honeycomb, Individual,
|
|
|
|
Inventory, Member, MyFileField, Profile, School, Student,
|
|
|
|
)
|
2011-10-14 02:51:33 +08:00
|
|
|
from .widgetadmin import site as widget_admin_site
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestDataMixin:
|
2015-02-23 08:53:57 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2016-02-06 04:56:52 +08:00
|
|
|
cls.superuser = User.objects.create_superuser(username='super', password='secret', email=None)
|
|
|
|
cls.u2 = User.objects.create_user(username='testser', password='secret')
|
2016-12-09 00:00:14 +08:00
|
|
|
Car.objects.create(owner=cls.superuser, make='Volkswagen', model='Passat')
|
|
|
|
Car.objects.create(owner=cls.u2, make='BMW', model='M3')
|
2015-02-23 08:53:57 +08:00
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class AdminFormfieldForDBFieldTests(SimpleTestCase):
|
2009-01-16 23:32:31 +08:00
|
|
|
"""
|
|
|
|
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
|
2010-11-23 05:55:29 +08:00
|
|
|
class MyModelAdmin(admin.ModelAdmin):
|
|
|
|
pass
|
2009-01-16 23:32:31 +08:00
|
|
|
for k in admin_overrides:
|
|
|
|
setattr(MyModelAdmin, k, admin_overrides[k])
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2009-01-16 23:32:31 +08: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-17 05:18:21 +08:00
|
|
|
|
2009-01-16 23:32:31 +08:00
|
|
|
# "unwrap" the widget wrapper, if needed
|
|
|
|
if isinstance(ff.widget, widgets.RelatedFieldWidgetWrapper):
|
|
|
|
widget = ff.widget.widget
|
|
|
|
else:
|
|
|
|
widget = ff.widget
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
self.assertIsInstance(widget, widgetclass)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2009-01-16 23:32:31 +08:00
|
|
|
# Return the formfield so that other tests can continue
|
|
|
|
return ff
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_DateField(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Event, 'start_date', widgets.AdminDateWidget)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_DateTimeField(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Member, 'birthdate', widgets.AdminSplitDateTime)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_TimeField(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Event, 'start_time', widgets.AdminTimeWidget)
|
2009-01-16 23:32:31 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_TextField(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Event, 'description', widgets.AdminTextareaWidget)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_URLField(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Event, 'link', widgets.AdminURLFieldWidget)
|
2009-01-16 23:32:31 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_IntegerField(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Event, 'min_age', widgets.AdminIntegerFieldWidget)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_CharField(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Member, 'name', widgets.AdminTextInputWidget)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_EmailField(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Member, 'email', widgets.AdminEmailInputWidget)
|
2013-07-26 19:22:30 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_FileField(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Album, 'cover_art', widgets.AdminFileWidget)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_ForeignKey(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Event, 'main_band', forms.Select)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_raw_id_ForeignKey(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Event, 'main_band', widgets.ForeignKeyRawIdWidget,
|
2013-08-05 07:06:47 +08:00
|
|
|
raw_id_fields=['main_band'])
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_radio_fields_ForeignKey(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
ff = self.assertFormfield(Event, 'main_band', widgets.AdminRadioSelect,
|
2013-10-27 03:15:03 +08:00
|
|
|
radio_fields={'main_band': admin.VERTICAL})
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(ff.empty_label)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_many_to_many(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Band, 'members', forms.SelectMultiple)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_raw_id_many_to_many(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Band, 'members', widgets.ManyToManyRawIdWidget,
|
2009-01-16 23:32:31 +08:00
|
|
|
raw_id_fields=['members'])
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_filtered_many_to_many(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Band, 'members', widgets.FilteredSelectMultiple,
|
2009-01-16 23:32:31 +08:00
|
|
|
filter_vertical=['members'])
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_formfield_overrides(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Event, 'start_date', forms.TextInput,
|
2009-12-31 06:11:48 +08:00
|
|
|
formfield_overrides={DateField: {'widget': forms.TextInput}})
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_formfield_overrides_widget_instances(self):
|
2012-12-08 19:41:11 +08:00
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Widget instances in formfield_overrides are not shared between
|
2012-12-08 19:41:11 +08:00
|
|
|
different fields. (#19423)
|
|
|
|
"""
|
|
|
|
class BandAdmin(admin.ModelAdmin):
|
|
|
|
formfield_overrides = {
|
2013-10-27 03:15:03 +08:00
|
|
|
CharField: {'widget': forms.TextInput(attrs={'size': '10'})}
|
2012-12-08 19:41:11 +08:00
|
|
|
}
|
2016-12-09 00:00:14 +08:00
|
|
|
ma = BandAdmin(Band, admin.site)
|
|
|
|
f1 = ma.formfield_for_dbfield(Band._meta.get_field('name'), request=None)
|
|
|
|
f2 = ma.formfield_for_dbfield(Band._meta.get_field('style'), request=None)
|
2012-12-08 19:41:11 +08:00
|
|
|
self.assertNotEqual(f1.widget, f2.widget)
|
|
|
|
self.assertEqual(f1.widget.attrs['maxlength'], '100')
|
|
|
|
self.assertEqual(f2.widget.attrs['maxlength'], '20')
|
|
|
|
self.assertEqual(f2.widget.attrs['size'], '10')
|
|
|
|
|
2020-04-13 19:04:26 +08:00
|
|
|
def test_formfield_overrides_m2m_filter_widget(self):
|
|
|
|
"""
|
|
|
|
The autocomplete_fields, raw_id_fields, filter_vertical, and
|
|
|
|
filter_horizontal widgets for ManyToManyFields may be overridden by
|
|
|
|
specifying a widget in formfield_overrides.
|
|
|
|
"""
|
|
|
|
class BandAdmin(admin.ModelAdmin):
|
|
|
|
filter_vertical = ['members']
|
|
|
|
formfield_overrides = {
|
|
|
|
ManyToManyField: {'widget': forms.CheckboxSelectMultiple},
|
|
|
|
}
|
|
|
|
ma = BandAdmin(Band, admin.site)
|
|
|
|
field = ma.formfield_for_dbfield(Band._meta.get_field('members'), request=None)
|
|
|
|
self.assertIsInstance(field.widget.widget, forms.CheckboxSelectMultiple)
|
|
|
|
|
2016-05-03 20:33:18 +08:00
|
|
|
def test_formfield_overrides_for_datetime_field(self):
|
|
|
|
"""
|
|
|
|
Overriding the widget for DateTimeField doesn't overrides the default
|
|
|
|
form_class for that field (#26449).
|
|
|
|
"""
|
|
|
|
class MemberAdmin(admin.ModelAdmin):
|
|
|
|
formfield_overrides = {DateTimeField: {'widget': widgets.AdminSplitDateTime}}
|
2016-12-09 00:00:14 +08:00
|
|
|
ma = MemberAdmin(Member, admin.site)
|
|
|
|
f1 = ma.formfield_for_dbfield(Member._meta.get_field('birthdate'), request=None)
|
2016-05-03 20:33:18 +08:00
|
|
|
self.assertIsInstance(f1.widget, widgets.AdminSplitDateTime)
|
|
|
|
self.assertIsInstance(f1, forms.SplitDateTimeField)
|
|
|
|
|
|
|
|
def test_formfield_overrides_for_custom_field(self):
|
|
|
|
"""
|
|
|
|
formfield_overrides works for a custom field class.
|
|
|
|
"""
|
|
|
|
class AlbumAdmin(admin.ModelAdmin):
|
2016-12-09 00:00:14 +08:00
|
|
|
formfield_overrides = {MyFileField: {'widget': forms.TextInput()}}
|
|
|
|
ma = AlbumAdmin(Member, admin.site)
|
|
|
|
f1 = ma.formfield_for_dbfield(Album._meta.get_field('backside_art'), request=None)
|
2016-05-03 20:33:18 +08:00
|
|
|
self.assertIsInstance(f1.widget, forms.TextInput)
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_field_with_choices(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Member, 'gender', forms.Select)
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_choices_with_radio_fields(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Member, 'gender', widgets.AdminRadioSelect,
|
2013-10-27 03:15:03 +08:00
|
|
|
radio_fields={'gender': admin.VERTICAL})
|
2009-01-16 23:32:31 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_inheritance(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Album, 'backside_art', widgets.AdminFileWidget)
|
2009-01-16 23:32:31 +08:00
|
|
|
|
2013-05-20 03:28:33 +08:00
|
|
|
def test_m2m_widgets(self):
|
|
|
|
"""m2m fields help text as it applies to admin app (#9321)."""
|
|
|
|
class AdvisorAdmin(admin.ModelAdmin):
|
|
|
|
filter_vertical = ['companies']
|
|
|
|
|
2016-12-09 00:00:14 +08:00
|
|
|
self.assertFormfield(Advisor, 'companies', widgets.FilteredSelectMultiple,
|
2013-05-20 03:28:33 +08:00
|
|
|
filter_vertical=['companies'])
|
2016-12-09 00:00:14 +08:00
|
|
|
ma = AdvisorAdmin(Advisor, admin.site)
|
|
|
|
f = ma.formfield_for_dbfield(Advisor._meta.get_field('companies'), request=None)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
2016-12-29 23:27:49 +08:00
|
|
|
f.help_text,
|
2019-06-28 00:39:47 +08:00
|
|
|
'Hold down “Control”, or “Command” on a Mac, to select more than one.'
|
2015-09-12 07:33:12 +08:00
|
|
|
)
|
2013-05-20 03:28:33 +08:00
|
|
|
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_widgets.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class AdminFormfieldForDBFieldWithRequestTests(TestDataMixin, TestCase):
|
2009-01-17 05:18:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_filter_choices_by_request_user(self):
|
2009-01-16 23:32:31 +08:00
|
|
|
"""
|
|
|
|
Ensure the user can only see their own cars in the foreign key dropdown.
|
|
|
|
"""
|
2016-02-06 04:56:52 +08:00
|
|
|
self.client.force_login(self.superuser)
|
2015-02-07 06:25:15 +08:00
|
|
|
response = self.client.get(reverse('admin:admin_widgets_cartire_add'))
|
2012-04-14 21:35:25 +08:00
|
|
|
self.assertNotContains(response, "BMW M3")
|
2015-12-03 07:55:50 +08:00
|
|
|
self.assertContains(response, "Volkswagen Passat")
|
2009-03-31 07:16:34 +08:00
|
|
|
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_widgets.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class AdminForeignKeyWidgetChangeList(TestDataMixin, TestCase):
|
2009-04-09 04:25:56 +08:00
|
|
|
|
2009-03-31 07:16:34 +08:00
|
|
|
def setUp(self):
|
2016-02-06 04:56:52 +08:00
|
|
|
self.client.force_login(self.superuser)
|
2009-04-09 04:25:56 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_changelist_ForeignKey(self):
|
2015-02-07 06:25:15 +08:00
|
|
|
response = self.client.get(reverse('admin:admin_widgets_car_changelist'))
|
2014-01-14 23:43:27 +08:00
|
|
|
self.assertContains(response, '/auth/user/add/')
|
2010-03-02 10:28:49 +08:00
|
|
|
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_widgets.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class AdminForeignKeyRawIdWidget(TestDataMixin, TestCase):
|
2010-03-02 10:28:49 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-02-06 04:56:52 +08:00
|
|
|
self.client.force_login(self.superuser)
|
2010-03-02 10:28:49 +08:00
|
|
|
|
|
|
|
def test_nonexistent_target_id(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
band = Band.objects.create(name='Bogey Blues')
|
2010-03-02 10:28:49 +08:00
|
|
|
pk = band.pk
|
|
|
|
band.delete()
|
|
|
|
post_data = {
|
2020-05-01 22:54:15 +08:00
|
|
|
"main_band": str(pk),
|
2010-03-02 10:28:49 +08:00
|
|
|
}
|
2017-02-03 09:43:21 +08:00
|
|
|
# Try posting with a nonexistent pk in a raw id field: this
|
2010-03-02 10:28:49 +08:00
|
|
|
# should result in an error message, not a server exception.
|
2015-02-07 06:25:15 +08:00
|
|
|
response = self.client.post(reverse('admin:admin_widgets_event_add'), post_data)
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertContains(response, 'Select a valid choice. That choice is not one of the available choices.')
|
2010-09-12 02:34:40 +08:00
|
|
|
|
|
|
|
def test_invalid_target_id(self):
|
|
|
|
|
|
|
|
for test_str in ('Iñtërnâtiônàlizætiøn', "1234'", -1234):
|
|
|
|
# This should result in an error message, not a server exception.
|
2016-04-08 10:04:45 +08:00
|
|
|
response = self.client.post(reverse('admin:admin_widgets_event_add'), {"main_band": test_str})
|
2010-09-12 02:34:40 +08:00
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertContains(response, 'Select a valid choice. That choice is not one of the available choices.')
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2011-04-22 20:03:50 +08:00
|
|
|
def test_url_params_from_lookup_dict_any_iterable(self):
|
2011-08-02 07:38:11 +08:00
|
|
|
lookup1 = widgets.url_params_from_lookup_dict({'color__in': ('red', 'blue')})
|
|
|
|
lookup2 = widgets.url_params_from_lookup_dict({'color__in': ['red', 'blue']})
|
2011-04-22 20:03:50 +08:00
|
|
|
self.assertEqual(lookup1, {'color__in': 'red,blue'})
|
|
|
|
self.assertEqual(lookup1, lookup2)
|
|
|
|
|
2013-08-07 00:38:31 +08:00
|
|
|
def test_url_params_from_lookup_dict_callable(self):
|
|
|
|
def my_callable():
|
|
|
|
return 'works'
|
|
|
|
lookup1 = widgets.url_params_from_lookup_dict({'myfield': my_callable})
|
|
|
|
lookup2 = widgets.url_params_from_lookup_dict({'myfield': my_callable()})
|
|
|
|
self.assertEqual(lookup1, lookup2)
|
|
|
|
|
2018-02-01 02:43:05 +08:00
|
|
|
def test_label_and_url_for_value_invalid_uuid(self):
|
|
|
|
field = Bee._meta.get_field('honeycomb')
|
|
|
|
self.assertIsInstance(field.target_field, UUIDField)
|
|
|
|
widget = widgets.ForeignKeyRawIdWidget(field.remote_field, admin.site)
|
|
|
|
self.assertEqual(widget.label_and_url_for_value('invalid-uuid'), ('', ''))
|
|
|
|
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class FilteredSelectMultipleWidgetTest(SimpleTestCase):
|
2010-10-15 10:35:51 +08:00
|
|
|
def test_render(self):
|
2015-03-07 01:45:53 +08:00
|
|
|
# Backslash in verbose_name to ensure it is JavaScript escaped.
|
|
|
|
w = widgets.FilteredSelectMultiple('test\\', False)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', 'test'),
|
2018-01-21 00:19:06 +08:00
|
|
|
'<select multiple name="test" class="selectfilter" '
|
2015-11-23 18:46:19 +08:00
|
|
|
'data-field-name="test\\" data-is-stacked="0">\n</select>'
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_stacked_render(self):
|
2015-03-07 01:45:53 +08:00
|
|
|
# Backslash in verbose_name to ensure it is JavaScript escaped.
|
|
|
|
w = widgets.FilteredSelectMultiple('test\\', True)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', 'test'),
|
2018-01-21 00:19:06 +08:00
|
|
|
'<select multiple name="test" class="selectfilterstacked" '
|
2015-11-23 18:46:19 +08:00
|
|
|
'data-field-name="test\\" data-is-stacked="1">\n</select>'
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class AdminDateWidgetTest(SimpleTestCase):
|
2011-11-20 19:12:37 +08:00
|
|
|
def test_attrs(self):
|
|
|
|
w = widgets.AdminDateWidget()
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', datetime(2007, 12, 1, 9, 30)),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input value="2007-12-01" type="text" class="vDateField" name="test" size="10">',
|
2011-11-20 19:12:37 +08:00
|
|
|
)
|
|
|
|
# pass attrs to widget
|
|
|
|
w = widgets.AdminDateWidget(attrs={'size': 20, 'class': 'myDateField'})
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', datetime(2007, 12, 1, 9, 30)),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input value="2007-12-01" type="text" class="myDateField" name="test" size="20">',
|
2011-11-20 19:12:37 +08:00
|
|
|
)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class AdminTimeWidgetTest(SimpleTestCase):
|
2011-11-20 19:12:37 +08:00
|
|
|
def test_attrs(self):
|
|
|
|
w = widgets.AdminTimeWidget()
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', datetime(2007, 12, 1, 9, 30)),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input value="09:30:00" type="text" class="vTimeField" name="test" size="8">',
|
2011-11-20 19:12:37 +08:00
|
|
|
)
|
|
|
|
# pass attrs to widget
|
|
|
|
w = widgets.AdminTimeWidget(attrs={'size': 20, 'class': 'myTimeField'})
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', datetime(2007, 12, 1, 9, 30)),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input value="09:30:00" type="text" class="myTimeField" name="test" size="20">',
|
2011-11-20 19:12:37 +08:00
|
|
|
)
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class AdminSplitDateTimeWidgetTest(SimpleTestCase):
|
2010-10-15 10:35:51 +08:00
|
|
|
def test_render(self):
|
2011-08-02 07:38:11 +08:00
|
|
|
w = widgets.AdminSplitDateTime()
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', datetime(2007, 12, 1, 9, 30)),
|
2015-09-12 07:33:12 +08:00
|
|
|
'<p class="datetime">'
|
|
|
|
'Date: <input value="2007-12-01" type="text" class="vDateField" '
|
2018-01-21 15:09:10 +08:00
|
|
|
'name="test_0" size="10"><br>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'Time: <input value="09:30:00" type="text" class="vTimeField" '
|
2018-01-21 15:09:10 +08:00
|
|
|
'name="test_1" size="8"></p>'
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_localization(self):
|
2011-08-02 07:38:11 +08:00
|
|
|
w = widgets.AdminSplitDateTime()
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2013-08-17 02:12:10 +08:00
|
|
|
with self.settings(USE_L10N=True), translation.override('de-at'):
|
|
|
|
w.is_localized = True
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
w.render('test', datetime(2007, 12, 1, 9, 30)),
|
2015-09-12 07:33:12 +08:00
|
|
|
'<p class="datetime">'
|
|
|
|
'Datum: <input value="01.12.2007" type="text" '
|
2018-01-21 15:09:10 +08:00
|
|
|
'class="vDateField" name="test_0"size="10"><br>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'Zeit: <input value="09:30:00" type="text" class="vTimeField" '
|
2018-01-21 15:09:10 +08:00
|
|
|
'name="test_1" size="8"></p>'
|
2013-08-17 02:12:10 +08:00
|
|
|
)
|
2010-10-15 10:35:51 +08:00
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class AdminURLWidgetTest(SimpleTestCase):
|
2019-05-23 18:06:34 +08:00
|
|
|
def test_get_context_validates_url(self):
|
|
|
|
w = widgets.AdminURLFieldWidget()
|
|
|
|
for invalid in ['', '/not/a/full/url/', 'javascript:alert("Danger XSS!")']:
|
|
|
|
with self.subTest(url=invalid):
|
|
|
|
self.assertFalse(w.get_context('name', invalid, {})['url_valid'])
|
|
|
|
self.assertTrue(w.get_context('name', 'http://example.com', {})['url_valid'])
|
|
|
|
|
2012-07-07 20:24:50 +08:00
|
|
|
def test_render(self):
|
|
|
|
w = widgets.AdminURLFieldWidget()
|
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', ''),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input class="vURLField" name="test" type="url">'
|
2012-07-07 20:24:50 +08:00
|
|
|
)
|
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', 'http://example.com'),
|
2015-09-12 07:33:12 +08:00
|
|
|
'<p class="url">Currently:<a href="http://example.com">'
|
2018-01-21 15:09:10 +08:00
|
|
|
'http://example.com</a><br>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'Change:<input class="vURLField" name="test" type="url" '
|
2018-01-21 15:09:10 +08:00
|
|
|
'value="http://example.com"></p>'
|
2012-07-07 20:24:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_render_idn(self):
|
|
|
|
w = widgets.AdminURLFieldWidget()
|
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', 'http://example-äüö.com'),
|
2015-09-12 07:33:12 +08:00
|
|
|
'<p class="url">Currently: <a href="http://xn--example--7za4pnc.com">'
|
2018-01-21 15:09:10 +08:00
|
|
|
'http://example-äüö.com</a><br>'
|
2015-09-12 07:33:12 +08:00
|
|
|
'Change:<input class="vURLField" name="test" type="url" '
|
2018-01-21 15:09:10 +08:00
|
|
|
'value="http://example-äüö.com"></p>'
|
2012-07-07 20:24:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_render_quoting(self):
|
2016-12-28 06:00:56 +08:00
|
|
|
"""
|
|
|
|
WARNING: This test doesn't use assertHTMLEqual since it will get rid
|
|
|
|
of some escapes which are tested here!
|
|
|
|
"""
|
|
|
|
HREF_RE = re.compile('href="([^"]+)"')
|
|
|
|
VALUE_RE = re.compile('value="([^"]+)"')
|
|
|
|
TEXT_RE = re.compile('<a[^>]+>([^>]+)</a>')
|
2012-07-07 20:24:50 +08:00
|
|
|
w = widgets.AdminURLFieldWidget()
|
2019-05-23 18:06:34 +08:00
|
|
|
output = w.render('test', 'http://example.com/<sometag>some-text</sometag>')
|
2016-12-28 06:00:56 +08:00
|
|
|
self.assertEqual(
|
|
|
|
HREF_RE.search(output).groups()[0],
|
2019-05-23 18:06:34 +08:00
|
|
|
'http://example.com/%3Csometag%3Esome-text%3C/sometag%3E',
|
2016-12-28 06:00:56 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
TEXT_RE.search(output).groups()[0],
|
2019-05-23 18:06:34 +08:00
|
|
|
'http://example.com/<sometag>some-text</sometag>',
|
2016-12-28 06:00:56 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
VALUE_RE.search(output).groups()[0],
|
2019-05-23 18:06:34 +08:00
|
|
|
'http://example.com/<sometag>some-text</sometag>',
|
2016-12-28 06:00:56 +08:00
|
|
|
)
|
2019-05-23 18:06:34 +08:00
|
|
|
output = w.render('test', 'http://example-äüö.com/<sometag>some-text</sometag>')
|
2016-12-28 06:00:56 +08:00
|
|
|
self.assertEqual(
|
|
|
|
HREF_RE.search(output).groups()[0],
|
2019-05-23 18:06:34 +08:00
|
|
|
'http://xn--example--7za4pnc.com/%3Csometag%3Esome-text%3C/sometag%3E',
|
2016-12-28 06:00:56 +08:00
|
|
|
)
|
2013-08-14 00:06:41 +08:00
|
|
|
self.assertEqual(
|
2016-12-28 06:00:56 +08:00
|
|
|
TEXT_RE.search(output).groups()[0],
|
2019-05-23 18:06:34 +08:00
|
|
|
'http://example-äüö.com/<sometag>some-text</sometag>',
|
2012-07-07 20:24:50 +08:00
|
|
|
)
|
2013-08-14 00:06:41 +08:00
|
|
|
self.assertEqual(
|
2016-12-28 06:00:56 +08:00
|
|
|
VALUE_RE.search(output).groups()[0],
|
2019-05-23 18:06:34 +08:00
|
|
|
'http://example-äüö.com/<sometag>some-text</sometag>',
|
2013-08-14 00:06:41 +08:00
|
|
|
)
|
2016-12-28 06:00:56 +08:00
|
|
|
output = w.render('test', 'http://www.example.com/%C3%A4"><script>alert("XSS!")</script>"')
|
2013-08-14 00:06:41 +08:00
|
|
|
self.assertEqual(
|
2016-12-28 06:00:56 +08:00
|
|
|
HREF_RE.search(output).groups()[0],
|
|
|
|
'http://www.example.com/%C3%A4%22%3E%3Cscript%3Ealert(%22XSS!%22)%3C/script%3E%22',
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
TEXT_RE.search(output).groups()[0],
|
2015-09-12 07:33:12 +08:00
|
|
|
'http://www.example.com/%C3%A4"><script>'
|
2016-12-28 06:00:56 +08:00
|
|
|
'alert("XSS!")</script>"'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
VALUE_RE.search(output).groups()[0],
|
|
|
|
'http://www.example.com/%C3%A4"><script>alert("XSS!")</script>"',
|
2012-07-07 20:24:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-08-18 22:16:22 +08:00
|
|
|
class AdminUUIDWidgetTests(SimpleTestCase):
|
|
|
|
def test_attrs(self):
|
|
|
|
w = widgets.AdminUUIDInputWidget()
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
w.render('test', '550e8400-e29b-41d4-a716-446655440000'),
|
|
|
|
'<input value="550e8400-e29b-41d4-a716-446655440000" type="text" class="vUUIDField" name="test">',
|
|
|
|
)
|
|
|
|
w = widgets.AdminUUIDInputWidget(attrs={'class': 'myUUIDInput'})
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
w.render('test', '550e8400-e29b-41d4-a716-446655440000'),
|
|
|
|
'<input value="550e8400-e29b-41d4-a716-446655440000" type="text" class="myUUIDInput" name="test">',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_widgets.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class AdminFileWidgetTests(TestDataMixin, TestCase):
|
2014-04-05 23:58:05 +08:00
|
|
|
|
2015-02-23 08:53:57 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpTestData()
|
2016-12-09 00:00:14 +08:00
|
|
|
band = Band.objects.create(name='Linkin Park')
|
2015-02-23 08:53:57 +08:00
|
|
|
cls.album = band.album_set.create(
|
2010-10-15 10:35:51 +08:00
|
|
|
name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
|
|
|
|
)
|
|
|
|
|
2014-04-05 23:58:05 +08:00
|
|
|
def test_render(self):
|
2011-08-02 07:38:11 +08:00
|
|
|
w = widgets.AdminFileWidget()
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2014-04-05 23:58:05 +08:00
|
|
|
w.render('test', self.album.cover_art),
|
|
|
|
'<p class="file-upload">Currently: <a href="%(STORAGE_URL)salbums/'
|
2016-09-17 00:15:00 +08:00
|
|
|
r'hybrid_theory.jpg">albums\hybrid_theory.jpg</a> '
|
2014-04-05 23:58:05 +08:00
|
|
|
'<span class="clearable-file-input">'
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="checkbox" name="test-clear" id="test-clear_id"> '
|
|
|
|
'<label for="test-clear_id">Clear</label></span><br>'
|
|
|
|
'Change: <input type="file" name="test"></p>' % {
|
2014-04-05 23:58:05 +08:00
|
|
|
'STORAGE_URL': default_storage.url(''),
|
2013-10-15 03:13:14 +08:00
|
|
|
},
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('test', SimpleUploadedFile('test', b'content')),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="file" name="test">',
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
|
|
|
|
2017-06-06 04:17:10 +08:00
|
|
|
def test_render_required(self):
|
|
|
|
widget = widgets.AdminFileWidget()
|
|
|
|
widget.is_required = True
|
|
|
|
self.assertHTMLEqual(
|
|
|
|
widget.render('test', self.album.cover_art),
|
|
|
|
'<p class="file-upload">Currently: <a href="%(STORAGE_URL)salbums/'
|
2018-01-21 15:09:10 +08:00
|
|
|
r'hybrid_theory.jpg">albums\hybrid_theory.jpg</a><br>'
|
|
|
|
'Change: <input type="file" name="test"></p>' % {
|
2017-06-06 04:17:10 +08:00
|
|
|
'STORAGE_URL': default_storage.url(''),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2014-04-05 23:58:05 +08:00
|
|
|
def test_readonly_fields(self):
|
|
|
|
"""
|
|
|
|
File widgets should render as a link when they're marked "read only."
|
|
|
|
"""
|
2016-02-06 04:56:52 +08:00
|
|
|
self.client.force_login(self.superuser)
|
2015-02-07 06:25:15 +08:00
|
|
|
response = self.client.get(reverse('admin:admin_widgets_album_change', args=(self.album.id,)))
|
2014-04-05 23:58:05 +08:00
|
|
|
self.assertContains(
|
|
|
|
response,
|
2016-11-05 19:47:12 +08:00
|
|
|
'<div class="readonly"><a href="%(STORAGE_URL)salbums/hybrid_theory.jpg">'
|
|
|
|
r'albums\hybrid_theory.jpg</a></div>' % {'STORAGE_URL': default_storage.url('')},
|
2014-04-05 23:58:05 +08:00
|
|
|
html=True,
|
|
|
|
)
|
|
|
|
self.assertNotContains(
|
|
|
|
response,
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="file" name="cover_art" id="id_cover_art">',
|
2014-04-05 23:58:05 +08:00
|
|
|
html=True,
|
|
|
|
)
|
2015-02-12 00:40:03 +08:00
|
|
|
response = self.client.get(reverse('admin:admin_widgets_album_add'))
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
2016-11-05 19:47:12 +08:00
|
|
|
'<div class="readonly"></div>',
|
2015-02-12 00:40:03 +08:00
|
|
|
html=True,
|
|
|
|
)
|
2014-04-05 23:58:05 +08:00
|
|
|
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_widgets.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class ForeignKeyRawIdWidgetTest(TestCase):
|
2014-01-14 23:43:27 +08:00
|
|
|
|
2010-10-15 10:35:51 +08:00
|
|
|
def test_render(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
band = Band.objects.create(name='Linkin Park')
|
2010-10-15 10:35:51 +08:00
|
|
|
band.album_set.create(
|
|
|
|
name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg'
|
|
|
|
)
|
2016-12-09 00:00:14 +08:00
|
|
|
rel = Album._meta.get_field('band').remote_field
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2011-08-02 07:38:11 +08:00
|
|
|
w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
w.render('test', band.pk, attrs={}),
|
|
|
|
'<input type="text" name="test" value="%(bandpk)s" '
|
2018-01-21 15:09:10 +08:00
|
|
|
'class="vForeignKeyRawIdAdminField">'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<a href="/admin_widgets/band/?_to_field=id" class="related-lookup" '
|
2016-01-28 08:43:04 +08:00
|
|
|
'id="lookup_id_test" title="Lookup"></a> <strong>'
|
|
|
|
'<a href="/admin_widgets/band/%(bandpk)s/change/">Linkin Park</a>'
|
|
|
|
'</strong>' % {'bandpk': band.pk}
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_relations_to_non_primary_key(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# ForeignKeyRawIdWidget works with fields which aren't related to
|
|
|
|
# the model's primary key.
|
2016-12-09 00:00:14 +08:00
|
|
|
apple = Inventory.objects.create(barcode=86, name='Apple')
|
|
|
|
Inventory.objects.create(barcode=22, name='Pear')
|
|
|
|
core = Inventory.objects.create(
|
2010-10-15 10:35:51 +08:00
|
|
|
barcode=87, name='Core', parent=apple
|
|
|
|
)
|
2016-12-09 00:00:14 +08:00
|
|
|
rel = Inventory._meta.get_field('parent').remote_field
|
2011-08-02 07:38:11 +08:00
|
|
|
w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
w.render('test', core.parent_id, attrs={}),
|
|
|
|
'<input type="text" name="test" value="86" '
|
2018-01-21 15:09:10 +08:00
|
|
|
'class="vForeignKeyRawIdAdminField">'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<a href="/admin_widgets/inventory/?_to_field=barcode" '
|
|
|
|
'class="related-lookup" id="lookup_id_test" title="Lookup"></a>'
|
2016-01-28 08:43:04 +08:00
|
|
|
' <strong><a href="/admin_widgets/inventory/%(pk)s/change/">'
|
|
|
|
'Apple</a></strong>' % {'pk': apple.pk}
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
|
|
|
|
2011-08-02 07:38:11 +08:00
|
|
|
def test_fk_related_model_not_in_admin(self):
|
2011-08-12 22:14:15 +08:00
|
|
|
# FK to a model not registered with admin site. Raw ID widget should
|
2011-08-02 07:38:11 +08:00
|
|
|
# have no magnifying glass link. See #16542
|
2016-12-09 00:00:14 +08:00
|
|
|
big_honeycomb = Honeycomb.objects.create(location='Old tree')
|
2011-08-02 07:38:11 +08:00
|
|
|
big_honeycomb.bee_set.create()
|
2016-12-09 00:00:14 +08:00
|
|
|
rel = Bee._meta.get_field('honeycomb').remote_field
|
2011-08-02 07:38:11 +08:00
|
|
|
|
|
|
|
w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('honeycomb_widget', big_honeycomb.pk, attrs={}),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="honeycomb_widget" value="%(hcombpk)s">'
|
2017-06-10 00:36:09 +08:00
|
|
|
' <strong>%(hcomb)s</strong>'
|
|
|
|
% {'hcombpk': big_honeycomb.pk, 'hcomb': big_honeycomb}
|
2011-08-02 07:38:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_fk_to_self_model_not_in_admin(self):
|
2011-08-12 22:14:15 +08:00
|
|
|
# FK to self, not registered with admin site. Raw ID widget should have
|
2011-08-02 07:38:11 +08:00
|
|
|
# no magnifying glass link. See #16542
|
2016-12-09 00:00:14 +08:00
|
|
|
subject1 = Individual.objects.create(name='Subject #1')
|
|
|
|
Individual.objects.create(name='Child', parent=subject1)
|
|
|
|
rel = Individual._meta.get_field('parent').remote_field
|
2011-08-02 07:38:11 +08:00
|
|
|
|
|
|
|
w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('individual_widget', subject1.pk, attrs={}),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="individual_widget" value="%(subj1pk)s">'
|
2017-06-10 00:36:09 +08:00
|
|
|
' <strong>%(subj1)s</strong>'
|
|
|
|
% {'subj1pk': subject1.pk, 'subj1': subject1}
|
2011-08-02 07:38:11 +08:00
|
|
|
)
|
2010-10-15 10:35:51 +08:00
|
|
|
|
|
|
|
def test_proper_manager_for_label_lookup(self):
|
|
|
|
# see #9258
|
2016-12-09 00:00:14 +08:00
|
|
|
rel = Inventory._meta.get_field('parent').remote_field
|
2011-08-02 07:38:11 +08:00
|
|
|
w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site)
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2016-12-09 00:00:14 +08:00
|
|
|
hidden = Inventory.objects.create(
|
2010-10-15 10:35:51 +08:00
|
|
|
barcode=93, name='Hidden', hidden=True
|
|
|
|
)
|
2016-12-09 00:00:14 +08:00
|
|
|
child_of_hidden = Inventory.objects.create(
|
2010-10-15 10:35:51 +08:00
|
|
|
barcode=94, name='Child of hidden', parent=hidden
|
|
|
|
)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
w.render('test', child_of_hidden.parent_id, attrs={}),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField">'
|
2015-09-12 07:33:12 +08:00
|
|
|
'<a href="/admin_widgets/inventory/?_to_field=barcode" '
|
|
|
|
'class="related-lookup" id="lookup_id_test" title="Lookup"></a>'
|
2016-01-28 08:43:04 +08:00
|
|
|
' <strong><a href="/admin_widgets/inventory/%(pk)s/change/">'
|
|
|
|
'Hidden</a></strong>' % {'pk': hidden.pk}
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_widgets.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class ManyToManyRawIdWidgetTest(TestCase):
|
2014-01-14 23:43:27 +08:00
|
|
|
|
2010-10-15 10:35:51 +08:00
|
|
|
def test_render(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
band = Band.objects.create(name='Linkin Park')
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2016-12-09 00:00:14 +08:00
|
|
|
m1 = Member.objects.create(name='Chester')
|
|
|
|
m2 = Member.objects.create(name='Mike')
|
2010-10-15 10:35:51 +08:00
|
|
|
band.members.add(m1, m2)
|
2016-12-09 00:00:14 +08:00
|
|
|
rel = Band._meta.get_field('members').remote_field
|
2010-10-15 10:35:51 +08:00
|
|
|
|
2011-08-02 07:38:11 +08:00
|
|
|
w = widgets.ManyToManyRawIdWidget(rel, widget_admin_site)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2014-06-24 21:07:36 +08:00
|
|
|
w.render('test', [m1.pk, m2.pk], attrs={}), (
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="test" value="%(m1pk)s,%(m2pk)s" class="vManyToManyRawIdAdminField">'
|
2014-06-24 21:07:36 +08:00
|
|
|
'<a href="/admin_widgets/member/" class="related-lookup" id="lookup_id_test" title="Lookup"></a>'
|
2017-06-02 07:08:59 +08:00
|
|
|
) % {'m1pk': m1.pk, 'm2pk': m2.pk}
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
|
|
|
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2014-06-24 21:07:36 +08:00
|
|
|
w.render('test', [m1.pk]), (
|
|
|
|
'<input type="text" name="test" value="%(m1pk)s" class="vManyToManyRawIdAdminField">'
|
|
|
|
'<a href="/admin_widgets/member/" class="related-lookup" id="lookup_id_test" title="Lookup"></a>'
|
2017-06-02 07:08:59 +08:00
|
|
|
) % {'m1pk': m1.pk}
|
2010-10-15 10:35:51 +08:00
|
|
|
)
|
|
|
|
|
2011-08-02 07:38:11 +08:00
|
|
|
def test_m2m_related_model_not_in_admin(self):
|
|
|
|
# M2M relationship with model not registered with admin site. Raw ID
|
2011-08-12 22:14:15 +08:00
|
|
|
# widget should have no magnifying glass link. See #16542
|
2016-12-09 00:00:14 +08:00
|
|
|
consultor1 = Advisor.objects.create(name='Rockstar Techie')
|
2011-08-02 07:38:11 +08:00
|
|
|
|
2016-12-09 00:00:14 +08:00
|
|
|
c1 = Company.objects.create(name='Doodle')
|
|
|
|
c2 = Company.objects.create(name='Pear')
|
2011-08-02 07:38:11 +08:00
|
|
|
consultor1.companies.add(c1, c2)
|
2016-12-09 00:00:14 +08:00
|
|
|
rel = Advisor._meta.get_field('companies').remote_field
|
2011-08-02 07:38:11 +08:00
|
|
|
|
|
|
|
w = widgets.ManyToManyRawIdWidget(rel, widget_admin_site)
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('company_widget1', [c1.pk, c2.pk], attrs={}),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="company_widget1" value="%(c1pk)s,%(c2pk)s">' % {'c1pk': c1.pk, 'c2pk': c2.pk}
|
2011-08-02 07:38:11 +08:00
|
|
|
)
|
|
|
|
|
2012-02-01 04:36:11 +08:00
|
|
|
self.assertHTMLEqual(
|
2013-08-14 04:22:46 +08:00
|
|
|
w.render('company_widget2', [c1.pk]),
|
2018-01-21 15:09:10 +08:00
|
|
|
'<input type="text" name="company_widget2" value="%(c1pk)s">' % {'c1pk': c1.pk}
|
2011-08-02 07:38:11 +08:00
|
|
|
)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2017-02-20 21:48:03 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_widgets.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class RelatedFieldWidgetWrapperTests(SimpleTestCase):
|
2010-11-23 05:55:29 +08:00
|
|
|
def test_no_can_add_related(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
rel = Individual._meta.get_field('parent').remote_field
|
2011-08-02 07:38:11 +08:00
|
|
|
w = widgets.AdminRadioSelect()
|
2010-11-23 05:55:29 +08:00
|
|
|
# Used to fail with a name error.
|
2011-08-02 07:38:11 +08:00
|
|
|
w = widgets.RelatedFieldWidgetWrapper(w, rel, widget_admin_site)
|
2010-11-23 05:55:29 +08:00
|
|
|
self.assertFalse(w.can_add_related)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2013-11-18 06:26:20 +08:00
|
|
|
def test_select_multiple_widget_cant_change_delete_related(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
rel = Individual._meta.get_field('parent').remote_field
|
2013-11-18 06:26:20 +08:00
|
|
|
widget = forms.SelectMultiple()
|
|
|
|
wrapper = widgets.RelatedFieldWidgetWrapper(
|
|
|
|
widget, rel, widget_admin_site,
|
|
|
|
can_add_related=True,
|
|
|
|
can_change_related=True,
|
|
|
|
can_delete_related=True,
|
|
|
|
)
|
|
|
|
self.assertTrue(wrapper.can_add_related)
|
|
|
|
self.assertFalse(wrapper.can_change_related)
|
|
|
|
self.assertFalse(wrapper.can_delete_related)
|
|
|
|
|
|
|
|
def test_on_delete_cascade_rel_cant_delete_related(self):
|
2016-12-09 00:00:14 +08:00
|
|
|
rel = Individual._meta.get_field('soulmate').remote_field
|
2013-11-18 06:26:20 +08:00
|
|
|
widget = forms.Select()
|
|
|
|
wrapper = widgets.RelatedFieldWidgetWrapper(
|
|
|
|
widget, rel, widget_admin_site,
|
|
|
|
can_add_related=True,
|
|
|
|
can_change_related=True,
|
|
|
|
can_delete_related=True,
|
|
|
|
)
|
|
|
|
self.assertTrue(wrapper.can_add_related)
|
|
|
|
self.assertTrue(wrapper.can_change_related)
|
|
|
|
self.assertFalse(wrapper.can_delete_related)
|
|
|
|
|
2017-02-20 21:48:03 +08:00
|
|
|
def test_custom_widget_render(self):
|
|
|
|
class CustomWidget(forms.Select):
|
|
|
|
def render(self, *args, **kwargs):
|
|
|
|
return 'custom render output'
|
|
|
|
rel = Album._meta.get_field('band').remote_field
|
|
|
|
widget = CustomWidget()
|
|
|
|
wrapper = widgets.RelatedFieldWidgetWrapper(
|
|
|
|
widget, rel, widget_admin_site,
|
|
|
|
can_add_related=True,
|
|
|
|
can_change_related=True,
|
|
|
|
can_delete_related=True,
|
|
|
|
)
|
|
|
|
output = wrapper.render('name', 'value')
|
|
|
|
self.assertIn('custom render output', output)
|
|
|
|
|
2017-03-08 02:56:29 +08:00
|
|
|
def test_widget_delegates_value_omitted_from_data(self):
|
|
|
|
class CustomWidget(forms.Select):
|
|
|
|
def value_omitted_from_data(self, data, files, name):
|
|
|
|
return False
|
|
|
|
rel = Album._meta.get_field('band').remote_field
|
|
|
|
widget = CustomWidget()
|
|
|
|
wrapper = widgets.RelatedFieldWidgetWrapper(widget, rel, widget_admin_site)
|
|
|
|
self.assertIs(wrapper.value_omitted_from_data({}, {}, 'band'), False)
|
|
|
|
|
2018-11-20 02:11:04 +08:00
|
|
|
def test_widget_is_hidden(self):
|
|
|
|
rel = Album._meta.get_field('band').remote_field
|
|
|
|
widget = forms.HiddenInput()
|
|
|
|
widget.choices = ()
|
|
|
|
wrapper = widgets.RelatedFieldWidgetWrapper(widget, rel, widget_admin_site)
|
|
|
|
self.assertIs(wrapper.is_hidden, True)
|
|
|
|
context = wrapper.get_context('band', None, {})
|
|
|
|
self.assertIs(context['is_hidden'], True)
|
|
|
|
output = wrapper.render('name', 'value')
|
|
|
|
# Related item links are hidden.
|
|
|
|
self.assertNotIn('<a ', output)
|
|
|
|
|
|
|
|
def test_widget_is_not_hidden(self):
|
|
|
|
rel = Album._meta.get_field('band').remote_field
|
|
|
|
widget = forms.Select()
|
|
|
|
wrapper = widgets.RelatedFieldWidgetWrapper(widget, rel, widget_admin_site)
|
|
|
|
self.assertIs(wrapper.is_hidden, False)
|
|
|
|
context = wrapper.get_context('band', None, {})
|
|
|
|
self.assertIs(context['is_hidden'], False)
|
|
|
|
output = wrapper.render('name', 'value')
|
|
|
|
# Related item links are present.
|
|
|
|
self.assertIn('<a ', output)
|
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='admin_widgets.urls')
|
2016-02-07 10:24:36 +08:00
|
|
|
class AdminWidgetSeleniumTestCase(AdminSeleniumTestCase):
|
|
|
|
|
|
|
|
available_apps = ['admin_widgets'] + AdminSeleniumTestCase.available_apps
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.u1 = User.objects.create_superuser(username='super', password='secret', email='super@example.com')
|
2013-06-10 18:11:46 +08:00
|
|
|
|
2016-02-07 10:24:36 +08:00
|
|
|
|
|
|
|
class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase):
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
def test_show_hide_date_time_picker_widgets(self):
|
|
|
|
"""
|
2017-08-26 23:58:42 +08:00
|
|
|
Pressing the ESC key or clicking on a widget value closes the date and
|
|
|
|
time picker widgets.
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
"""
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
|
|
|
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
|
|
|
# Open a page that has a date and time picker widgets
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
# First, with the date picker widget ---------------------------------
|
2017-08-26 23:58:42 +08:00
|
|
|
cal_icon = self.selenium.find_element_by_id('calendarlink0')
|
2016-10-27 15:53:39 +08:00
|
|
|
# The date picker is hidden
|
2019-11-28 22:10:13 +08:00
|
|
|
self.assertFalse(self.selenium.find_element_by_id('calendarbox0').is_displayed())
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
# Click the calendar icon
|
2017-08-26 23:58:42 +08:00
|
|
|
cal_icon.click()
|
2016-10-27 15:53:39 +08:00
|
|
|
# The date picker is visible
|
2019-11-28 22:10:13 +08:00
|
|
|
self.assertTrue(self.selenium.find_element_by_id('calendarbox0').is_displayed())
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
# Press the ESC key
|
2012-02-22 15:14:10 +08:00
|
|
|
self.selenium.find_element_by_tag_name('body').send_keys([Keys.ESCAPE])
|
2016-10-27 15:53:39 +08:00
|
|
|
# The date picker is hidden again
|
2019-11-28 22:10:13 +08:00
|
|
|
self.assertFalse(self.selenium.find_element_by_id('calendarbox0').is_displayed())
|
2017-08-26 23:58:42 +08:00
|
|
|
# Click the calendar icon, then on the 15th of current month
|
|
|
|
cal_icon.click()
|
|
|
|
self.selenium.find_element_by_xpath("//a[contains(text(), '15')]").click()
|
2019-11-28 22:10:13 +08:00
|
|
|
self.assertFalse(self.selenium.find_element_by_id('calendarbox0').is_displayed())
|
2017-08-26 23:58:42 +08:00
|
|
|
self.assertEqual(
|
|
|
|
self.selenium.find_element_by_id('id_birthdate_0').get_attribute('value'),
|
|
|
|
datetime.today().strftime('%Y-%m-') + '15',
|
|
|
|
)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
|
|
|
|
# Then, with the time picker widget ----------------------------------
|
2017-08-26 23:58:42 +08:00
|
|
|
time_icon = self.selenium.find_element_by_id('clocklink0')
|
2016-10-27 15:53:39 +08:00
|
|
|
# The time picker is hidden
|
2019-11-28 22:10:13 +08:00
|
|
|
self.assertFalse(self.selenium.find_element_by_id('clockbox0').is_displayed())
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
# Click the time icon
|
2017-08-26 23:58:42 +08:00
|
|
|
time_icon.click()
|
2016-10-27 15:53:39 +08:00
|
|
|
# The time picker is visible
|
2019-11-28 22:10:13 +08:00
|
|
|
self.assertTrue(self.selenium.find_element_by_id('clockbox0').is_displayed())
|
2015-06-21 07:47:15 +08:00
|
|
|
self.assertEqual(
|
|
|
|
[
|
|
|
|
x.text for x in
|
|
|
|
self.selenium.find_elements_by_xpath("//ul[@class='timelist']/li/a")
|
|
|
|
],
|
|
|
|
['Now', 'Midnight', '6 a.m.', 'Noon', '6 p.m.']
|
|
|
|
)
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
# Press the ESC key
|
2012-02-22 15:14:10 +08:00
|
|
|
self.selenium.find_element_by_tag_name('body').send_keys([Keys.ESCAPE])
|
2016-10-27 15:53:39 +08:00
|
|
|
# The time picker is hidden again
|
2019-11-28 22:10:13 +08:00
|
|
|
self.assertFalse(self.selenium.find_element_by_id('clockbox0').is_displayed())
|
2017-08-26 23:58:42 +08:00
|
|
|
# Click the time icon, then select the 'Noon' value
|
|
|
|
time_icon.click()
|
|
|
|
self.selenium.find_element_by_xpath("//a[contains(text(), 'Noon')]").click()
|
2019-11-28 22:10:13 +08:00
|
|
|
self.assertFalse(self.selenium.find_element_by_id('clockbox0').is_displayed())
|
2017-08-26 23:58:42 +08:00
|
|
|
self.assertEqual(
|
|
|
|
self.selenium.find_element_by_id('id_birthdate_1').get_attribute('value'),
|
|
|
|
'12:00:00',
|
|
|
|
)
|
2012-02-22 15:14:10 +08:00
|
|
|
|
2013-09-08 05:12:13 +08:00
|
|
|
def test_calendar_nonday_class(self):
|
|
|
|
"""
|
|
|
|
Ensure cells that are not days of the month have the `nonday` CSS class.
|
|
|
|
Refs #4574.
|
|
|
|
"""
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
|
|
|
# Open a page that has a date and time picker widgets
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
|
2013-09-08 05:12:13 +08:00
|
|
|
|
|
|
|
# fill in the birth date.
|
|
|
|
self.selenium.find_element_by_id('id_birthdate_0').send_keys('2013-06-01')
|
|
|
|
|
|
|
|
# Click the calendar icon
|
|
|
|
self.selenium.find_element_by_id('calendarlink0').click()
|
|
|
|
|
|
|
|
# get all the tds within the calendar
|
|
|
|
calendar0 = self.selenium.find_element_by_id('calendarin0')
|
|
|
|
tds = calendar0.find_elements_by_tag_name('td')
|
|
|
|
|
|
|
|
# make sure the first and last 6 cells have class nonday
|
|
|
|
for td in tds[:6] + tds[-6:]:
|
|
|
|
self.assertEqual(td.get_attribute('class'), 'nonday')
|
|
|
|
|
|
|
|
def test_calendar_selected_class(self):
|
|
|
|
"""
|
|
|
|
Ensure cell for the day in the input has the `selected` CSS class.
|
|
|
|
Refs #4574.
|
|
|
|
"""
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
|
|
|
# Open a page that has a date and time picker widgets
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
|
2013-09-08 05:12:13 +08:00
|
|
|
|
|
|
|
# fill in the birth date.
|
|
|
|
self.selenium.find_element_by_id('id_birthdate_0').send_keys('2013-06-01')
|
|
|
|
|
|
|
|
# Click the calendar icon
|
|
|
|
self.selenium.find_element_by_id('calendarlink0').click()
|
|
|
|
|
|
|
|
# get all the tds within the calendar
|
|
|
|
calendar0 = self.selenium.find_element_by_id('calendarin0')
|
|
|
|
tds = calendar0.find_elements_by_tag_name('td')
|
|
|
|
|
|
|
|
# verify the selected cell
|
|
|
|
selected = tds[6]
|
|
|
|
self.assertEqual(selected.get_attribute('class'), 'selected')
|
|
|
|
|
|
|
|
self.assertEqual(selected.text, '1')
|
|
|
|
|
|
|
|
def test_calendar_no_selected_class(self):
|
|
|
|
"""
|
|
|
|
Ensure no cells are given the selected class when the field is empty.
|
|
|
|
Refs #4574.
|
|
|
|
"""
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
|
|
|
# Open a page that has a date and time picker widgets
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
|
2013-09-08 05:12:13 +08:00
|
|
|
|
|
|
|
# Click the calendar icon
|
|
|
|
self.selenium.find_element_by_id('calendarlink0').click()
|
|
|
|
|
|
|
|
# get all the tds within the calendar
|
|
|
|
calendar0 = self.selenium.find_element_by_id('calendarin0')
|
|
|
|
tds = calendar0.find_elements_by_tag_name('td')
|
|
|
|
|
|
|
|
# verify there are no cells with the selected class
|
|
|
|
selected = [td for td in tds if td.get_attribute('class') == 'selected']
|
|
|
|
|
|
|
|
self.assertEqual(len(selected), 0)
|
|
|
|
|
2014-04-16 03:01:55 +08:00
|
|
|
def test_calendar_show_date_from_input(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The calendar shows the date from the input field for every locale
|
|
|
|
supported by Django.
|
2014-04-16 03:01:55 +08:00
|
|
|
"""
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
|
|
|
|
|
|
|
# Enter test data
|
2016-12-09 00:00:14 +08:00
|
|
|
member = Member.objects.create(name='Bob', birthdate=datetime(1984, 5, 15), gender='M')
|
2014-04-16 03:01:55 +08:00
|
|
|
|
2015-11-28 03:08:08 +08:00
|
|
|
# Get month name translations for every locale
|
|
|
|
month_string = 'May'
|
2014-04-16 03:01:55 +08:00
|
|
|
path = os.path.join(os.path.dirname(import_module('django.contrib.admin').__file__), 'locale')
|
|
|
|
for language_code, language_name in settings.LANGUAGES:
|
|
|
|
try:
|
|
|
|
catalog = gettext.translation('djangojs', path, [language_code])
|
2019-01-28 23:01:35 +08:00
|
|
|
except OSError:
|
2014-04-16 03:01:55 +08:00
|
|
|
continue
|
|
|
|
if month_string in catalog._catalog:
|
2015-11-28 03:08:08 +08:00
|
|
|
month_name = catalog._catalog[month_string]
|
2014-04-16 03:01:55 +08:00
|
|
|
else:
|
2015-11-28 03:08:08 +08:00
|
|
|
month_name = month_string
|
2014-04-16 03:01:55 +08:00
|
|
|
|
|
|
|
# Get the expected caption
|
2015-11-28 03:08:08 +08:00
|
|
|
may_translation = month_name
|
2019-11-19 15:29:47 +08:00
|
|
|
expected_caption = '{:s} {:d}'.format(may_translation.upper(), 1984)
|
2014-04-16 03:01:55 +08:00
|
|
|
|
|
|
|
# Test with every locale
|
|
|
|
with override_settings(LANGUAGE_CODE=language_code, USE_L10N=True):
|
|
|
|
|
|
|
|
# Open a page that has a date picker widget
|
2016-04-08 10:04:45 +08:00
|
|
|
url = reverse('admin:admin_widgets_member_change', args=(member.pk,))
|
|
|
|
self.selenium.get(self.live_server_url + url)
|
2014-04-16 03:01:55 +08:00
|
|
|
# Click on the calendar icon
|
|
|
|
self.selenium.find_element_by_id('calendarlink0').click()
|
|
|
|
# Make sure that the right month and year are displayed
|
2015-08-20 08:43:27 +08:00
|
|
|
self.wait_for_text('#calendarin0 caption', expected_caption)
|
2014-04-16 03:01:55 +08:00
|
|
|
|
2013-09-08 05:12:13 +08:00
|
|
|
|
2016-02-07 10:24:36 +08:00
|
|
|
@override_settings(TIME_ZONE='Asia/Singapore')
|
|
|
|
class DateTimePickerShortcutsSeleniumTests(AdminWidgetSeleniumTestCase):
|
2013-07-01 22:48:14 +08:00
|
|
|
|
|
|
|
def test_date_time_picker_shortcuts(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
date/time/datetime picker shortcuts work in the current time zone.
|
2013-07-01 22:48:14 +08:00
|
|
|
Refs #20663.
|
|
|
|
|
|
|
|
This test case is fairly tricky, it relies on selenium still running the browser
|
|
|
|
in the default time zone "America/Chicago" despite `override_settings` changing
|
|
|
|
the time zone to "Asia/Singapore".
|
|
|
|
"""
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
|
|
|
|
|
|
|
error_margin = timedelta(seconds=10)
|
|
|
|
|
2013-11-03 07:57:35 +08:00
|
|
|
# If we are neighbouring a DST, we add an hour of error margin.
|
|
|
|
tz = pytz.timezone('America/Chicago')
|
|
|
|
utc_now = datetime.now(pytz.utc)
|
|
|
|
tz_yesterday = (utc_now - timedelta(days=1)).astimezone(tz).tzname()
|
|
|
|
tz_tomorrow = (utc_now + timedelta(days=1)).astimezone(tz).tzname()
|
|
|
|
if tz_yesterday != tz_tomorrow:
|
|
|
|
error_margin += timedelta(hours=1)
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_member_add'))
|
2013-07-01 22:48:14 +08:00
|
|
|
|
|
|
|
self.selenium.find_element_by_id('id_name').send_keys('test')
|
|
|
|
|
|
|
|
# Click on the "today" and "now" shortcuts.
|
2016-04-08 10:04:45 +08:00
|
|
|
shortcuts = self.selenium.find_elements_by_css_selector('.field-birthdate .datetimeshortcuts')
|
2013-07-01 22:48:14 +08:00
|
|
|
|
2020-04-20 17:37:15 +08:00
|
|
|
now = datetime.now()
|
2013-07-01 22:48:14 +08:00
|
|
|
for shortcut in shortcuts:
|
|
|
|
shortcut.find_element_by_tag_name('a').click()
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
# There is a time zone mismatch warning.
|
2013-07-01 22:48:14 +08:00
|
|
|
# Warning: This would effectively fail if the TIME_ZONE defined in the
|
|
|
|
# settings has the same UTC offset as "Asia/Singapore" because the
|
|
|
|
# mismatch warning would be rightfully missing from the page.
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.find_elements_by_css_selector('.field-birthdate .timezonewarning')
|
2013-07-01 22:48:14 +08:00
|
|
|
|
|
|
|
# Submit the form.
|
2019-11-27 21:36:26 +08:00
|
|
|
with self.wait_page_loaded():
|
2019-11-28 16:24:19 +08:00
|
|
|
self.selenium.find_element_by_name('_save').click()
|
2013-07-01 22:48:14 +08:00
|
|
|
|
|
|
|
# Make sure that "now" in javascript is within 10 seconds
|
|
|
|
# from "now" on the server side.
|
2016-12-09 00:00:14 +08:00
|
|
|
member = Member.objects.get(name='test')
|
2013-07-01 22:48:14 +08:00
|
|
|
self.assertGreater(member.birthdate, now - error_margin)
|
|
|
|
self.assertLess(member.birthdate, now + error_margin)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2015-09-16 01:46:03 +08:00
|
|
|
# The above tests run with Asia/Singapore which are on the positive side of
|
|
|
|
# UTC. Here we test with a timezone on the negative side.
|
|
|
|
@override_settings(TIME_ZONE='US/Eastern')
|
2016-02-07 10:24:36 +08:00
|
|
|
class DateTimePickerAltTimezoneSeleniumTests(DateTimePickerShortcutsSeleniumTests):
|
2015-09-16 01:46:03 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-02-07 10:24:36 +08:00
|
|
|
class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
|
2012-02-23 16:07:07 +08:00
|
|
|
|
2015-03-05 21:21:29 +08:00
|
|
|
def setUp(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUp()
|
2016-12-09 00:00:14 +08:00
|
|
|
self.lisa = Student.objects.create(name='Lisa')
|
|
|
|
self.john = Student.objects.create(name='John')
|
|
|
|
self.bob = Student.objects.create(name='Bob')
|
|
|
|
self.peter = Student.objects.create(name='Peter')
|
|
|
|
self.jenny = Student.objects.create(name='Jenny')
|
|
|
|
self.jason = Student.objects.create(name='Jason')
|
|
|
|
self.cliff = Student.objects.create(name='Cliff')
|
|
|
|
self.arthur = Student.objects.create(name='Arthur')
|
|
|
|
self.school = School.objects.create(name='School of Awesome')
|
2012-02-23 16:07:07 +08:00
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
def assertActiveButtons(self, mode, field_name, choose, remove, choose_all=None, remove_all=None):
|
2012-03-04 09:20:03 +08:00
|
|
|
choose_link = '#id_%s_add_link' % field_name
|
|
|
|
choose_all_link = '#id_%s_add_all_link' % field_name
|
|
|
|
remove_link = '#id_%s_remove_link' % field_name
|
|
|
|
remove_all_link = '#id_%s_remove_all_link' % field_name
|
|
|
|
self.assertEqual(self.has_css_class(choose_link, 'active'), choose)
|
|
|
|
self.assertEqual(self.has_css_class(remove_link, 'active'), remove)
|
|
|
|
if mode == 'horizontal':
|
|
|
|
self.assertEqual(self.has_css_class(choose_all_link, 'active'), choose_all)
|
|
|
|
self.assertEqual(self.has_css_class(remove_all_link, 'active'), remove_all)
|
|
|
|
|
|
|
|
def execute_basic_operations(self, mode, field_name):
|
2016-05-13 19:36:41 +08:00
|
|
|
original_url = self.selenium.current_url
|
|
|
|
|
2012-02-23 16:07:07 +08:00
|
|
|
from_box = '#id_%s_from' % field_name
|
|
|
|
to_box = '#id_%s_to' % field_name
|
|
|
|
choose_link = 'id_%s_add_link' % field_name
|
|
|
|
choose_all_link = 'id_%s_add_all_link' % field_name
|
|
|
|
remove_link = 'id_%s_remove_link' % field_name
|
|
|
|
remove_all_link = 'id_%s_remove_all_link' % field_name
|
|
|
|
|
|
|
|
# Initial positions ---------------------------------------------------
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(from_box, [
|
|
|
|
str(self.arthur.id), str(self.bob.id),
|
|
|
|
str(self.cliff.id), str(self.jason.id),
|
|
|
|
str(self.jenny.id), str(self.john.id),
|
|
|
|
])
|
|
|
|
self.assertSelectOptions(to_box, [str(self.lisa.id), str(self.peter.id)])
|
2012-03-04 09:20:03 +08:00
|
|
|
self.assertActiveButtons(mode, field_name, False, False, True, True)
|
2012-02-23 16:07:07 +08:00
|
|
|
|
|
|
|
# Click 'Choose all' --------------------------------------------------
|
|
|
|
if mode == 'horizontal':
|
|
|
|
self.selenium.find_element_by_id(choose_all_link).click()
|
|
|
|
elif mode == 'vertical':
|
|
|
|
# There 's no 'Choose all' button in vertical mode, so individually
|
|
|
|
# select all options and click 'Choose'.
|
2012-03-05 16:13:09 +08:00
|
|
|
for option in self.selenium.find_elements_by_css_selector(from_box + ' > option'):
|
2012-02-23 16:07:07 +08:00
|
|
|
option.click()
|
|
|
|
self.selenium.find_element_by_id(choose_link).click()
|
|
|
|
self.assertSelectOptions(from_box, [])
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(to_box, [
|
|
|
|
str(self.lisa.id), str(self.peter.id),
|
|
|
|
str(self.arthur.id), str(self.bob.id),
|
|
|
|
str(self.cliff.id), str(self.jason.id),
|
|
|
|
str(self.jenny.id), str(self.john.id),
|
|
|
|
])
|
2012-03-04 09:20:03 +08:00
|
|
|
self.assertActiveButtons(mode, field_name, False, False, False, True)
|
2012-02-23 16:07:07 +08:00
|
|
|
|
|
|
|
# Click 'Remove all' --------------------------------------------------
|
|
|
|
if mode == 'horizontal':
|
|
|
|
self.selenium.find_element_by_id(remove_all_link).click()
|
|
|
|
elif mode == 'vertical':
|
|
|
|
# There 's no 'Remove all' button in vertical mode, so individually
|
|
|
|
# select all options and click 'Remove'.
|
2012-03-05 16:13:09 +08:00
|
|
|
for option in self.selenium.find_elements_by_css_selector(to_box + ' > option'):
|
2012-02-23 16:07:07 +08:00
|
|
|
option.click()
|
|
|
|
self.selenium.find_element_by_id(remove_link).click()
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(from_box, [
|
|
|
|
str(self.lisa.id), str(self.peter.id),
|
|
|
|
str(self.arthur.id), str(self.bob.id),
|
|
|
|
str(self.cliff.id), str(self.jason.id),
|
|
|
|
str(self.jenny.id), str(self.john.id),
|
|
|
|
])
|
2012-02-23 16:07:07 +08:00
|
|
|
self.assertSelectOptions(to_box, [])
|
2012-03-04 09:20:03 +08:00
|
|
|
self.assertActiveButtons(mode, field_name, False, False, True, False)
|
2012-02-23 16:07:07 +08:00
|
|
|
|
|
|
|
# Choose some options ------------------------------------------------
|
2019-11-27 23:34:07 +08:00
|
|
|
from_lisa_select_option = self.selenium.find_element_by_css_selector(
|
|
|
|
'{} > option[value="{}"]'.format(from_box, self.lisa.id)
|
|
|
|
)
|
2013-09-07 04:46:38 +08:00
|
|
|
|
|
|
|
# Check the title attribute is there for tool tips: ticket #20821
|
|
|
|
self.assertEqual(from_lisa_select_option.get_attribute('title'), from_lisa_select_option.get_attribute('text'))
|
|
|
|
|
2019-11-27 23:34:07 +08:00
|
|
|
self.select_option(from_box, str(self.lisa.id))
|
|
|
|
self.select_option(from_box, str(self.jason.id))
|
|
|
|
self.select_option(from_box, str(self.bob.id))
|
|
|
|
self.select_option(from_box, str(self.john.id))
|
2012-03-04 09:20:03 +08:00
|
|
|
self.assertActiveButtons(mode, field_name, True, False, True, False)
|
2012-02-23 16:07:07 +08:00
|
|
|
self.selenium.find_element_by_id(choose_link).click()
|
2012-03-04 09:20:03 +08:00
|
|
|
self.assertActiveButtons(mode, field_name, False, False, True, True)
|
2012-02-23 16:07:07 +08:00
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(from_box, [
|
|
|
|
str(self.peter.id), str(self.arthur.id),
|
|
|
|
str(self.cliff.id), str(self.jenny.id),
|
|
|
|
])
|
|
|
|
self.assertSelectOptions(to_box, [
|
|
|
|
str(self.lisa.id), str(self.bob.id),
|
|
|
|
str(self.jason.id), str(self.john.id),
|
|
|
|
])
|
2012-02-23 16:07:07 +08:00
|
|
|
|
2013-09-07 04:46:38 +08:00
|
|
|
# Check the tooltip is still there after moving: ticket #20821
|
2019-11-27 23:34:07 +08:00
|
|
|
to_lisa_select_option = self.selenium.find_element_by_css_selector(
|
|
|
|
'{} > option[value="{}"]'.format(to_box, self.lisa.id)
|
|
|
|
)
|
2013-09-07 04:46:38 +08:00
|
|
|
self.assertEqual(to_lisa_select_option.get_attribute('title'), to_lisa_select_option.get_attribute('text'))
|
|
|
|
|
2012-02-23 16:07:07 +08:00
|
|
|
# Remove some options -------------------------------------------------
|
2019-11-27 23:34:07 +08:00
|
|
|
self.select_option(to_box, str(self.lisa.id))
|
|
|
|
self.select_option(to_box, str(self.bob.id))
|
2012-03-04 09:20:03 +08:00
|
|
|
self.assertActiveButtons(mode, field_name, False, True, True, True)
|
2012-02-23 16:07:07 +08:00
|
|
|
self.selenium.find_element_by_id(remove_link).click()
|
2012-03-04 09:20:03 +08:00
|
|
|
self.assertActiveButtons(mode, field_name, False, False, True, True)
|
2012-02-23 16:07:07 +08:00
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(from_box, [
|
|
|
|
str(self.peter.id), str(self.arthur.id),
|
|
|
|
str(self.cliff.id), str(self.jenny.id),
|
|
|
|
str(self.lisa.id), str(self.bob.id)
|
|
|
|
])
|
|
|
|
self.assertSelectOptions(to_box, [str(self.jason.id), str(self.john.id)])
|
2012-02-23 16:07:07 +08:00
|
|
|
|
|
|
|
# Choose some more options --------------------------------------------
|
2019-11-27 23:34:07 +08:00
|
|
|
self.select_option(from_box, str(self.arthur.id))
|
|
|
|
self.select_option(from_box, str(self.cliff.id))
|
2012-02-23 16:07:07 +08:00
|
|
|
self.selenium.find_element_by_id(choose_link).click()
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(from_box, [
|
|
|
|
str(self.peter.id), str(self.jenny.id),
|
|
|
|
str(self.lisa.id), str(self.bob.id),
|
|
|
|
])
|
|
|
|
self.assertSelectOptions(to_box, [
|
|
|
|
str(self.jason.id), str(self.john.id),
|
|
|
|
str(self.arthur.id), str(self.cliff.id),
|
|
|
|
])
|
2012-02-23 16:07:07 +08:00
|
|
|
|
2016-05-04 01:09:07 +08:00
|
|
|
# Choose some more options --------------------------------------------
|
2019-11-27 23:34:07 +08:00
|
|
|
self.select_option(from_box, str(self.peter.id))
|
|
|
|
self.select_option(from_box, str(self.lisa.id))
|
2016-05-04 01:09:07 +08:00
|
|
|
|
|
|
|
# Confirm they're selected after clicking inactive buttons: ticket #26575
|
|
|
|
self.assertSelectedOptions(from_box, [str(self.peter.id), str(self.lisa.id)])
|
|
|
|
self.selenium.find_element_by_id(remove_link).click()
|
|
|
|
self.assertSelectedOptions(from_box, [str(self.peter.id), str(self.lisa.id)])
|
|
|
|
|
|
|
|
# Unselect the options ------------------------------------------------
|
2019-11-27 23:34:07 +08:00
|
|
|
self.deselect_option(from_box, str(self.peter.id))
|
|
|
|
self.deselect_option(from_box, str(self.lisa.id))
|
2016-05-04 01:09:07 +08:00
|
|
|
|
|
|
|
# Choose some more options --------------------------------------------
|
2019-11-27 23:34:07 +08:00
|
|
|
self.select_option(to_box, str(self.jason.id))
|
|
|
|
self.select_option(to_box, str(self.john.id))
|
2016-05-04 01:09:07 +08:00
|
|
|
|
|
|
|
# Confirm they're selected after clicking inactive buttons: ticket #26575
|
|
|
|
self.assertSelectedOptions(to_box, [str(self.jason.id), str(self.john.id)])
|
|
|
|
self.selenium.find_element_by_id(choose_link).click()
|
|
|
|
self.assertSelectedOptions(to_box, [str(self.jason.id), str(self.john.id)])
|
|
|
|
|
|
|
|
# Unselect the options ------------------------------------------------
|
2019-11-27 23:34:07 +08:00
|
|
|
self.deselect_option(to_box, str(self.jason.id))
|
|
|
|
self.deselect_option(to_box, str(self.john.id))
|
2016-05-04 01:09:07 +08:00
|
|
|
|
2016-05-13 19:36:41 +08:00
|
|
|
# Pressing buttons shouldn't change the URL.
|
|
|
|
self.assertEqual(self.selenium.current_url, original_url)
|
|
|
|
|
2012-02-23 16:07:07 +08:00
|
|
|
def test_basic(self):
|
2015-10-31 09:16:56 +08:00
|
|
|
self.school.students.set([self.lisa, self.peter])
|
|
|
|
self.school.alumni.set([self.lisa, self.peter])
|
2012-02-23 16:07:07 +08:00
|
|
|
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_school_change', args=(self.school.id,)))
|
2012-02-23 16:07:07 +08:00
|
|
|
|
2019-11-27 21:36:26 +08:00
|
|
|
self.wait_page_ready()
|
2012-03-04 09:20:03 +08:00
|
|
|
self.execute_basic_operations('vertical', 'students')
|
|
|
|
self.execute_basic_operations('horizontal', 'alumni')
|
2012-02-23 16:07:07 +08:00
|
|
|
|
|
|
|
# Save and check that everything is properly stored in the database ---
|
|
|
|
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
|
2019-11-27 21:36:26 +08:00
|
|
|
self.wait_page_ready()
|
2016-12-09 00:00:14 +08:00
|
|
|
self.school = School.objects.get(id=self.school.id) # Reload from database
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual(list(self.school.students.all()), [self.arthur, self.cliff, self.jason, self.john])
|
|
|
|
self.assertEqual(list(self.school.alumni.all()), [self.arthur, self.cliff, self.jason, self.john])
|
2012-02-23 16:07:07 +08:00
|
|
|
|
2012-03-04 09:20:03 +08:00
|
|
|
def test_filter(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Typing in the search box filters out options displayed in the 'from'
|
|
|
|
box.
|
2012-03-04 09:20:03 +08:00
|
|
|
"""
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
|
|
|
|
2015-10-31 09:16:56 +08:00
|
|
|
self.school.students.set([self.lisa, self.peter])
|
|
|
|
self.school.alumni.set([self.lisa, self.peter])
|
2012-03-04 09:20:03 +08:00
|
|
|
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_school_change', args=(self.school.id,)))
|
2012-03-04 09:20:03 +08:00
|
|
|
|
|
|
|
for field_name in ['students', 'alumni']:
|
|
|
|
from_box = '#id_%s_from' % field_name
|
|
|
|
to_box = '#id_%s_to' % field_name
|
2016-01-11 22:16:23 +08:00
|
|
|
choose_link = 'id_%s_add_link' % field_name
|
|
|
|
remove_link = 'id_%s_remove_link' % field_name
|
|
|
|
input = self.selenium.find_element_by_id('id_%s_input' % field_name)
|
2012-03-04 09:20:03 +08:00
|
|
|
|
|
|
|
# Initial values
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(from_box, [
|
|
|
|
str(self.arthur.id), str(self.bob.id),
|
|
|
|
str(self.cliff.id), str(self.jason.id),
|
|
|
|
str(self.jenny.id), str(self.john.id),
|
|
|
|
])
|
2012-03-04 09:20:03 +08:00
|
|
|
|
|
|
|
# Typing in some characters filters out non-matching options
|
|
|
|
input.send_keys('a')
|
|
|
|
self.assertSelectOptions(from_box, [str(self.arthur.id), str(self.jason.id)])
|
|
|
|
input.send_keys('R')
|
|
|
|
self.assertSelectOptions(from_box, [str(self.arthur.id)])
|
|
|
|
|
|
|
|
# Clearing the text box makes the other options reappear
|
|
|
|
input.send_keys([Keys.BACK_SPACE])
|
|
|
|
self.assertSelectOptions(from_box, [str(self.arthur.id), str(self.jason.id)])
|
|
|
|
input.send_keys([Keys.BACK_SPACE])
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(from_box, [
|
|
|
|
str(self.arthur.id), str(self.bob.id),
|
|
|
|
str(self.cliff.id), str(self.jason.id),
|
|
|
|
str(self.jenny.id), str(self.john.id),
|
|
|
|
])
|
2012-03-04 09:20:03 +08:00
|
|
|
|
2012-03-04 12:50:58 +08:00
|
|
|
# -----------------------------------------------------------------
|
2016-10-27 15:53:39 +08:00
|
|
|
# Choosing a filtered option sends it properly to the 'to' box.
|
2012-03-04 09:20:03 +08:00
|
|
|
input.send_keys('a')
|
|
|
|
self.assertSelectOptions(from_box, [str(self.arthur.id), str(self.jason.id)])
|
2019-11-27 23:34:07 +08:00
|
|
|
self.select_option(from_box, str(self.jason.id))
|
2016-01-11 22:16:23 +08:00
|
|
|
self.selenium.find_element_by_id(choose_link).click()
|
2012-03-04 09:20:03 +08:00
|
|
|
self.assertSelectOptions(from_box, [str(self.arthur.id)])
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(to_box, [
|
|
|
|
str(self.lisa.id), str(self.peter.id), str(self.jason.id),
|
|
|
|
])
|
2012-03-04 12:50:58 +08:00
|
|
|
|
2019-11-27 23:34:07 +08:00
|
|
|
self.select_option(to_box, str(self.lisa.id))
|
2016-01-11 22:16:23 +08:00
|
|
|
self.selenium.find_element_by_id(remove_link).click()
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(from_box, [str(self.arthur.id), str(self.lisa.id)])
|
|
|
|
self.assertSelectOptions(to_box, [str(self.peter.id), str(self.jason.id)])
|
2012-03-04 12:50:58 +08:00
|
|
|
|
2013-11-03 05:02:56 +08:00
|
|
|
input.send_keys([Keys.BACK_SPACE]) # Clear text box
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertSelectOptions(from_box, [
|
|
|
|
str(self.arthur.id), str(self.bob.id),
|
|
|
|
str(self.cliff.id), str(self.jenny.id),
|
|
|
|
str(self.john.id), str(self.lisa.id),
|
|
|
|
])
|
|
|
|
self.assertSelectOptions(to_box, [str(self.peter.id), str(self.jason.id)])
|
2012-03-04 12:50:58 +08:00
|
|
|
|
2014-08-09 02:10:09 +08:00
|
|
|
# -----------------------------------------------------------------
|
2016-10-27 15:53:39 +08:00
|
|
|
# Pressing enter on a filtered option sends it properly to
|
|
|
|
# the 'to' box.
|
2019-11-27 23:34:07 +08:00
|
|
|
self.select_option(to_box, str(self.jason.id))
|
2016-01-11 22:16:23 +08:00
|
|
|
self.selenium.find_element_by_id(remove_link).click()
|
2014-08-09 02:10:09 +08:00
|
|
|
input.send_keys('ja')
|
|
|
|
self.assertSelectOptions(from_box, [str(self.jason.id)])
|
|
|
|
input.send_keys([Keys.ENTER])
|
|
|
|
self.assertSelectOptions(to_box, [str(self.peter.id), str(self.jason.id)])
|
|
|
|
input.send_keys([Keys.BACK_SPACE, Keys.BACK_SPACE])
|
|
|
|
|
2012-03-04 12:50:58 +08:00
|
|
|
# Save and check that everything is properly stored in the database ---
|
2019-11-27 21:36:26 +08:00
|
|
|
with self.wait_page_loaded():
|
|
|
|
self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
|
2016-12-09 00:00:14 +08:00
|
|
|
self.school = School.objects.get(id=self.school.id) # Reload from database
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual(list(self.school.students.all()), [self.jason, self.peter])
|
|
|
|
self.assertEqual(list(self.school.alumni.all()), [self.jason, self.peter])
|
2012-03-04 09:20:03 +08:00
|
|
|
|
2015-12-30 23:09:43 +08:00
|
|
|
def test_back_button_bug(self):
|
|
|
|
"""
|
|
|
|
Some browsers had a bug where navigating away from the change page
|
|
|
|
and then clicking the browser's back button would clear the
|
|
|
|
filter_horizontal/filter_vertical widgets (#13614).
|
|
|
|
"""
|
|
|
|
self.school.students.set([self.lisa, self.peter])
|
|
|
|
self.school.alumni.set([self.lisa, self.peter])
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
|
|
|
change_url = reverse('admin:admin_widgets_school_change', args=(self.school.id,))
|
|
|
|
self.selenium.get(self.live_server_url + change_url)
|
|
|
|
# Navigate away and go back to the change form page.
|
|
|
|
self.selenium.find_element_by_link_text('Home').click()
|
|
|
|
self.selenium.back()
|
|
|
|
expected_unselected_values = [
|
|
|
|
str(self.arthur.id), str(self.bob.id), str(self.cliff.id),
|
|
|
|
str(self.jason.id), str(self.jenny.id), str(self.john.id),
|
|
|
|
]
|
|
|
|
expected_selected_values = [str(self.lisa.id), str(self.peter.id)]
|
2016-10-27 15:53:39 +08:00
|
|
|
# Everything is still in place
|
2015-12-30 23:09:43 +08:00
|
|
|
self.assertSelectOptions('#id_students_from', expected_unselected_values)
|
|
|
|
self.assertSelectOptions('#id_students_to', expected_selected_values)
|
|
|
|
self.assertSelectOptions('#id_alumni_from', expected_unselected_values)
|
|
|
|
self.assertSelectOptions('#id_alumni_to', expected_selected_values)
|
|
|
|
|
2015-12-02 20:34:35 +08:00
|
|
|
def test_refresh_page(self):
|
|
|
|
"""
|
|
|
|
Horizontal and vertical filter widgets keep selected options on page
|
|
|
|
reload (#22955).
|
|
|
|
"""
|
|
|
|
self.school.students.add(self.arthur, self.jason)
|
|
|
|
self.school.alumni.add(self.arthur, self.jason)
|
|
|
|
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
|
|
|
change_url = reverse('admin:admin_widgets_school_change', args=(self.school.id,))
|
|
|
|
self.selenium.get(self.live_server_url + change_url)
|
|
|
|
|
|
|
|
options_len = len(self.selenium.find_elements_by_css_selector('#id_students_to > option'))
|
|
|
|
self.assertEqual(options_len, 2)
|
|
|
|
|
|
|
|
# self.selenium.refresh() or send_keys(Keys.F5) does hard reload and
|
|
|
|
# doesn't replicate what happens when a user clicks the browser's
|
|
|
|
# 'Refresh' button.
|
2019-11-27 21:36:26 +08:00
|
|
|
with self.wait_page_loaded():
|
|
|
|
self.selenium.execute_script("location.reload()")
|
2015-12-02 20:34:35 +08:00
|
|
|
|
|
|
|
options_len = len(self.selenium.find_elements_by_css_selector('#id_students_to > option'))
|
|
|
|
self.assertEqual(options_len, 2)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2016-02-07 10:24:36 +08:00
|
|
|
class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
|
2013-08-05 07:06:47 +08:00
|
|
|
|
2015-03-05 21:21:29 +08:00
|
|
|
def setUp(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUp()
|
2016-12-09 00:00:14 +08:00
|
|
|
Band.objects.create(id=42, name='Bogey Blues')
|
|
|
|
Band.objects.create(id=98, name='Green Potatoes')
|
2013-08-05 07:06:47 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_ForeignKey(self):
|
2013-08-05 07:06:47 +08:00
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_event_add'))
|
2013-08-05 07:06:47 +08:00
|
|
|
main_window = self.selenium.current_window_handle
|
|
|
|
|
|
|
|
# No value has been selected yet
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual(self.selenium.find_element_by_id('id_main_band').get_attribute('value'), '')
|
2013-08-05 07:06:47 +08:00
|
|
|
|
|
|
|
# Open the popup window and click on a band
|
|
|
|
self.selenium.find_element_by_id('lookup_id_main_band').click()
|
2019-11-29 12:06:42 +08:00
|
|
|
self.wait_for_and_switch_to_popup()
|
2013-08-05 07:06:47 +08:00
|
|
|
link = self.selenium.find_element_by_link_text('Bogey Blues')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('/band/42/', link.get_attribute('href'))
|
2013-08-05 07:06:47 +08:00
|
|
|
link.click()
|
|
|
|
|
|
|
|
# The field now contains the selected band's id
|
2014-07-24 20:25:15 +08:00
|
|
|
self.selenium.switch_to.window(main_window)
|
2014-01-03 18:25:24 +08:00
|
|
|
self.wait_for_value('#id_main_band', '42')
|
2013-08-05 07:06:47 +08:00
|
|
|
|
|
|
|
# Reopen the popup window and click on another band
|
|
|
|
self.selenium.find_element_by_id('lookup_id_main_band').click()
|
2019-11-29 12:06:42 +08:00
|
|
|
self.wait_for_and_switch_to_popup()
|
2013-08-05 07:06:47 +08:00
|
|
|
link = self.selenium.find_element_by_link_text('Green Potatoes')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('/band/98/', link.get_attribute('href'))
|
2013-08-05 07:06:47 +08:00
|
|
|
link.click()
|
|
|
|
|
|
|
|
# The field now contains the other selected band's id
|
2014-07-24 20:25:15 +08:00
|
|
|
self.selenium.switch_to.window(main_window)
|
2014-01-03 18:25:24 +08:00
|
|
|
self.wait_for_value('#id_main_band', '98')
|
2013-08-05 07:06:47 +08:00
|
|
|
|
|
|
|
def test_many_to_many(self):
|
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_event_add'))
|
2013-08-05 07:06:47 +08:00
|
|
|
main_window = self.selenium.current_window_handle
|
|
|
|
|
|
|
|
# No value has been selected yet
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual(self.selenium.find_element_by_id('id_supporting_bands').get_attribute('value'), '')
|
2013-08-05 07:06:47 +08:00
|
|
|
|
2016-01-30 05:21:21 +08:00
|
|
|
# Help text for the field is displayed
|
|
|
|
self.assertEqual(
|
2016-04-15 01:10:55 +08:00
|
|
|
self.selenium.find_element_by_css_selector('.field-supporting_bands div.help').text,
|
2016-01-30 05:21:21 +08:00
|
|
|
'Supporting Bands.'
|
|
|
|
)
|
|
|
|
|
2013-08-05 07:06:47 +08:00
|
|
|
# Open the popup window and click on a band
|
|
|
|
self.selenium.find_element_by_id('lookup_id_supporting_bands').click()
|
2019-11-29 12:06:42 +08:00
|
|
|
self.wait_for_and_switch_to_popup()
|
2013-08-05 07:06:47 +08:00
|
|
|
link = self.selenium.find_element_by_link_text('Bogey Blues')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('/band/42/', link.get_attribute('href'))
|
2013-08-05 07:06:47 +08:00
|
|
|
link.click()
|
|
|
|
|
|
|
|
# The field now contains the selected band's id
|
2014-07-24 20:25:15 +08:00
|
|
|
self.selenium.switch_to.window(main_window)
|
2014-01-03 18:25:24 +08:00
|
|
|
self.wait_for_value('#id_supporting_bands', '42')
|
2013-08-05 07:06:47 +08:00
|
|
|
|
|
|
|
# Reopen the popup window and click on another band
|
|
|
|
self.selenium.find_element_by_id('lookup_id_supporting_bands').click()
|
2019-11-29 12:06:42 +08:00
|
|
|
self.wait_for_and_switch_to_popup()
|
2013-08-05 07:06:47 +08:00
|
|
|
link = self.selenium.find_element_by_link_text('Green Potatoes')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('/band/98/', link.get_attribute('href'))
|
2013-08-05 07:06:47 +08:00
|
|
|
link.click()
|
|
|
|
|
|
|
|
# The field now contains the two selected bands' ids
|
2014-07-24 20:25:15 +08:00
|
|
|
self.selenium.switch_to.window(main_window)
|
2014-01-03 18:25:24 +08:00
|
|
|
self.wait_for_value('#id_supporting_bands', '42,98')
|
2013-08-05 07:06:47 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2016-02-07 10:24:36 +08:00
|
|
|
class RelatedFieldWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
|
2013-09-08 00:52:14 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_ForeignKey_using_to_field(self):
|
2013-09-08 00:52:14 +08:00
|
|
|
self.admin_login(username='super', password='secret', login_url='/')
|
2016-04-08 10:04:45 +08:00
|
|
|
self.selenium.get(self.live_server_url + reverse('admin:admin_widgets_profile_add'))
|
2013-09-08 00:52:14 +08:00
|
|
|
|
|
|
|
main_window = self.selenium.current_window_handle
|
|
|
|
# Click the Add User button to add new
|
|
|
|
self.selenium.find_element_by_id('add_id_user').click()
|
2019-11-29 12:06:42 +08:00
|
|
|
self.wait_for_and_switch_to_popup()
|
2013-09-08 00:52:14 +08:00
|
|
|
password_field = self.selenium.find_element_by_id('id_password')
|
|
|
|
password_field.send_keys('password')
|
|
|
|
|
|
|
|
username_field = self.selenium.find_element_by_id('id_username')
|
|
|
|
username_value = 'newuser'
|
|
|
|
username_field.send_keys(username_value)
|
|
|
|
|
|
|
|
save_button_css_selector = '.submit-row > input[type=submit]'
|
|
|
|
self.selenium.find_element_by_css_selector(save_button_css_selector).click()
|
2014-07-24 20:25:15 +08:00
|
|
|
self.selenium.switch_to.window(main_window)
|
2014-01-03 07:31:53 +08:00
|
|
|
# The field now contains the new user
|
2016-01-15 08:35:34 +08:00
|
|
|
self.selenium.find_element_by_css_selector('#id_user option[value=newuser]')
|
2013-09-08 00:52:14 +08:00
|
|
|
|
2013-11-18 06:26:20 +08:00
|
|
|
# Click the Change User button to change it
|
|
|
|
self.selenium.find_element_by_id('change_id_user').click()
|
2019-11-29 12:06:42 +08:00
|
|
|
self.wait_for_and_switch_to_popup()
|
2013-11-18 06:26:20 +08:00
|
|
|
|
|
|
|
username_field = self.selenium.find_element_by_id('id_username')
|
|
|
|
username_value = 'changednewuser'
|
|
|
|
username_field.clear()
|
|
|
|
username_field.send_keys(username_value)
|
|
|
|
|
|
|
|
save_button_css_selector = '.submit-row > input[type=submit]'
|
|
|
|
self.selenium.find_element_by_css_selector(save_button_css_selector).click()
|
2015-08-01 02:34:38 +08:00
|
|
|
self.selenium.switch_to.window(main_window)
|
2013-11-18 06:26:20 +08:00
|
|
|
self.selenium.find_element_by_css_selector('#id_user option[value=changednewuser]')
|
|
|
|
|
2013-09-08 00:52:14 +08:00
|
|
|
# Go ahead and submit the form to make sure it works
|
|
|
|
self.selenium.find_element_by_css_selector(save_button_css_selector).click()
|
2019-07-01 02:35:05 +08:00
|
|
|
self.wait_for_text('li.success', 'The profile “changednewuser” was added successfully.')
|
2016-12-09 00:00:14 +08:00
|
|
|
profiles = Profile.objects.all()
|
2013-09-08 00:52:14 +08:00
|
|
|
self.assertEqual(len(profiles), 1)
|
|
|
|
self.assertEqual(profiles[0].user.username, username_value)
|