From cb431687a79899102b3ca05a558a03cbc8c5c384 Mon Sep 17 00:00:00 2001 From: Tina Zhang Date: Wed, 19 Oct 2016 21:40:55 +0100 Subject: [PATCH] Remove CorsModel and provide AbstractCorsModel instead --- HISTORY.rst | 6 ++++++ README.rst | 5 ++--- corsheaders/models.py | 11 +++++------ tests/settings.py | 1 + tests/test_middleware.py | 11 +++++++++-- tests/test_model.py | 11 ----------- tests/testapp/__init__.py | 0 tests/testapp/models.py | 5 +++++ 8 files changed, 28 insertions(+), 22 deletions(-) delete mode 100644 tests/test_model.py create mode 100644 tests/testapp/__init__.py create mode 100644 tests/testapp/models.py diff --git a/HISTORY.rst b/HISTORY.rst index da2e05ed..27557b97 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -5,6 +5,12 @@ Pending ------- * New release notes go here. +* Remove previously undocumented ``CorsModel`` as it was causing migration + issues. For backwards compatibility, any users previously using ``CorsModel`` + should create a model in their own app that inherits from the new + ``AbstractCorsModel``, and to keep using the same data, set the model's + ``db_table`` to 'corsheaders_corsmodel'. Users not using ``CorsModel`` + will find they have an unused table that they can drop. 1.3.1 (2016-11-09) ------------------ diff --git a/README.rst b/README.rst index 0af05af8..1914ff3a 100644 --- a/README.rst +++ b/README.rst @@ -236,9 +236,8 @@ undo the header replacement: If set, this should be the path to a model to look up allowed origins, in the form ``app.modelname``. Defaults to ``None``. -The model should have one field, a ``CharField`` called ``cors``, that -in each instance contains an allowed origin. ``django-cors-headers`` supplies -such a model for you; set the setting to ``corsheaders.CorsModel`` to use it. +The model should inherit from ``corsheaders.models.AbstractCorsModel`` and specify +the allowed origin in the ``CharField`` called ``cors``. Signals ------- diff --git a/corsheaders/models.py b/corsheaders/models.py index 1f9097b3..17dac610 100644 --- a/corsheaders/models.py +++ b/corsheaders/models.py @@ -1,10 +1,9 @@ from django.db import models -from django.utils.encoding import python_2_unicode_compatible -@python_2_unicode_compatible -class CorsModel(models.Model): - cors = models.CharField(max_length=255) +class AbstractCorsModel(models.Model): + class Meta: + abstract = True + db_table = 'corsheaders_corsmodel' - def __str__(self): - return self.cors + cors = models.CharField(max_length=255) diff --git a/tests/settings.py b/tests/settings.py index edfaabbd..0d09a0b2 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -5,6 +5,7 @@ INSTALLED_APPS = [ 'corsheaders', + 'testapp', ] DATABASES = { diff --git a/tests/test_middleware.py b/tests/test_middleware.py index 82eb4463..51b9a22e 100644 --- a/tests/test_middleware.py +++ b/tests/test_middleware.py @@ -8,7 +8,8 @@ ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE, ) -from corsheaders.models import CorsModel +from corsheaders.signals import check_request_enabled +from testapp.models import CorsModel from .utils import ( append_middleware, @@ -119,7 +120,7 @@ def test_options_will_not_add_origin_when_domain_not_found_in_origin_regex_white resp = self.client.options('/', HTTP_ORIGIN='http://foo.example.com') assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp - @override_settings(CORS_MODEL='corsheaders.CorsModel') + @override_settings(CORS_MODEL='testapp.CorsModel') def test_get_when_custom_model_enabled(self): CorsModel.objects.create(cors='example.com') resp = self.client.get('/', HTTP_ORIGIN='http://example.com') @@ -143,6 +144,12 @@ def test_options_no_header(self): resp = self.client.options('/') assert resp.status_code == 404 + @override_settings(CORS_MODEL='testapp.CorsModel') + def test_process_response_when_custom_model_enabled(self): + CorsModel.objects.create(cors='foo.google.com') + response = self.client.get('/', HTTP_ORIGIN='http://foo.google.com') + assert response.get(ACCESS_CONTROL_ALLOW_ORIGIN, None) == 'http://foo.google.com' + @override_settings( CORS_ALLOW_CREDENTIALS=True, CORS_ORIGIN_ALLOW_ALL=True, diff --git a/tests/test_model.py b/tests/test_model.py deleted file mode 100644 index 926c8bb7..00000000 --- a/tests/test_model.py +++ /dev/null @@ -1,11 +0,0 @@ -from django.test import SimpleTestCase -from django.utils import six - -from corsheaders.models import CorsModel - - -class CorsModelTests(SimpleTestCase): - - def test_str(self): - instance = CorsModel(cors='foo') - assert six.text_type(instance) == 'foo' diff --git a/tests/testapp/__init__.py b/tests/testapp/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/testapp/models.py b/tests/testapp/models.py new file mode 100644 index 00000000..20e8e73c --- /dev/null +++ b/tests/testapp/models.py @@ -0,0 +1,5 @@ +from corsheaders.models import AbstractCorsModel + + +class CorsModel(AbstractCorsModel): + pass