forked from mongodb-labs/django-mongodb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prevent the creation of embedded models
- Loading branch information
Showing
14 changed files
with
234 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,48 @@ | ||
from django.db import NotSupportedError | ||
from django.db.models.manager import BaseManager | ||
|
||
from .queryset import MongoQuerySet | ||
|
||
|
||
class MongoManager(BaseManager.from_queryset(MongoQuerySet)): | ||
pass | ||
|
||
|
||
class EmbeddedModelManager(BaseManager): | ||
""" | ||
Prevent all queryset operations on embedded models since they don't have | ||
their own collection. | ||
""" | ||
|
||
def get_queryset(self): | ||
raise NotSupportedError("EmbeddedModels cannot be queried.") | ||
|
||
def all(self): | ||
raise NotSupportedError("EmbeddedModels cannot be queried.") | ||
|
||
def get(self, *args, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be queried.") | ||
|
||
def get_or_create(self, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be queried.") | ||
|
||
def filter(self, *args, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be queried.") | ||
|
||
def create(self, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be created.") | ||
|
||
def bulk_create(self, *args, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be created.") | ||
|
||
def update(self, *args, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be updated.") | ||
|
||
def bulk_update(self, *args, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be updated.") | ||
|
||
def update_or_create(self, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be updated or created.") | ||
|
||
def delete(self): | ||
raise NotSupportedError("EmbeddedModels cannot be deleted.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.db import NotSupportedError, models | ||
|
||
from .managers import EmbeddedModelManager | ||
|
||
|
||
class EmbeddedModel(models.Model): | ||
objects = EmbeddedModelManager() | ||
|
||
class Meta: | ||
abstract = True | ||
|
||
def delete(self, *args, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be deleted.") | ||
|
||
def save(self, *args, **kwargs): | ||
raise NotSupportedError("EmbeddedModels cannot be saved.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django_mongodb_backend.models import EmbeddedModel | ||
|
||
|
||
class Embed(EmbeddedModel): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from django.db import NotSupportedError | ||
from django.test import SimpleTestCase | ||
|
||
from .models import Embed | ||
|
||
|
||
class TestMethods(SimpleTestCase): | ||
def test_save(self): | ||
e = Embed() | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be saved."): | ||
e.save() | ||
|
||
def test_delete(self): | ||
e = Embed() | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be deleted."): | ||
e.delete() | ||
|
||
|
||
class TestManagerMethods(SimpleTestCase): | ||
def test_all(self): | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be queried."): | ||
Embed.objects.all() | ||
|
||
def test_get(self): | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be queried."): | ||
Embed.objects.get() | ||
|
||
def test_get_or_create(self): | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be queried."): | ||
Embed.objects.get_or_create() | ||
|
||
def test_filter(self): | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be queried."): | ||
Embed.objects.filter(foo="bar") | ||
|
||
def test_create(self): | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be created."): | ||
Embed.objects.create() | ||
|
||
def test_bulk_create(self): | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be created."): | ||
Embed.objects.bulk_create() | ||
|
||
def test_update(self): | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be updated."): | ||
Embed.objects.update(foo="bar") | ||
|
||
def test_bulk_update(self): | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be updated."): | ||
Embed.objects.bulk_update() | ||
|
||
def test_update_or_create(self): | ||
msg = "EmbeddedModels cannot be updated or created." | ||
with self.assertRaisesMessage(NotSupportedError, msg): | ||
Embed.objects.update_or_create() | ||
|
||
def test_delete(self): | ||
with self.assertRaisesMessage(NotSupportedError, "EmbeddedModels cannot be deleted."): | ||
Embed.objects.delete() |
Oops, something went wrong.