diff --git a/django_iban/__init__.py b/django_iban/__init__.py index e69de29..d91d7cf 100644 --- a/django_iban/__init__.py +++ b/django_iban/__init__.py @@ -0,0 +1 @@ +default_app_config = 'django_iban.apps.IBANFieldConfig' diff --git a/django_iban/apps.py b/django_iban/apps.py new file mode 100644 index 0000000..4b514bc --- /dev/null +++ b/django_iban/apps.py @@ -0,0 +1,48 @@ +from os.path import join +from os.path import dirname +from os.path import abspath +from django.db import connection +from itertools import chain +from django.apps import AppConfig +from django.conf import settings +from django.db.models.signals import pre_migrate + +from django_iban.fields import IBANField + + +class IBANFieldConfig(AppConfig): + name = 'django_iban' + verbose_name = 'Django IBAN Field' + + def ready(self): + pre_migrate.connect(self.enforce_database_constraint_callback) + + def enforce_database_constraint_callback(self, sender, **kwargs): + database_backend = settings.DATABASES['default']['ENGINE'] + enforcing_status = any([ + iban_field.enforce_database_constraint + for iban_field in chain.from_iterable([ + model._meta.fields for model in sender.get_models()]) + if isinstance(iban_field, IBANField)]) + + if database_backend in ( + 'django.db.backends.postgresql_psycopg2', + 'django.contrib.gis.db.backends.postgis' + ) and enforcing_status: + cursor = connection.cursor() + base_path = dirname(abspath(__file__)) + + cursor.execute("select * from pg_available_extensions where name = 'plpythonu'") + if cursor.rowcount == 1: + cursor.execute( + """ + SELECT p.proname FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_proc p + ON p.pronamespace = n.oid + WHERE n.nspname = 'public' AND p.proname = 'is_valid_iban'; + """) + + if cursor.rowcount != 1: + with open(join(base_path, 'pg_iban_validator.sql')) as iban_type_definition: + cursor.execute(iban_type_definition.read()) + else: + raise Exception('django_iban requested database enforcing but plpython extension not installed!') diff --git a/django_iban/migrations/0001_initial.py b/django_iban/migrations/0001_initial.py deleted file mode 100644 index aef64aa..0000000 --- a/django_iban/migrations/0001_initial.py +++ /dev/null @@ -1,31 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from os.path import join -from os.path import dirname -from os.path import abspath -from django.db import migrations -from django.db import connection -from django.conf import settings - - -def create_iban_type(*args, **kwargs): - database_backend = settings.DATABASES['default']['ENGINE'] - if database_backend in ( - 'django.db.backends.postgresql_psycopg2', - 'django.contrib.gis.db.backends.postgis' - ): - cursor = connection.cursor() - base_path = dirname(dirname(abspath(__file__))) - cursor.execute("select * from pg_available_extensions where name = 'plpythonu'") - if cursor.rowcount == 1: - with open(join(base_path, 'pg_iban_validator.sql')) as iban_type: - cursor.execute(iban_type.read()) - - -class Migration(migrations.Migration): - dependencies = [] - - operations = [ - migrations.RunPython(create_iban_type), - ] diff --git a/django_iban/migrations/__init__.py b/django_iban/migrations/__init__.py deleted file mode 100644 index e69de29..0000000