-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from Vacasa/feature/response-context-data
Feature: Response Contexts
- Loading branch information
Showing
4 changed files
with
81 additions
and
19 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,5 +1,21 @@ | ||
from rest_framework import status | ||
from rest_framework.response import Response as BaseResponse | ||
|
||
|
||
class Response(BaseResponse): | ||
pass | ||
def __init__(self, *args, **kwargs): | ||
context = kwargs.pop("context", {}) | ||
super(Response, self).__init__(*args, **kwargs) | ||
self.context = context | ||
|
||
@property | ||
def ok(self): | ||
"""Returns True if :attr:`status_code` is less than 400, False if not.""" | ||
if self.status_code >= status.HTTP_400_BAD_REQUEST: | ||
return False | ||
return True | ||
|
||
@property | ||
def created(self): | ||
"""Returns True if :attr:`status_code` is 201, False if not.""" | ||
return self.status_code == status.HTTP_201_CREATED |
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,27 @@ | ||
from django.test import TestCase | ||
from rest_framework import status | ||
|
||
from drf_jsonapi.response import Response | ||
|
||
|
||
class ResponseTestCase(TestCase): | ||
def test_init_with_context(self): | ||
context = {"key": "value"} | ||
response = Response(context=context) | ||
self.assertDictEqual(response.context, context) | ||
|
||
def test_ok_success(self): | ||
response = Response(status=status.HTTP_200_OK) | ||
self.assertTrue(response.ok) | ||
|
||
def test_ok_error(self): | ||
response = Response(status=status.HTTP_400_BAD_REQUEST) | ||
self.assertFalse(response.ok) | ||
|
||
def test_created_true(self): | ||
response = Response(status=status.HTTP_201_CREATED) | ||
self.assertTrue(response.created) | ||
|
||
def test_created_false(self): | ||
response = Response(status=status.HTTP_200_OK) | ||
self.assertFalse(response.created) |