Ticket #22943: validator_patch_2.2.diff
File validator_patch_2.2.diff, 12.4 KB (added by , 10 years ago) |
---|
-
django/db/migrations/writer.py
diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 68f3eb5..e9d35da 100644
a b import decimal 6 6 import collections 7 7 from importlib import import_module 8 8 import os 9 import re 9 10 import sys 10 11 import types 11 12 … … from django.utils.encoding import force_text 17 18 from django.utils.functional import Promise 18 19 19 20 21 COMPILED_REGEX_TYPE = type(re.compile('')) 22 23 20 24 class SettingsReference(str): 21 25 """ 22 26 Special subclass of string which actually references a current settings … … class MigrationWriter(object): 344 348 # "()", not "(,)" because (,) is invalid Python syntax. 345 349 format = "(%s)" if len(strings) != 1 else "(%s,)" 346 350 return format % (", ".join(strings)), imports 351 # Compiled regex (#22943) 352 elif isinstance(value, COMPILED_REGEX_TYPE): 353 imports = set(["import re"]) 354 regex_pattern, pattern_imports = cls.serialize(value.pattern) 355 regex_flags, flag_imports = cls.serialize(value.flags) 356 imports.update(pattern_imports) 357 imports.update(flag_imports) 358 args = [regex_pattern] 359 if value.flags: 360 args.append(regex_flags) 361 return "re.compile(%s)" % ', '.join(args), imports 347 362 # Uh oh. 348 363 else: 349 364 raise ValueError("Cannot serialize: %r\nThere are some values Django cannot serialize into migration files.\nFor more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing" % value) -
docs/releases/1.7.txt
diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index b738d16..6c75519 100644
a b Miscellaneous 1272 1272 a relation from the related object back to the content type for filtering, 1273 1273 ordering and other query operations. 1274 1274 1275 * When a model field's :attr:`~django.db.models.Field.validators` contains1276 a :class:`~django.core.validators.RegexValidator`, the regular expression1277 must now be passed as a regular expression string. You can no longer use a1278 pre-compiled regular expression in this case, as it is not serializable.1279 The :attr:`~django.core.validators.RegexValidator.flags` attribute was added1280 to :class:`~django.core.validators.RegexValidator` to simplify this change.1281 1282 1275 * When running tests on PostgreSQL, the :setting:`USER` will need read access 1283 1276 to the built-in ``postgres`` database. This is in lieu of the previous 1284 1277 behavior of connecting to the actual non-test database. -
tests/migrations/test_writer.py
diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index cce0775..67915e7 100644
a b 2 2 from __future__ import unicode_literals 3 3 4 4 import datetime 5 import re 5 6 import os 6 7 import tokenize 7 8 import unittest 8 9 9 from django.core.validators import RegexValidator, EmailValidator 10 from django.core.validators import RegexValidator, EmailValidator, validate_slug 10 11 from django.db import models, migrations 11 12 from django.db.migrations.writer import MigrationWriter, SettingsReference 12 13 from django.test import TestCase … … class WriterTests(TestCase): 104 105 self.assertEqual(string, repr(datetime.datetime(2014, 3, 31, 16, 4, 31))) 105 106 self.assertEqual(imports, {'import datetime'}) 106 107 # Classes 107 validator = RegexValidator(message="hello")108 string, imports = MigrationWriter.serialize(validator)109 self.assertEqual(string, "django.core.validators.RegexValidator(message='hello')")110 self.serialize_round_trip(validator)111 validator = EmailValidator(message="hello") # Test with a subclass.112 string, imports = MigrationWriter.serialize(validator)113 self.assertEqual(string, "django.core.validators.EmailValidator(message='hello')")114 self.serialize_round_trip(validator)115 validator = deconstructible(path="custom.EmailValidator")(EmailValidator)(message="hello")116 string, imports = MigrationWriter.serialize(validator)117 self.assertEqual(string, "custom.EmailValidator(message='hello')")118 108 # Django fields 119 109 self.assertSerializedFieldEqual(models.CharField(max_length=255)) 120 110 self.assertSerializedFieldEqual(models.TextField(null=True, blank=True)) … … class WriterTests(TestCase): 135 125 ) 136 126 ) 137 127 128 def test_serialize_validators(self): 129 """ 130 Test serialization of class-based validators, including 131 regexes. 132 """ 133 validator = RegexValidator(message="hello") 134 string, imports = MigrationWriter.serialize(validator) 135 self.assertEqual(string, "django.core.validators.RegexValidator(message='hello')") 136 self.serialize_round_trip(validator) 137 138 validator = RegexValidator(regex=re.compile('^\\w+$', re.U)) # Test with a compiled regex. 139 string, imports = MigrationWriter.serialize(validator) 140 self.assertEqual(string, "django.core.validators.RegexValidator(regex=re.compile('^\\\\w+$', 32))") 141 self.serialize_round_trip(validator) 142 143 validator = validate_slug 144 string, imports = MigrationWriter.serialize(validator) 145 self.assertEqual(string, "django.core.validators.RegexValidator(re.compile('^[-a-zA-Z0-9_]+$'), \"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.\", 'invalid')") 146 self.serialize_round_trip(validator) 147 148 validator = EmailValidator(message="hello") # Test with a subclass. 149 string, imports = MigrationWriter.serialize(validator) 150 self.assertEqual(string, "django.core.validators.EmailValidator(message='hello')") 151 self.serialize_round_trip(validator) 152 153 validator = deconstructible(path="custom.EmailValidator")(EmailValidator)(message="hello") 154 string, imports = MigrationWriter.serialize(validator) 155 self.assertEqual(string, "custom.EmailValidator(message='hello')") 156 138 157 def test_serialize_empty_nonempty_tuple(self): 139 158 """ 140 159 Ticket #22679: makemigrations generates invalid code for (an empty