| 1 |
import json |
| 2 |
|
| 3 |
from django.contrib.postgres import lookups |
| 4 |
from django.contrib.postgres.forms import SimpleArrayField |
| 5 |
from django.contrib.postgres.validators import ArrayMaxLengthValidator |
| 6 |
from django.core import checks, exceptions |
| 7 |
from django.db.models import Field, Func, IntegerField, Transform, Value |
| 8 |
from django.db.models.fields.mixins import CheckFieldDefaultMixin |
| 9 |
from django.db.models.lookups import Exact, In |
| 10 |
from django.utils.translation import gettext_lazy as _ |
| 11 |
|
| 12 |
from ..utils import prefix_validation_error |
| 13 |
from .utils import AttributeSetter |
| 14 |
|
| 15 |
__all__ = ["ArrayField"] |
| 16 |
|
| 17 |
|
| 18 |
class ArrayField(CheckFieldDefaultMixin, Field): |
| 19 |
empty_strings_allowed = False |
| 20 |
default_error_messages = { |
| 21 |
"item_invalid": _("Item %(nth)s in the array did not validate:"), |
| 22 |
"nested_array_mismatch": _("Nested arrays must have the same length."), |
| 23 |
} |
| 24 |
_default_hint = ("list", "[]") |
| 25 |
|
| 26 |
def __init__(self, base_field, size=None, **kwargs): |
| 27 |
self.base_field = base_field |
| 28 |
self.db_collation = getattr(self.base_field, "db_collation", None) |
| 29 |
self.size = size |
| 30 |
if self.size: |
| 31 |
self.default_validators = [ |
| 32 |
*self.default_validators, |
| 33 |
ArrayMaxLengthValidator(self.size), |
| 34 |
] |
| 35 |
# For performance, only add a from_db_value() method if the base field |
| 36 |
# implements it. |
| 37 |
if hasattr(self.base_field, "from_db_value"): |
| 38 |
self.from_db_value = self._from_db_value |
| 39 |
super().__init__(**kwargs) |
| 40 |
|
| 41 |
@property |
| 42 |
def model(self): |
| 43 |
try: |
| 44 |
return self.__dict__["model"] |
| 45 |
except KeyError: |
| 46 |
raise AttributeError( |
| 47 |
"'%s' object has no attribute 'model'" % self.__class__.__name__ |
| 48 |
) |
| 49 |
|
| 50 |
@model.setter |
| 51 |
def model(self, model): |
| 52 |
self.__dict__["model"] = model |
| 53 |
self.base_field.model = model |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def _choices_is_value(cls, value): |
| 57 |
return isinstance(value, (list, tuple)) or super()._choices_is_value(value) |
| 58 |
|
| 59 |
def check(self, **kwargs): |
| 60 |
errors = super().check(**kwargs) |
| 61 |
if self.base_field.remote_field: |
| 62 |
errors.append( |
| 63 |
checks.Error( |
| 64 |
"Base field for array cannot be a related field.", |
| 65 |
obj=self, |
| 66 |
id="postgres.E002", |
| 67 |
) |
| 68 |
) |
| 69 |
else: |
| 70 |
base_checks = self.base_field.check() |
| 71 |
if base_checks: |
| 72 |
error_messages = "\n ".join( |
| 73 |
"%s (%s)" % (base_check.msg, base_check.id) |
| 74 |
for base_check in base_checks |
| 75 |
if isinstance(base_check, checks.Error) |
| 76 |
) |
| 77 |
if error_messages: |
| 78 |
errors.append( |
| 79 |
checks.Error( |
| 80 |
"Base field for array has errors:\n %s" % error_messages, |
| 81 |
obj=self, |
| 82 |
id="postgres.E001", |
| 83 |
) |
| 84 |
) |
| 85 |
warning_messages = "\n ".join( |
| 86 |
"%s (%s)" % (base_check.msg, base_check.id) |
| 87 |
for base_check in base_checks |
| 88 |
if isinstance(base_check, checks.Warning) |
| 89 |
) |
| 90 |
if warning_messages: |
| 91 |
errors.append( |
| 92 |
checks.Warning( |
| 93 |
"Base field for array has warnings:\n %s" |
| 94 |
% warning_messages, |
| 95 |
obj=self, |
| 96 |
id="postgres.W004", |
| 97 |
) |
| 98 |
) |
| 99 |
return errors |
| 100 |
|
| 101 |
def set_attributes_from_name(self, name): |
| 102 |
super().set_attributes_from_name(name) |
| 103 |
self.base_field.set_attributes_from_name(name) |
| 104 |
|
| 105 |
@property |
| 106 |
def description(self): |
| 107 |
return "Array of %s" % self.base_field.description |
| 108 |
|
| 109 |
def db_type(self, connection): |
| 110 |
size = self.size or "" |
| 111 |
return "%s[%s]" % (self.base_field.db_type(connection), size) |
| 112 |
|
| 113 |
def cast_db_type(self, connection): |
| 114 |
size = self.size or "" |
| 115 |
return "%s[%s]" % (self.base_field.cast_db_type(connection), size) |
| 116 |
|
| 117 |
def db_parameters(self, connection): |
| 118 |
db_params = super().db_parameters(connection) |
| 119 |
db_params["collation"] = self.db_collation |
| 120 |
return db_params |
| 121 |
|
| 122 |
def get_placeholder(self, value, compiler, connection): |
| 123 |
return "%s::{}".format(self.db_type(connection)) |
| 124 |
|
| 125 |
def get_db_prep_value(self, value, connection, prepared=False): |
| 126 |
if isinstance(value, (list, tuple)): |
| 127 |
return [ |
| 128 |
self.base_field.get_db_prep_value(i, connection, prepared=False) |
| 129 |
for i in value |
| 130 |
] |
| 131 |
return value |
| 132 |
|
| 133 |
def deconstruct(self): |
| 134 |
name, path, args, kwargs = super().deconstruct() |
| 135 |
if path == "django.contrib.postgres.fields.array.ArrayField": |
| 136 |
path = "django.contrib.postgres.fields.ArrayField" |
| 137 |
kwargs.update( |
| 138 |
{ |
| 139 |
"base_field": self.base_field.clone(), |
| 140 |
"size": self.size, |
| 141 |
} |
| 142 |
) |
| 143 |
return name, path, args, kwargs |
| 144 |
|
| 145 |
def to_python(self, value): |
| 146 |
if isinstance(value, str): |
| 147 |
# Assume we're deserializing |
| 148 |
vals = json.loads(value) |
| 149 |
value = [self.base_field.to_python(val) for val in vals] |
| 150 |
return value |
| 151 |
|
| 152 |
def _from_db_value(self, value, expression, connection): |
| 153 |
if value is None: |
| 154 |
return value |
| 155 |
return [ |
| 156 |
self.base_field.from_db_value(item, expression, connection) |
| 157 |
for item in value |
| 158 |
] |
| 159 |
|
| 160 |
def value_to_string(self, obj): |
| 161 |
values = [] |
| 162 |
vals = self.value_from_object(obj) |
| 163 |
base_field = self.base_field |
| 164 |
|
| 165 |
for val in vals: |
| 166 |
if val is None: |
| 167 |
values.append(None) |
| 168 |
else: |
| 169 |
obj = AttributeSetter(base_field.attname, val) |
| 170 |
values.append(base_field.value_to_string(obj)) |
| 171 |
return json.dumps(values, ensure_ascii=False) |
| 172 |
|
| 173 |
def get_transform(self, name): |
| 174 |
transform = super().get_transform(name) |
| 175 |
if transform: |
| 176 |
return transform |
| 177 |
if "_" not in name: |
| 178 |
try: |
| 179 |
index = int(name) |
| 180 |
except ValueError: |
| 181 |
pass |
| 182 |
else: |
| 183 |
index += 1 # postgres uses 1-indexing |
| 184 |
return IndexTransformFactory(index, self.base_field) |
| 185 |
try: |
| 186 |
start, end = name.split("_") |
| 187 |
start = int(start) + 1 |
| 188 |
end = int(end) # don't add one here because postgres slices are weird |
| 189 |
except ValueError: |
| 190 |
pass |
| 191 |
else: |
| 192 |
return SliceTransformFactory(start, end) |
| 193 |
|
| 194 |
def validate(self, value, model_instance): |
| 195 |
super().validate(value, model_instance) |
| 196 |
for index, part in enumerate(value): |
| 197 |
try: |
| 198 |
self.base_field.validate(part, model_instance) |
| 199 |
except exceptions.ValidationError as error: |
| 200 |
raise prefix_validation_error( |
| 201 |
error, |
| 202 |
prefix=self.error_messages["item_invalid"], |
| 203 |
code="item_invalid", |
| 204 |
params={"nth": index + 1}, |
| 205 |
) |
| 206 |
if isinstance(self.base_field, ArrayField): |
| 207 |
if len({len(i) for i in value}) > 1: |
| 208 |
raise exceptions.ValidationError( |
| 209 |
self.error_messages["nested_array_mismatch"], |
| 210 |
code="nested_array_mismatch", |
| 211 |
) |
| 212 |
|
| 213 |
def run_validators(self, value): |
| 214 |
super().run_validators(value) |
| 215 |
for index, part in enumerate(value): |
| 216 |
try: |
| 217 |
self.base_field.run_validators(part) |
| 218 |
except exceptions.ValidationError as error: |
| 219 |
raise prefix_validation_error( |
| 220 |
error, |
| 221 |
prefix=self.error_messages["item_invalid"], |
| 222 |
code="item_invalid", |
| 223 |
params={"nth": index + 1}, |
| 224 |
) |
| 225 |
|
| 226 |
def formfield(self, **kwargs): |
| 227 |
return super().formfield( |
| 228 |
**{ |
| 229 |
"form_class": SimpleArrayField, |
| 230 |
"base_field": self.base_field.formfield(), |
| 231 |
"max_length": self.size, |
| 232 |
**kwargs, |
| 233 |
} |
| 234 |
) |
| 235 |
|
| 236 |
def slice_expression(self, expression, start, length): |
| 237 |
# If length is not provided, don't specify an end to slice to the end |
| 238 |
# of the array. |
| 239 |
end = None if length is None else start + length - 1 |
| 240 |
return SliceTransform(start, end, expression) |
| 241 |
|
| 242 |
|
| 243 |
class ArrayRHSMixin: |
| 244 |
def __init__(self, lhs, rhs): |
| 245 |
# Don't wrap arrays that contains only None values, psycopg doesn't |
| 246 |
# allow this. |
| 247 |
if isinstance(rhs, (tuple, list)) and any(self._rhs_not_none_values(rhs)): |
| 248 |
expressions = [] |
| 249 |
for value in rhs: |
| 250 |
if not hasattr(value, "resolve_expression"): |
| 251 |
field = lhs.output_field |
| 252 |
value = Value(field.base_field.get_prep_value(value)) |
| 253 |
expressions.append(value) |
| 254 |
rhs = Func( |
| 255 |
*expressions, |
| 256 |
function="ARRAY", |
| 257 |
template="%(function)s[%(expressions)s]", |
| 258 |
) |
| 259 |
super().__init__(lhs, rhs) |
| 260 |
|
| 261 |
def process_rhs(self, compiler, connection): |
| 262 |
rhs, rhs_params = super().process_rhs(compiler, connection) |
| 263 |
cast_type = self.lhs.output_field.cast_db_type(connection) |
| 264 |
return "%s::%s" % (rhs, cast_type), rhs_params |
| 265 |
|
| 266 |
def _rhs_not_none_values(self, rhs): |
| 267 |
for x in rhs: |
| 268 |
if isinstance(x, (list, tuple)): |
| 269 |
yield from self._rhs_not_none_values(x) |
| 270 |
elif x is not None: |
| 271 |
yield True |
| 272 |
|
| 273 |
|
| 274 |
@ArrayField.register_lookup |
| 275 |
class ArrayContains(ArrayRHSMixin, lookups.DataContains): |
| 276 |
pass |
| 277 |
|
| 278 |
|
| 279 |
@ArrayField.register_lookup |
| 280 |
class ArrayContainedBy(ArrayRHSMixin, lookups.ContainedBy): |
| 281 |
pass |
| 282 |
|
| 283 |
|
| 284 |
@ArrayField.register_lookup |
| 285 |
class ArrayExact(ArrayRHSMixin, Exact): |
| 286 |
pass |
| 287 |
|
| 288 |
|
| 289 |
@ArrayField.register_lookup |
| 290 |
class ArrayOverlap(ArrayRHSMixin, lookups.Overlap): |
| 291 |
pass |
| 292 |
|
| 293 |
|
| 294 |
@ArrayField.register_lookup |
| 295 |
class ArrayLenTransform(Transform): |
| 296 |
lookup_name = "len" |
| 297 |
output_field = IntegerField() |
| 298 |
|
| 299 |
def as_sql(self, compiler, connection): |
| 300 |
lhs, params = compiler.compile(self.lhs) |
| 301 |
# Distinguish NULL and empty arrays |
| 302 |
return ( |
| 303 |
"CASE WHEN %(lhs)s IS NULL THEN NULL ELSE " |
| 304 |
"coalesce(array_length(%(lhs)s, 1), 0) END" |
| 305 |
) % {"lhs": lhs}, params * 2 |
| 306 |
|
| 307 |
|
| 308 |
@ArrayField.register_lookup |
| 309 |
class ArrayInLookup(In): |
| 310 |
def get_prep_lookup(self): |
| 311 |
values = super().get_prep_lookup() |
| 312 |
if hasattr(values, "resolve_expression"): |
| 313 |
return values |
| 314 |
# In.process_rhs() expects values to be hashable, so convert lists |
| 315 |
# to tuples. |
| 316 |
prepared_values = [] |
| 317 |
for value in values: |
| 318 |
if hasattr(value, "resolve_expression"): |
| 319 |
prepared_values.append(value) |
| 320 |
else: |
| 321 |
prepared_values.append(tuple(value)) |
| 322 |
return prepared_values |
| 323 |
|
| 324 |
|
| 325 |
class IndexTransform(Transform): |
| 326 |
def __init__(self, index, base_field, *args, **kwargs): |
| 327 |
super().__init__(*args, **kwargs) |
| 328 |
self.index = index |
| 329 |
self.base_field = base_field |
| 330 |
|
| 331 |
def as_sql(self, compiler, connection): |
| 332 |
lhs, params = compiler.compile(self.lhs) |
| 333 |
if not lhs.endswith("]"): |
| 334 |
lhs = "(%s)" % lhs |
| 335 |
return "%s[%%s]" % lhs, (*params, self.index) |
| 336 |
|
| 337 |
@property |
| 338 |
def output_field(self): |
| 339 |
return self.base_field |
| 340 |
|
| 341 |
|
| 342 |
class IndexTransformFactory: |
| 343 |
def __init__(self, index, base_field): |
| 344 |
self.index = index |
| 345 |
self.base_field = base_field |
| 346 |
|
| 347 |
def __call__(self, *args, **kwargs): |
| 348 |
return IndexTransform(self.index, self.base_field, *args, **kwargs) |
| 349 |
|
| 350 |
|
| 351 |
class SliceTransform(Transform): |
| 352 |
def __init__(self, start, end, *args, **kwargs): |
| 353 |
super().__init__(*args, **kwargs) |
| 354 |
self.start = start |
| 355 |
self.end = end |
| 356 |
|
| 357 |
def as_sql(self, compiler, connection): |
| 358 |
lhs, params = compiler.compile(self.lhs) |
| 359 |
# self.start is set to 1 if slice start is not provided. |
| 360 |
if self.end is None: |
| 361 |
return f"({lhs})[%s:]", (*params, self.start) |
| 362 |
else: |
| 363 |
return f"({lhs})[%s:%s]", (*params, self.start, self.end) |
| 364 |
|
| 365 |
|
| 366 |
class SliceTransformFactory: |
| 367 |
def __init__(self, start, end): |
| 368 |
self.start = start |
| 369 |
self.end = end |
| 370 |
|
| 371 |
def __call__(self, *args, **kwargs): |
| 372 |
return SliceTransform(self.start, self.end, *args, **kwargs) |