Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
2f0cc4f5
Kaydet (Commit)
2f0cc4f5
authored
Tem 04, 2014
tarafından
Simon Charette
Kaydeden (comit)
Andrew Godwin
Tem 05, 2014
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
[1.7.x] Fixed #22943 -- Correctly serialize compiled regexes.
Thanks to antialiasis at gmail dot com for the patch.
üst
1f889800
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
19 deletions
+61
-19
writer.py
django/db/migrations/writer.py
+15
-0
1.7.txt
docs/releases/1.7.txt
+0
-7
test_writer.py
tests/migrations/test_writer.py
+46
-12
No files found.
django/db/migrations/writer.py
Dosyayı görüntüle @
2f0cc4f5
...
...
@@ -6,6 +6,7 @@ import decimal
import
collections
from
importlib
import
import_module
import
os
import
re
import
sys
import
types
...
...
@@ -17,6 +18,9 @@ from django.utils.encoding import force_text
from
django.utils.functional
import
Promise
COMPILED_REGEX_TYPE
=
type
(
re
.
compile
(
''
))
class
SettingsReference
(
str
):
"""
Special subclass of string which actually references a current settings
...
...
@@ -344,6 +348,17 @@ class MigrationWriter(object):
# "()", not "(,)" because (,) is invalid Python syntax.
format
=
"(
%
s)"
if
len
(
strings
)
!=
1
else
"(
%
s,)"
return
format
%
(
", "
.
join
(
strings
)),
imports
# Compiled regex
elif
isinstance
(
value
,
COMPILED_REGEX_TYPE
):
imports
=
set
([
"import re"
])
regex_pattern
,
pattern_imports
=
cls
.
serialize
(
value
.
pattern
)
regex_flags
,
flag_imports
=
cls
.
serialize
(
value
.
flags
)
imports
.
update
(
pattern_imports
)
imports
.
update
(
flag_imports
)
args
=
[
regex_pattern
]
if
value
.
flags
:
args
.
append
(
regex_flags
)
return
"re.compile(
%
s)"
%
', '
.
join
(
args
),
imports
# Uh oh.
else
:
raise
ValueError
(
"Cannot serialize:
%
r
\n
There are some values Django cannot serialize into migration files.
\n
For more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing"
%
value
)
...
...
docs/releases/1.7.txt
Dosyayı görüntüle @
2f0cc4f5
...
...
@@ -1390,13 +1390,6 @@ Miscellaneous
a relation from the related object back to the content type for filtering,
ordering and other query operations.
* When a model field's :attr:`~django.db.models.Field.validators` contains
a :class:`~django.core.validators.RegexValidator`, the regular expression
must now be passed as a regular expression string. You can no longer use a
pre-compiled regular expression in this case, as it is not serializable.
The :attr:`~django.core.validators.RegexValidator.flags` attribute was added
to :class:`~django.core.validators.RegexValidator` to simplify this change.
* When running tests on PostgreSQL, the :setting:`USER` will need read access
to the built-in ``postgres`` database. This is in lieu of the previous
behavior of connecting to the actual non-test database.
...
...
tests/migrations/test_writer.py
Dosyayı görüntüle @
2f0cc4f5
...
...
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
import
datetime
import
os
import
re
import
tokenize
import
unittest
...
...
@@ -103,18 +104,6 @@ class WriterTests(TestCase):
string
,
imports
=
MigrationWriter
.
serialize
(
safe_datetime
)
self
.
assertEqual
(
string
,
repr
(
datetime
.
datetime
(
2014
,
3
,
31
,
16
,
4
,
31
)))
self
.
assertEqual
(
imports
,
{
'import datetime'
})
# Classes
validator
=
RegexValidator
(
message
=
"hello"
)
string
,
imports
=
MigrationWriter
.
serialize
(
validator
)
self
.
assertEqual
(
string
,
"django.core.validators.RegexValidator(message='hello')"
)
self
.
serialize_round_trip
(
validator
)
validator
=
EmailValidator
(
message
=
"hello"
)
# Test with a subclass.
string
,
imports
=
MigrationWriter
.
serialize
(
validator
)
self
.
assertEqual
(
string
,
"django.core.validators.EmailValidator(message='hello')"
)
self
.
serialize_round_trip
(
validator
)
validator
=
deconstructible
(
path
=
"custom.EmailValidator"
)(
EmailValidator
)(
message
=
"hello"
)
string
,
imports
=
MigrationWriter
.
serialize
(
validator
)
self
.
assertEqual
(
string
,
"custom.EmailValidator(message='hello')"
)
# Django fields
self
.
assertSerializedFieldEqual
(
models
.
CharField
(
max_length
=
255
))
self
.
assertSerializedFieldEqual
(
models
.
TextField
(
null
=
True
,
blank
=
True
))
...
...
@@ -135,6 +124,51 @@ class WriterTests(TestCase):
)
)
def
test_serialize_compiled_regex
(
self
):
"""
Make sure compiled regex can be serialized.
"""
regex
=
re
.
compile
(
r'^\w+$'
,
re
.
U
)
self
.
assertSerializedEqual
(
regex
)
def
test_serialize_class_based_validators
(
self
):
"""
Ticket #22943: Test serialization of class-based validators, including
compiled regexes.
"""
validator
=
RegexValidator
(
message
=
"hello"
)
string
=
MigrationWriter
.
serialize
(
validator
)[
0
]
self
.
assertEqual
(
string
,
"django.core.validators.RegexValidator(message='hello')"
)
self
.
serialize_round_trip
(
validator
)
# Test with a compiled regex.
validator
=
RegexValidator
(
regex
=
re
.
compile
(
r'^\w+$'
,
re
.
U
))
string
=
MigrationWriter
.
serialize
(
validator
)[
0
]
self
.
assertEqual
(
string
,
"django.core.validators.RegexValidator(regex=re.compile('^
\\\\
w+$', 32))"
)
self
.
serialize_round_trip
(
validator
)
# Test a string regex with flag
validator
=
RegexValidator
(
r'^[0-9]+$'
,
flags
=
re
.
U
)
string
=
MigrationWriter
.
serialize
(
validator
)[
0
]
self
.
assertEqual
(
string
,
"django.core.validators.RegexValidator('^[0-9]+$', flags=32)"
)
self
.
serialize_round_trip
(
validator
)
# Test message and code
validator
=
RegexValidator
(
'^[-a-zA-Z0-9_]+$'
,
'Invalid'
,
'invalid'
)
string
=
MigrationWriter
.
serialize
(
validator
)[
0
]
self
.
assertEqual
(
string
,
"django.core.validators.RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid')"
)
self
.
serialize_round_trip
(
validator
)
# Test with a subclass.
validator
=
EmailValidator
(
message
=
"hello"
)
string
=
MigrationWriter
.
serialize
(
validator
)[
0
]
self
.
assertEqual
(
string
,
"django.core.validators.EmailValidator(message='hello')"
)
self
.
serialize_round_trip
(
validator
)
validator
=
deconstructible
(
path
=
"custom.EmailValidator"
)(
EmailValidator
)(
message
=
"hello"
)
string
=
MigrationWriter
.
serialize
(
validator
)[
0
]
self
.
assertEqual
(
string
,
"custom.EmailValidator(message='hello')"
)
def
test_serialize_empty_nonempty_tuple
(
self
):
"""
Ticket #22679: makemigrations generates invalid code for (an empty
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment