-
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.
Added ObjectNotFoundMixin and unit test
- Loading branch information
Showing
7 changed files
with
115 additions
and
72 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.http import Http404 | ||
from django.shortcuts import render | ||
from django.views.generic.detail import SingleObjectMixin | ||
|
||
|
||
class ObjectNotFoundMixin(SingleObjectMixin): | ||
|
||
def dispatch(self, request, *args, **kwargs): | ||
pk = self.kwargs.get(self.pk_url_kwarg) | ||
try: | ||
self.get_object() | ||
except Http404: | ||
object_type = self.model.__name__ | ||
return object_not_found(self.request, pk, object_type) | ||
|
||
return super().dispatch(request, *args, **kwargs) | ||
|
||
|
||
def object_not_found(request, pk, object_type): | ||
return render(request, 'obj_not_found.html', | ||
{'title': object_type + ' not found', 'object_id': pk, 'object_type': object_type}) | ||
|
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,25 @@ | ||
from django.test import RequestFactory, TestCase | ||
|
||
from letters.mixins import ObjectNotFoundMixin | ||
from letters.models import Letter | ||
|
||
|
||
class ObjectNotFoundMixinTestCase(TestCase): | ||
""" | ||
ObjectNotFoundMixin.dispatch() should return rendered html giving details about the object that wasn't found | ||
""" | ||
|
||
def test_dispatch(self): | ||
request = RequestFactory() | ||
mixin = ObjectNotFoundMixin() | ||
mixin.model = Letter | ||
mixin.queryset = Letter.objects.all() | ||
mixin.request = request | ||
mixin.kwargs = {'pk': 1} | ||
|
||
response = mixin.dispatch(request) | ||
|
||
self.assertTrue('<title>Letter not found</title>' in str(response.content), | ||
"ObjectNotFoundMixin response content should include '<object_type> not found'") | ||
self.assertTrue('ID 1' in str(response.content), | ||
"ObjectNotFoundMixin) response content should include 'ID <object_id>' if it's for a letter") |
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
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