Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

apps.py 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from psycopg2.extras import DateRange, DateTimeRange, DateTimeTZRange, NumericRange
  2. from django.apps import AppConfig
  3. from django.core.signals import setting_changed
  4. from django.db import connections
  5. from django.db.backends.signals import connection_created
  6. from django.db.migrations.writer import MigrationWriter
  7. from django.db.models import CharField, OrderBy, TextField
  8. from django.db.models.functions import Collate
  9. from django.db.models.indexes import IndexExpression
  10. from django.utils.translation import gettext_lazy as _
  11. from .indexes import OpClass
  12. from .lookups import SearchLookup, TrigramSimilar, TrigramWordSimilar, Unaccent
  13. from .serializers import RangeSerializer
  14. from .signals import register_type_handlers
  15. RANGE_TYPES = (DateRange, DateTimeRange, DateTimeTZRange, NumericRange)
  16. def uninstall_if_needed(setting, value, enter, **kwargs):
  17. """
  18. Undo the effects of PostgresConfig.ready() when django.contrib.postgres
  19. is "uninstalled" by override_settings().
  20. """
  21. if (
  22. not enter
  23. and setting == "INSTALLED_APPS"
  24. and "django.contrib.postgres" not in set(value)
  25. ):
  26. connection_created.disconnect(register_type_handlers)
  27. CharField._unregister_lookup(Unaccent)
  28. TextField._unregister_lookup(Unaccent)
  29. CharField._unregister_lookup(SearchLookup)
  30. TextField._unregister_lookup(SearchLookup)
  31. CharField._unregister_lookup(TrigramSimilar)
  32. TextField._unregister_lookup(TrigramSimilar)
  33. CharField._unregister_lookup(TrigramWordSimilar)
  34. TextField._unregister_lookup(TrigramWordSimilar)
  35. # Disconnect this receiver until the next time this app is installed
  36. # and ready() connects it again to prevent unnecessary processing on
  37. # each setting change.
  38. setting_changed.disconnect(uninstall_if_needed)
  39. MigrationWriter.unregister_serializer(RANGE_TYPES)
  40. class PostgresConfig(AppConfig):
  41. name = "django.contrib.postgres"
  42. verbose_name = _("PostgreSQL extensions")
  43. def ready(self):
  44. setting_changed.connect(uninstall_if_needed)
  45. # Connections may already exist before we are called.
  46. for conn in connections.all(initialized_only=True):
  47. if conn.vendor == "postgresql":
  48. conn.introspection.data_types_reverse.update(
  49. {
  50. 3904: "django.contrib.postgres.fields.IntegerRangeField",
  51. 3906: "django.contrib.postgres.fields.DecimalRangeField",
  52. 3910: "django.contrib.postgres.fields.DateTimeRangeField",
  53. 3912: "django.contrib.postgres.fields.DateRangeField",
  54. 3926: "django.contrib.postgres.fields.BigIntegerRangeField",
  55. }
  56. )
  57. if conn.connection is not None:
  58. register_type_handlers(conn)
  59. connection_created.connect(register_type_handlers)
  60. CharField.register_lookup(Unaccent)
  61. TextField.register_lookup(Unaccent)
  62. CharField.register_lookup(SearchLookup)
  63. TextField.register_lookup(SearchLookup)
  64. CharField.register_lookup(TrigramSimilar)
  65. TextField.register_lookup(TrigramSimilar)
  66. CharField.register_lookup(TrigramWordSimilar)
  67. TextField.register_lookup(TrigramWordSimilar)
  68. MigrationWriter.register_serializer(RANGE_TYPES, RangeSerializer)
  69. IndexExpression.register_wrappers(OrderBy, OpClass, Collate)