Skip to content

Commit 879330b

Browse files
committed
edits
1 parent 14af7cb commit 879330b

File tree

7 files changed

+92
-62
lines changed

7 files changed

+92
-62
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ repos:
4444
hooks:
4545
- id: rstcheck
4646
additional_dependencies: [sphinx]
47-
args: ["--ignore-directives=fieldlookup", "--ignore-roles=lookup"]
47+
args: ["--ignore-directives=fieldlookup,setting", "--ignore-roles=lookup,setting"]
4848

4949
# We use the Python version instead of the original version which seems to require Docker
5050
# https://github.com/koalaman/shellcheck-precommit

django_mongodb/forms/array.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
from itertools import chain
33

44
from django import forms
5-
from django.contrib.postgres.validators import (
6-
ArrayMaxLengthValidator,
7-
ArrayMinLengthValidator,
8-
)
95
from django.core.exceptions import ValidationError
106
from django.utils.translation import gettext_lazy as _
117

8+
from django_mongodb.validators import ArrayMaxLengthValidator, ArrayMinLengthValidator
9+
1210
from ..utils import prefix_validation_error
1311

1412

django_mongodb/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ def prefix_validation_error(error, prefix, code, params):
6868
if error.error_list == [error]:
6969
error_params = error.params or {}
7070
return ValidationError(
71-
# We can't simply concatenate messages since they might require
72-
# their associated parameters to be expressed correctly which
73-
# is not something `format_lazy` does. For example, proxied
71+
# Messages can't simply be concatenated since they might require
72+
# their associated parameters to be expressed correctly which is
73+
# not something format_lazy() does. For example, proxied
7474
# ngettext calls require a count parameter and are converted
7575
# to an empty string if they are missing it.
7676
message=format_lazy(

django_mongodb/validators.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.core.validators import MaxLengthValidator, MinLengthValidator
2+
from django.utils.translation import ngettext_lazy
3+
4+
5+
class ArrayMaxLengthValidator(MaxLengthValidator):
6+
message = ngettext_lazy(
7+
"List contains %(show_value)d item, it should contain no more than %(limit_value)d.",
8+
"List contains %(show_value)d items, it should contain no more than %(limit_value)d.",
9+
"show_value",
10+
)
11+
12+
13+
class ArrayMinLengthValidator(MinLengthValidator):
14+
message = ngettext_lazy(
15+
"List contains %(show_value)d item, it should contain no fewer than %(limit_value)d.",
16+
"List contains %(show_value)d items, it should contain no fewer than %(limit_value)d.",
17+
"show_value",
18+
)

docs/source/_ext/djangodocs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ def setup(app):
44
rolename="lookup",
55
indextemplate="pair: %s; field lookup type",
66
)
7+
app.add_crossref_type(
8+
directivename="setting",
9+
rolename="setting",
10+
indextemplate="pair: %s; setting",
11+
)

docs/source/forms.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ Stores an :class:`~bson.objectid.ObjectId`.
9797
This field handles arrays by reproducing the underlying field a fixed
9898
number of times.
9999

100+
The template for this widget is located in
101+
``django_mongodb/templates/mongodb/widgets``. Don't forget to configure
102+
template loading appropriately, for example, by using a
103+
:class:`~django.template.backends.django.DjangoTemplates` engine with
104+
:setting:`APP_DIRS=True <TEMPLATES-APP_DIRS>` and ``"django_mongodb"`` in
105+
:setting:`INSTALLED_APPS`.
106+
100107
.. attribute:: base_field
101108

102109
This is a required argument. It specifies the form field to be

tests/model_fields_/test_arrayfield.py

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,34 @@ class MyModel(models.Model):
8181
instance = MyModel(field=value)
8282
self.assertEqual(instance.get_field_display(), display)
8383

84+
def test_deconstruct(self):
85+
field = ArrayField(models.IntegerField())
86+
name, path, args, kwargs = field.deconstruct()
87+
new = ArrayField(*args, **kwargs)
88+
self.assertEqual(type(new.base_field), type(field.base_field))
89+
self.assertIsNot(new.base_field, field.base_field)
90+
91+
def test_deconstruct_with_size(self):
92+
field = ArrayField(models.IntegerField(), size=3)
93+
name, path, args, kwargs = field.deconstruct()
94+
new = ArrayField(*args, **kwargs)
95+
self.assertEqual(new.size, field.size)
96+
97+
def test_deconstruct_args(self):
98+
field = ArrayField(models.CharField(max_length=20))
99+
name, path, args, kwargs = field.deconstruct()
100+
new = ArrayField(*args, **kwargs)
101+
self.assertEqual(new.base_field.max_length, field.base_field.max_length)
102+
103+
def test_subclass_deconstruct(self):
104+
field = ArrayField(models.IntegerField())
105+
name, path, args, kwargs = field.deconstruct()
106+
self.assertEqual(path, "django_mongodb.fields.ArrayField")
107+
108+
field = ArrayFieldSubclass()
109+
name, path, args, kwargs = field.deconstruct()
110+
self.assertEqual(path, "model_fields_.models.ArrayFieldSubclass")
111+
84112

85113
class SaveLoadTests(TestCase):
86114
def test_integer(self):
@@ -613,53 +641,41 @@ class MyModel(models.Model):
613641
self.assertEqual(MyModel._meta.get_field("field").check(), [])
614642

615643

644+
@isolate_apps("model_fields_")
616645
class MigrationsTests(TransactionTestCase):
617646
available_apps = ["model_fields_"]
618647

619-
def test_deconstruct(self):
620-
field = ArrayField(models.IntegerField())
621-
name, path, args, kwargs = field.deconstruct()
622-
new = ArrayField(*args, **kwargs)
623-
self.assertEqual(type(new.base_field), type(field.base_field))
624-
self.assertIsNot(new.base_field, field.base_field)
625-
626-
def test_deconstruct_with_size(self):
627-
field = ArrayField(models.IntegerField(), size=3)
628-
name, path, args, kwargs = field.deconstruct()
629-
new = ArrayField(*args, **kwargs)
630-
self.assertEqual(new.size, field.size)
631-
632-
def test_deconstruct_args(self):
633-
field = ArrayField(models.CharField(max_length=20))
634-
name, path, args, kwargs = field.deconstruct()
635-
new = ArrayField(*args, **kwargs)
636-
self.assertEqual(new.base_field.max_length, field.base_field.max_length)
637-
638-
def test_subclass_deconstruct(self):
639-
field = ArrayField(models.IntegerField())
640-
name, path, args, kwargs = field.deconstruct()
641-
self.assertEqual(path, "django_mongodb.fields.ArrayField")
642-
643-
field = ArrayFieldSubclass()
644-
name, path, args, kwargs = field.deconstruct()
645-
self.assertEqual(path, "model_fields_.models.ArrayFieldSubclass")
646-
647648
@override_settings(
648649
MIGRATION_MODULES={
649650
"model_fields_": "model_fields_.array_default_migrations",
650651
}
651652
)
652653
def test_adding_field_with_default(self):
653-
# See #22962
654+
class IntegerArrayDefaultModel(models.Model):
655+
field = ArrayField(models.IntegerField(), size=None)
656+
654657
table_name = "model_fields__integerarraydefaultmodel"
655-
with connection.cursor() as cursor:
656-
self.assertNotIn(table_name, connection.introspection.table_names(cursor))
657-
call_command("migrate", "model_fields_", verbosity=0)
658-
with connection.cursor() as cursor:
659-
self.assertIn(table_name, connection.introspection.table_names(cursor))
658+
self.assertNotIn(table_name, connection.introspection.table_names(None))
659+
# Create collection
660+
call_command("migrate", "model_fields_", "0001", verbosity=0)
661+
self.assertIn(table_name, connection.introspection.table_names(None))
662+
obj = IntegerArrayDefaultModel.objects.create(field=[1, 2, 3])
663+
# Add `field2 to IntegerArrayDefaultModel.
664+
call_command("migrate", "model_fields_", "0002", verbosity=0)
665+
666+
class UpdatedIntegerArrayDefaultModel(models.Model):
667+
field = ArrayField(models.IntegerField(), size=None)
668+
field_2 = ArrayField(models.IntegerField(), default=[], size=None)
669+
670+
class Meta:
671+
db_table = "model_fields__integerarraydefaultmodel"
672+
673+
obj = UpdatedIntegerArrayDefaultModel.objects.get()
674+
# The default is populated on existing documents.
675+
self.assertEqual(obj.field_2, [])
676+
# Cleanup.
660677
call_command("migrate", "model_fields_", "zero", verbosity=0)
661-
with connection.cursor() as cursor:
662-
self.assertNotIn(table_name, connection.introspection.table_names(cursor))
678+
self.assertNotIn(table_name, connection.introspection.table_names(None))
663679

664680
@override_settings(
665681
MIGRATION_MODULES={
@@ -669,7 +685,7 @@ def test_adding_field_with_default(self):
669685
def test_adding_arrayfield_with_index(self):
670686
table_name = "model_fields__chartextarrayindexmodel"
671687
call_command("migrate", "model_fields_", verbosity=0)
672-
# All fields should have regular indexes.
688+
# All fields should have indexes.
673689
indexes = [
674690
c["columns"][0]
675691
for c in connection.introspection.get_constraints(None, table_name).values()
@@ -779,11 +795,7 @@ class AdminUtilsTests(SimpleTestCase):
779795

780796
def test_array_display_for_field(self):
781797
array_field = ArrayField(models.IntegerField())
782-
display_value = display_for_field(
783-
[1, 2],
784-
array_field,
785-
self.empty_value,
786-
)
798+
display_value = display_for_field([1, 2], array_field, self.empty_value)
787799
self.assertEqual(display_value, "1, 2")
788800

789801
def test_array_with_choices_display_for_field(self):
@@ -794,17 +806,7 @@ def test_array_with_choices_display_for_field(self):
794806
([1, 2], "2nd choice"),
795807
],
796808
)
797-
798-
display_value = display_for_field(
799-
[1, 2],
800-
array_field,
801-
self.empty_value,
802-
)
809+
display_value = display_for_field([1, 2], array_field, self.empty_value)
803810
self.assertEqual(display_value, "2nd choice")
804-
805-
display_value = display_for_field(
806-
[99, 99],
807-
array_field,
808-
self.empty_value,
809-
)
811+
display_value = display_for_field([99, 99], array_field, self.empty_value)
810812
self.assertEqual(display_value, self.empty_value)

0 commit comments

Comments
 (0)