2015-04-04 18:10:26 +02:00
|
|
|
"""
|
|
|
|
Indirection layer for PostgreSQL-specific fields, so the tests don't fail when
|
|
|
|
run with a backend other than PostgreSQL.
|
|
|
|
"""
|
2024-01-26 12:45:07 +01:00
|
|
|
|
2019-02-04 00:27:19 +01:00
|
|
|
import enum
|
|
|
|
|
2015-04-04 18:10:26 +02:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
try:
|
|
|
|
from django.contrib.postgres.fields import (
|
2017-02-11 13:16:35 +01:00
|
|
|
ArrayField,
|
|
|
|
BigIntegerRangeField,
|
2018-10-03 01:17:23 +02:00
|
|
|
DateRangeField,
|
|
|
|
DateTimeRangeField,
|
|
|
|
DecimalRangeField,
|
2019-06-09 02:56:37 +02:00
|
|
|
HStoreField,
|
|
|
|
IntegerRangeField,
|
2015-04-04 18:10:26 +02:00
|
|
|
)
|
2019-10-10 20:04:17 +02:00
|
|
|
from django.contrib.postgres.search import SearchVector, SearchVectorField
|
2015-04-04 18:10:26 +02:00
|
|
|
except ImportError:
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2015-04-04 18:10:26 +02:00
|
|
|
class DummyArrayField(models.Field):
|
|
|
|
def __init__(self, base_field, size=None, **kwargs):
|
2017-01-21 14:13:44 +01:00
|
|
|
super().__init__(**kwargs)
|
2015-04-04 18:10:26 +02:00
|
|
|
|
|
|
|
def deconstruct(self):
|
2017-01-21 14:13:44 +01:00
|
|
|
name, path, args, kwargs = super().deconstruct()
|
2015-04-04 18:10:26 +02:00
|
|
|
kwargs.update(
|
|
|
|
{
|
|
|
|
"base_field": "",
|
|
|
|
"size": 1,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return name, path, args, kwargs
|
|
|
|
|
2021-06-17 23:13:49 +02:00
|
|
|
class DummyContinuousRangeField(models.Field):
|
|
|
|
def __init__(self, *args, default_bounds="[)", **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
name, path, args, kwargs = super().deconstruct()
|
|
|
|
kwargs["default_bounds"] = "[)"
|
|
|
|
return name, path, args, kwargs
|
|
|
|
|
2015-04-04 18:10:26 +02:00
|
|
|
ArrayField = DummyArrayField
|
|
|
|
BigIntegerRangeField = models.Field
|
|
|
|
DateRangeField = models.Field
|
2021-06-17 23:13:49 +02:00
|
|
|
DateTimeRangeField = DummyContinuousRangeField
|
|
|
|
DecimalRangeField = DummyContinuousRangeField
|
2015-04-04 18:10:26 +02:00
|
|
|
HStoreField = models.Field
|
|
|
|
IntegerRangeField = models.Field
|
2019-10-10 20:04:17 +02:00
|
|
|
SearchVector = models.Expression
|
2015-05-31 23:45:03 +02:00
|
|
|
SearchVectorField = models.Field
|
2019-02-04 00:27:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EnumField(models.CharField):
|
|
|
|
def get_prep_value(self, value):
|
|
|
|
return value.value if isinstance(value, enum.Enum) else value
|