Skip to content

Commit

Permalink
Remove CorsModel and provide AbstractCorsModel instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Tina Zhang authored and adamchainz committed Jan 7, 2017
1 parent 1958434 commit cb43168
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
5 changes: 2 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
11 changes: 5 additions & 6 deletions corsheaders/models.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

INSTALLED_APPS = [
'corsheaders',
'testapp',
]

DATABASES = {
Expand Down
11 changes: 9 additions & 2 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand All @@ -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,
Expand Down
11 changes: 0 additions & 11 deletions tests/test_model.py

This file was deleted.

Empty file added tests/testapp/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from corsheaders.models import AbstractCorsModel


class CorsModel(AbstractCorsModel):
pass

0 comments on commit cb43168

Please sign in to comment.