Ticket #12521: 12521.tests.diff
File 12521.tests.diff, 7.0 KB (added by , 15 years ago) |
---|
-
django/contrib/auth/admin.py
=== modified file 'django/contrib/auth/admin.py'
41 41 if url.endswith('password'): 42 42 return self.user_change_password(request, url.split('/')[0]) 43 43 return super(UserAdmin, self).__call__(request, url) 44 44 45 45 def get_urls(self): 46 46 from django.conf.urls.defaults import patterns 47 47 return patterns('', … … 94 94 'save_as': False, 95 95 'username_help_text': self.model._meta.get_field('username').help_text, 96 96 'root_path': self.admin_site.root_path, 97 'app_label': self.model._meta.app_label, 97 'app_label': self.model._meta.app_label, 98 98 }, context_instance=template.RequestContext(request)) 99 99 100 100 def user_change_password(self, request, id): -
django/contrib/auth/forms.py
=== modified file 'django/contrib/auth/forms.py'
1 from django.contrib.auth.models import User , UNUSABLE_PASSWORD1 from django.contrib.auth.models import User 2 2 from django.contrib.auth import authenticate 3 3 from django.contrib.auth.tokens import default_token_generator 4 4 from django.contrib.sites.models import Site … … 21 21 model = User 22 22 fields = ("username",) 23 23 24 def clean(self):25 # Fill the password field so model validation won't complain about it26 # being blank. We'll set it with the real value below.27 self.instance.password = UNUSABLE_PASSWORD28 super(UserCreationForm, self).clean()29 30 24 def clean_username(self): 31 25 username = self.cleaned_data["username"] 32 26 try: … … 40 34 password2 = self.cleaned_data["password2"] 41 35 if password1 != password2: 42 36 raise forms.ValidationError(_("The two password fields didn't match.")) 43 self.instance.set_password(password1)44 37 return password2 45 38 39 def save(self, *args, **kwargs): 40 self.instance.set_password(self.cleaned_data["password1"]) 41 return super(UserCreationForm, self).save(*args, **kwargs) 42 46 43 class UserChangeForm(forms.ModelForm): 47 44 username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^\w+$', 48 45 help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."), 49 46 error_message = _("This value must contain only letters, numbers and underscores.")) 50 47 51 48 class Meta: 52 49 model = User 53 50 -
tests/modeltests/model_forms/models.py
=== modified file 'tests/modeltests/model_forms/models.py'
1152 1152 >>> class BigIntForm(forms.ModelForm): 1153 1153 ... class Meta: 1154 1154 ... model = BigInt 1155 ... 1155 ... 1156 1156 >>> bif = BigIntForm({'biggie': '-9223372036854775808'}) 1157 1157 >>> bif.is_valid() 1158 1158 True … … 1425 1425 >>> form._errors 1426 1426 {'__all__': [u'Price with this Price and Quantity already exists.']} 1427 1427 1428 # This form is never valid because quantity is blank=False. 1428 This Price instance generated by this form is not valid because the quantity 1429 field is required, but the form is valid because the field is excluded from 1430 the form. This is for backwards compatibility. 1431 1429 1432 >>> class PriceForm(ModelForm): 1430 1433 ... class Meta: 1431 1434 ... model = Price 1432 1435 ... exclude = ('quantity',) 1433 1436 >>> form = PriceForm({'price': '6.00'}) 1434 1437 >>> form.is_valid() 1438 True 1439 >>> price = form.save(commit=False) 1440 >>> price.full_validate() 1435 1441 Traceback (most recent call last): 1436 1442 ... 1437 UnresolvableValidationError: {'quantity': [u'This field cannot be null.']} 1443 ValidationError: {'quantity': [u'This field cannot be null.']} 1444 1445 The form should not validate fields that it doesn't contain even if they are 1446 specified using 'fields', not 'exclude'. 1447 1448 >>> class PriceForm(ModelForm): 1449 ... class Meta: 1450 ... model = Price 1451 ... fields = ('price',) 1452 >>> form = PriceForm({'price': '6.00'}) 1453 >>> form.is_valid() 1454 True 1455 1456 The form should still have an instance of a model that is not complete and 1457 not saved into a DB yet. 1458 1459 >>> form.instance.price 1460 Decimal('6.00') 1461 >>> form.instance.quantity is None 1462 True 1463 >>> form.instance.pk is None 1464 True 1438 1465 1439 1466 # Unique & unique together with null values 1440 1467 >>> class BookForm(ModelForm): -
tests/regressiontests/admin_views/tests.py
=== modified file 'tests/regressiontests/admin_views/tests.py'
4 4 import datetime 5 5 from django.core.files import temp as tempfile 6 6 from django.test import TestCase 7 from django.contrib.auth.models import User, Permission 7 from django.contrib.auth.models import User, Permission, UNUSABLE_PASSWORD 8 from django.contrib.auth import admin 8 9 from django.contrib.contenttypes.models import ContentType 9 10 from django.contrib.admin.models import LogEntry, DELETION 10 11 from django.contrib.admin.sites import LOGIN_FORM_KEY … … 622 623 response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/history/' % quote(self.pk)) 623 624 self.assertContains(response, escape(self.pk)) 624 625 self.failUnlessEqual(response.status_code, 200) 625 626 626 627 def test_get_change_view(self): 627 628 "Retrieving the object using urlencoded form of primary key should work" 628 629 response = self.client.get('/test_admin/admin/admin_views/modelwithstringprimarykey/%s/' % quote(self.pk)) … … 1753 1754 self.assertEqual(Post.objects.count(), 2) 1754 1755 p = Post.objects.order_by('-id')[0] 1755 1756 self.assertEqual(p.posted, datetime.date.today()) 1757 1758 class IncompleteFormTest(TestCase): 1759 """ 1760 Tests validation of a ModelForm that doesn't explicitly have all data 1761 corresponding to model fields. Model validation shouldn't fail 1762 such a forms. 1763 """ 1764 fixtures = ['admin-views-users.xml'] 1765 1766 def setUp(self): 1767 self.client.login(username='super', password='secret') 1768 1769 def tearDown(self): 1770 self.client.logout() 1771 1772 def test_user_creation(self): 1773 response = self.client.post('/test_admin/admin/auth/user/add/', { 1774 'username': 'newuser', 1775 'password1': 'newpassword', 1776 'password2': 'newpassword', 1777 }) 1778 new_user = User.objects.order_by('-id')[0] 1779 self.assertRedirects(response, '/test_admin/admin/auth/user/%s/' % new_user.pk) 1780 self.assertNotEquals(new_user.password, UNUSABLE_PASSWORD) 1781 1782 def test_password_mismatch(self): 1783 response = self.client.post('/test_admin/admin/auth/user/add/', { 1784 'username': 'newuser', 1785 'password1': 'newpassword', 1786 'password2': 'mismatch', 1787 }) 1788 self.assertEquals(response.status_code, 200) 1789 self.assert_('password' not in response.context['form'].errors) 1790 self.assertFormError(response, 'form', 'password2', ["The two password fields didn't match."])