2019-01-11 02:06:14 +08:00
|
|
|
from psycopg2.extras import (
|
|
|
|
DateRange, DateTimeRange, DateTimeTZRange, NumericRange,
|
|
|
|
)
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
from django.apps import AppConfig
|
2016-07-20 16:18:11 +08:00
|
|
|
from django.db import connections
|
2014-03-15 01:34:49 +08:00
|
|
|
from django.db.backends.signals import connection_created
|
2019-01-11 02:06:14 +08:00
|
|
|
from django.db.migrations.writer import MigrationWriter
|
2014-09-06 04:53:11 +08:00
|
|
|
from django.db.models import CharField, TextField
|
2018-11-13 00:15:48 +08:00
|
|
|
from django.test.signals import setting_changed
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2015-06-06 00:37:48 +08:00
|
|
|
from .lookups import SearchLookup, TrigramSimilar, Unaccent
|
2019-01-11 02:06:14 +08:00
|
|
|
from .serializers import RangeSerializer
|
2017-05-03 13:25:30 +08:00
|
|
|
from .signals import register_type_handlers
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2019-01-11 02:06:14 +08:00
|
|
|
RANGE_TYPES = (DateRange, DateTimeRange, DateTimeTZRange, NumericRange)
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2018-11-13 00:15:48 +08:00
|
|
|
def uninstall_if_needed(setting, value, enter, **kwargs):
|
|
|
|
"""
|
|
|
|
Undo the effects of PostgresConfig.ready() when django.contrib.postgres
|
|
|
|
is "uninstalled" by override_settings().
|
|
|
|
"""
|
|
|
|
if not enter and setting == 'INSTALLED_APPS' and 'django.contrib.postgres' not in set(value):
|
|
|
|
connection_created.disconnect(register_type_handlers)
|
|
|
|
CharField._unregister_lookup(Unaccent)
|
|
|
|
TextField._unregister_lookup(Unaccent)
|
|
|
|
CharField._unregister_lookup(SearchLookup)
|
|
|
|
TextField._unregister_lookup(SearchLookup)
|
|
|
|
CharField._unregister_lookup(TrigramSimilar)
|
|
|
|
TextField._unregister_lookup(TrigramSimilar)
|
|
|
|
# Disconnect this receiver until the next time this app is installed
|
|
|
|
# and ready() connects it again to prevent unnecessary processing on
|
|
|
|
# each setting change.
|
|
|
|
setting_changed.disconnect(uninstall_if_needed)
|
2019-01-11 02:06:14 +08:00
|
|
|
MigrationWriter.unregister_serializer(RANGE_TYPES)
|
2018-11-13 00:15:48 +08:00
|
|
|
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
class PostgresConfig(AppConfig):
|
|
|
|
name = 'django.contrib.postgres'
|
|
|
|
verbose_name = _('PostgreSQL extensions')
|
|
|
|
|
|
|
|
def ready(self):
|
2018-11-13 00:15:48 +08:00
|
|
|
setting_changed.connect(uninstall_if_needed)
|
2016-07-20 16:18:11 +08:00
|
|
|
# Connections may already exist before we are called.
|
|
|
|
for conn in connections.all():
|
2017-09-09 20:39:09 +08:00
|
|
|
if conn.vendor == 'postgresql':
|
|
|
|
conn.introspection.data_types_reverse.update({
|
2018-04-10 00:46:31 +08:00
|
|
|
3802: 'django.contrib.postgres.fields.JSONField',
|
|
|
|
3904: 'django.contrib.postgres.fields.IntegerRangeField',
|
2018-10-03 07:17:23 +08:00
|
|
|
3906: 'django.contrib.postgres.fields.DecimalRangeField',
|
2018-04-10 00:46:31 +08:00
|
|
|
3910: 'django.contrib.postgres.fields.DateTimeRangeField',
|
|
|
|
3912: 'django.contrib.postgres.fields.DateRangeField',
|
|
|
|
3926: 'django.contrib.postgres.fields.BigIntegerRangeField',
|
2017-09-09 20:39:09 +08:00
|
|
|
})
|
|
|
|
if conn.connection is not None:
|
|
|
|
register_type_handlers(conn)
|
2017-05-03 13:25:30 +08:00
|
|
|
connection_created.connect(register_type_handlers)
|
2014-09-06 04:53:11 +08:00
|
|
|
CharField.register_lookup(Unaccent)
|
|
|
|
TextField.register_lookup(Unaccent)
|
2015-06-01 05:45:03 +08:00
|
|
|
CharField.register_lookup(SearchLookup)
|
|
|
|
TextField.register_lookup(SearchLookup)
|
2015-06-06 00:37:48 +08:00
|
|
|
CharField.register_lookup(TrigramSimilar)
|
|
|
|
TextField.register_lookup(TrigramSimilar)
|
2019-01-11 02:06:14 +08:00
|
|
|
MigrationWriter.register_serializer(RANGE_TYPES, RangeSerializer)
|