Commit e385a7b8 by Tom Christie

Merge master

parents 2aad8e4b 48b66ec2
...@@ -8,23 +8,23 @@ python: ...@@ -8,23 +8,23 @@ python:
- "3.4" - "3.4"
env: env:
- DJANGO="https://www.djangoproject.com/download/1.7.b4/tarball/" - DJANGO="https://www.djangoproject.com/download/1.7c2/tarball/"
- DJANGO="django==1.6.5" - DJANGO="django==1.6.5"
- DJANGO="django==1.5.8" - DJANGO="django==1.5.8"
- DJANGO="django==1.4.13" - DJANGO="django==1.4.13"
install: install:
- pip install $DJANGO - pip install $DJANGO
- pip install defusedxml==0.3 Pillow==2.3.0 - pip install defusedxml==0.3
- pip install Pillow==2.3.0
- pip install django-guardian==1.2.3
- pip install pytest-django==2.6.1 - pip install pytest-django==2.6.1
- "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install oauth2==1.5.211; fi" - "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install oauth2==1.5.211; fi"
- "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-oauth-plus==2.2.4; fi" - "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-oauth-plus==2.2.4; fi"
- "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-oauth2-provider==0.2.4; fi" - "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-oauth2-provider==0.2.4; fi"
- "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-guardian==1.1.1; fi"
- "if [[ ${DJANGO::11} == 'django==1.3' ]]; then pip install django-filter==0.5.4; fi" - "if [[ ${DJANGO::11} == 'django==1.3' ]]; then pip install django-filter==0.5.4; fi"
- "if [[ ${DJANGO::11} != 'django==1.3' ]]; then pip install django-filter==0.7; fi" - "if [[ ${DJANGO::11} != 'django==1.3' ]]; then pip install django-filter==0.7; fi"
- "if [[ ${TRAVIS_PYTHON_VERSION::1} == '3' ]]; then pip install -e git+https://github.com/linovia/django-guardian.git@feature/django_1_7#egg=django-guardian-1.2.0; fi" - "if [[ ${DJANGO} == 'https://www.djangoproject.com/download/1.7c2/tarball/' ]]; then pip install -e git+https://github.com/linovia/django-guardian.git@feature/django_1_7#egg=django-guardian-1.2.0; fi"
- "if [[ ${DJANGO} == 'https://www.djangoproject.com/download/1.7.b4/tarball/' ]]; then pip install -e git+https://github.com/linovia/django-guardian.git@feature/django_1_7#egg=django-guardian-1.2.0; fi"
- export PYTHONPATH=. - export PYTHONPATH=.
script: script:
...@@ -33,7 +33,7 @@ script: ...@@ -33,7 +33,7 @@ script:
matrix: matrix:
exclude: exclude:
- python: "2.6" - python: "2.6"
env: DJANGO="https://www.djangoproject.com/download/1.7.b4/tarball/" env: DJANGO="https://www.djangoproject.com/download/1.7c2/tarball/"
- python: "3.2" - python: "3.2"
env: DJANGO="django==1.4.13" env: DJANGO="django==1.4.13"
- python: "3.3" - python: "3.3"
......
---
#### Django REST framework 3 - Kickstarter announcement!
We are currently running a Kickstarter campaign to help fund the development of Django REST framework 3.
If you want to help drive sustainable open-source development forward, then **please check out [the Kickstarter project](https://www.kickstarter.com/projects/tomchristie/django-rest-framework-3) and consider funding us.**
---
# Django REST framework # Django REST framework
[![build-status-image]][travis] [![build-status-image]][travis]
...@@ -18,7 +8,7 @@ If you want to help drive sustainable open-source development forward, then **pl ...@@ -18,7 +8,7 @@ If you want to help drive sustainable open-source development forward, then **pl
# Overview # Overview
Django REST framework is a powerful and flexible toolkit that makes it easy to build Web APIs. Django REST framework is a powerful and flexible toolkit for building Web APIs.
Some reasons you might want to use REST framework: Some reasons you might want to use REST framework:
......
...@@ -62,7 +62,7 @@ A dictionary of error codes to error messages. ...@@ -62,7 +62,7 @@ A dictionary of error codes to error messages.
### `widget` ### `widget`
Used only if rendering the field to HTML. Used only if rendering the field to HTML.
This argument sets the widget that should be used to render the field. This argument sets the widget that should be used to render the field. For more details, and a list of available widgets, see [the Django documentation on form widgets][django-widgets].
### `label` ### `label`
...@@ -362,12 +362,17 @@ The [drf-compound-fields][drf-compound-fields] package provides "compound" seria ...@@ -362,12 +362,17 @@ The [drf-compound-fields][drf-compound-fields] package provides "compound" seria
The [drf-extra-fields][drf-extra-fields] package provides extra serializer fields for REST framework, including `Base64ImageField` and `PointField` classes. The [drf-extra-fields][drf-extra-fields] package provides extra serializer fields for REST framework, including `Base64ImageField` and `PointField` classes.
## django-rest-framework-gis
The [django-rest-framework-gis][django-rest-framework-gis] package provides geographic addons for django rest framework like a `GeometryField` field and a GeoJSON serializer.
[cite]: https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.cleaned_data [cite]: https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.cleaned_data
[FILE_UPLOAD_HANDLERS]: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FILE_UPLOAD_HANDLERS [FILE_UPLOAD_HANDLERS]: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FILE_UPLOAD_HANDLERS
[ecma262]: http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 [ecma262]: http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
[strftime]: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior [strftime]: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
[django-widgets]: https://docs.djangoproject.com/en/dev/ref/forms/widgets/
[iso8601]: http://www.w3.org/TR/NOTE-datetime [iso8601]: http://www.w3.org/TR/NOTE-datetime
[drf-compound-fields]: http://drf-compound-fields.readthedocs.org [drf-compound-fields]: http://drf-compound-fields.readthedocs.org
[drf-extra-fields]: https://github.com/Hipo/drf-extra-fields [drf-extra-fields]: https://github.com/Hipo/drf-extra-fields
[django-rest-framework-gis]: https://github.com/djangonauts/django-rest-framework-gis
...@@ -43,6 +43,12 @@ For more complex cases you might also want to override various methods on the vi ...@@ -43,6 +43,12 @@ For more complex cases you might also want to override various methods on the vi
return 20 return 20
return 100 return 100
def list(self, request):
# Note the use of `get_queryset()` instead of `self.queryset`
queryset = self.get_queryset()
serializer = UserSerializer(queryset, many=True)
return Response(serializer.data)
For very simple cases you might want to pass through any class attributes using the `.as_view()` method. For example, your URLconf might include something the following entry. For very simple cases you might want to pass through any class attributes using the `.as_view()` method. For example, your URLconf might include something the following entry.
url(r'^/users/', ListCreateAPIView.as_view(model=User), name='user-list') url(r'^/users/', ListCreateAPIView.as_view(model=User), name='user-list')
...@@ -63,7 +69,7 @@ Each of the concrete generic views provided is built by combining `GenericAPIVie ...@@ -63,7 +69,7 @@ Each of the concrete generic views provided is built by combining `GenericAPIVie
The following attributes control the basic view behavior. The following attributes control the basic view behavior.
* `queryset` - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the `get_queryset()` method. * `queryset` - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the `get_queryset()` method. If you are overriding a view method, it is important that you call `get_queryset()` instead of accessing this property directly, as `queryset` will get evaluated once, and those results will be cached for all subsequent requests.
* `serializer_class` - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the `get_serializer_class()` method. * `serializer_class` - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the `get_serializer_class()` method.
* `lookup_field` - The model field that should be used to for performing object lookup of individual model instances. Defaults to `'pk'`. Note that when using hyperlinked APIs you'll need to ensure that *both* the API views *and* the serializer classes set the lookup fields if you need to use a custom value. * `lookup_field` - The model field that should be used to for performing object lookup of individual model instances. Defaults to `'pk'`. Note that when using hyperlinked APIs you'll need to ensure that *both* the API views *and* the serializer classes set the lookup fields if you need to use a custom value.
* `lookup_url_kwarg` - The URL keyword argument that should be used for object lookup. The URL conf should include a keyword argument corresponding to this value. If unset this defaults to using the same value as `lookup_field`. * `lookup_url_kwarg` - The URL keyword argument that should be used for object lookup. The URL conf should include a keyword argument corresponding to this value. If unset this defaults to using the same value as `lookup_field`.
...@@ -93,6 +99,8 @@ The following attributes are used to control pagination when used with list view ...@@ -93,6 +99,8 @@ The following attributes are used to control pagination when used with list view
Returns the queryset that should be used for list views, and that should be used as the base for lookups in detail views. Defaults to returning the queryset specified by the `queryset` attribute, or the default queryset for the model if the `model` shortcut is being used. Returns the queryset that should be used for list views, and that should be used as the base for lookups in detail views. Defaults to returning the queryset specified by the `queryset` attribute, or the default queryset for the model if the `model` shortcut is being used.
This method should always be used rather than accessing `self.queryset` directly, as `self.queryset` gets evaluated only once, and those results are cached for all subsequent requests.
May be overridden to provide dynamic behavior such as returning a queryset that is specific to the user making the request. May be overridden to provide dynamic behavior such as returning a queryset that is specific to the user making the request.
For example: For example:
......
...@@ -244,7 +244,7 @@ The [REST Condition][rest-condition] package is another extension for building c ...@@ -244,7 +244,7 @@ The [REST Condition][rest-condition] package is another extension for building c
[authentication]: authentication.md [authentication]: authentication.md
[throttling]: throttling.md [throttling]: throttling.md
[filtering]: filtering.md [filtering]: filtering.md
[contribauth]: https://docs.djangoproject.com/en/1.0/topics/auth/#permissions [contribauth]: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#custom-permissions
[objectpermissions]: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#handling-object-permissions [objectpermissions]: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#handling-object-permissions
[guardian]: https://github.com/lukaszb/django-guardian [guardian]: https://github.com/lukaszb/django-guardian
[get_objects_for_user]: http://pythonhosted.org/django-guardian/api/guardian.shortcuts.html#get-objects-for-user [get_objects_for_user]: http://pythonhosted.org/django-guardian/api/guardian.shortcuts.html#get-objects-for-user
......
...@@ -580,7 +580,21 @@ The following custom model serializer could be used as a base class for model se ...@@ -580,7 +580,21 @@ The following custom model serializer could be used as a base class for model se
def get_pk_field(self, model_field): def get_pk_field(self, model_field):
return None return None
---
# Third party packages
The following third party packages are also available.
## MongoengineModelSerializer
The [django-rest-framework-mongoengine][mongoengine] package provides a `MongoEngineModelSerializer` serializer class that supports using MongoDB as the storage layer for Django REST framework.
## GeoFeatureModelSerializer
The [django-rest-framework-gis][django-rest-framework-gis] package provides a `GeoFeatureModelSerializer` serializer class that supports GeoJSON both for read and write operations.
[cite]: https://groups.google.com/d/topic/django-users/sVFaOfQi4wY/discussion [cite]: https://groups.google.com/d/topic/django-users/sVFaOfQi4wY/discussion
[relations]: relations.md [relations]: relations.md
[mongoengine]: https://github.com/umutbozkurt/django-rest-framework-mongoengine
[django-rest-framework-gis]: https://github.com/djangonauts/django-rest-framework-gis
...@@ -58,7 +58,7 @@ using the `APIView` class based views. ...@@ -58,7 +58,7 @@ using the `APIView` class based views.
Or, if you're using the `@api_view` decorator with function based views. Or, if you're using the `@api_view` decorator with function based views.
@api_view('GET') @api_view(['GET'])
@throttle_classes([UserRateThrottle]) @throttle_classes([UserRateThrottle])
def example_view(request, format=None): def example_view(request, format=None):
content = { content = {
......
...@@ -307,3 +307,76 @@ table { ...@@ -307,3 +307,76 @@ table {
.side-nav { .side-nav {
overflow-y: scroll; overflow-y: scroll;
} }
ul.sponsor.diamond li a {
float: left;
width: 600px;
height: 20px;
text-align: center;
margin: 10px 70px;
padding: 300px 0 0 0;
background-position: 0 50%;
background-size: 600px auto;
background-repeat: no-repeat;
font-size: 200%;
}
@media (max-width: 1000px) {
ul.sponsor.diamond li a {
float: left;
width: 300px;
height: 20px;
text-align: center;
margin: 10px 40px;
padding: 300px 0 0 0;
background-position: 0 50%;
background-size: 280px auto;
background-repeat: no-repeat;
font-size: 150%;
}
}
ul.sponsor.platinum li a {
float: left;
width: 300px;
height: 20px;
text-align: center;
margin: 10px 40px;
padding: 300px 0 0 0;
background-position: 0 50%;
background-size: 280px auto;
background-repeat: no-repeat;
font-size: 150%;
}
ul.sponsor.gold li a {
float: left;
width: 130px;
height: 20px;
text-align: center;
margin: 10px 30px;
padding: 150px 0 0 0;
background-position: 0 50%;
background-size: 130px auto;
background-repeat: no-repeat;
font-size: 120%;
}
ul.sponsor.silver li a {
float: left;
width: 130px;
height: 20px;
text-align: center;
margin: 10px 30px;
padding: 150px 0 0 0;
background-position: 0 50%;
background-size: 130px auto;
background-repeat: no-repeat;
font-size: 120%;
}
ul.sponsor {
list-style: none;
display: block;
}
...@@ -9,14 +9,6 @@ ...@@ -9,14 +9,6 @@
--- ---
#### Django REST framework 3 - Kickstarter announcement!
We are currently running a Kickstarter campaign to help fund the development of Django REST framework 3.
If you want to help drive sustainable open-source development **please [check out the Kickstarter project](https://www.kickstarter.com/projects/tomchristie/django-rest-framework-3) and consider funding us.**
---
<p> <p>
<h1 style="position: absolute; <h1 style="position: absolute;
width: 1px; width: 1px;
......
...@@ -69,6 +69,7 @@ For more specific CSS tweaks than simply overriding the default bootstrap theme ...@@ -69,6 +69,7 @@ For more specific CSS tweaks than simply overriding the default bootstrap theme
All of the blocks available in the browsable API base template that can be used in your `api.html`. All of the blocks available in the browsable API base template that can be used in your `api.html`.
* `body` - The entire html `<body>`.
* `bodyclass` - Class attribute for the `<body>` tag, empty by default. * `bodyclass` - Class attribute for the `<body>` tag, empty by default.
* `bootstrap_theme` - CSS for the Bootstrap theme. * `bootstrap_theme` - CSS for the Bootstrap theme.
* `bootstrap_navbar_variant` - CSS class for the navbar. * `bootstrap_navbar_variant` - CSS class for the navbar.
...@@ -167,10 +168,10 @@ You can now add the `autocomplete_light.ChoiceWidget` widget to the serializer f ...@@ -167,10 +168,10 @@ You can now add the `autocomplete_light.ChoiceWidget` widget to the serializer f
[bootstrap]: http://getbootstrap.com [bootstrap]: http://getbootstrap.com
[cerulean]: ../img/cerulean.png [cerulean]: ../img/cerulean.png
[slate]: ../img/slate.png [slate]: ../img/slate.png
[bcustomize]: http://twitter.github.com/bootstrap/customize.html#variables [bcustomize]: http://getbootstrap.com/2.3.2/customize.html
[bswatch]: http://bootswatch.com/ [bswatch]: http://bootswatch.com/
[bcomponents]: http://twitter.github.com/bootstrap/components.html [bcomponents]: http://getbootstrap.com/2.3.2/components.html
[bcomponentsnav]: http://twitter.github.com/bootstrap/components.html#navbar [bcomponentsnav]: http://getbootstrap.com/2.3.2/components.html#navbar
[autocomplete-packages]: https://www.djangopackages.com/grids/g/auto-complete/ [autocomplete-packages]: https://www.djangopackages.com/grids/g/auto-complete/
[django-autocomplete-light]: https://github.com/yourlabs/django-autocomplete-light [django-autocomplete-light]: https://github.com/yourlabs/django-autocomplete-light
[django-autocomplete-light-install]: http://django-autocomplete-light.readthedocs.org/en/latest/#install [django-autocomplete-light-install]: http://django-autocomplete-light.readthedocs.org/en/latest/#install
......
...@@ -95,7 +95,7 @@ You can modify the response behavior to `OPTIONS` requests by overriding the `me ...@@ -95,7 +95,7 @@ You can modify the response behavior to `OPTIONS` requests by overriding the `me
To be fully RESTful an API should present its available actions as hypermedia controls in the responses that it sends. To be fully RESTful an API should present its available actions as hypermedia controls in the responses that it sends.
In this approach, rather than documenting the available API endpoints up front, the description instead concentrates on the *media types* that are used. The available actions take may be taken on any given URL are not strictly fixed, but are instead made available by the presence of link and form controls in the returned document. In this approach, rather than documenting the available API endpoints up front, the description instead concentrates on the *media types* that are used. The available actions that may be taken on any given URL are not strictly fixed, but are instead made available by the presence of link and form controls in the returned document.
To implement a hypermedia API you'll need to decide on an appropriate media type for the API, and implement a custom renderer and parser for that media type. The [REST, Hypermedia & HATEOAS][hypermedia-docs] section of the documentation includes pointers to background reading, as well as links to various hypermedia formats. To implement a hypermedia API you'll need to decide on an appropriate media type for the API, and implement a custom renderer and parser for that media type. The [REST, Hypermedia & HATEOAS][hypermedia-docs] section of the documentation includes pointers to background reading, as well as links to various hypermedia formats.
......
...@@ -129,7 +129,7 @@ Then, add the following property to **both** the `SnippetList` and `SnippetDetai ...@@ -129,7 +129,7 @@ Then, add the following property to **both** the `SnippetList` and `SnippetDetai
If you open a browser and navigate to the browsable API at the moment, you'll find that you're no longer able to create new code snippets. In order to do so we'd need to be able to login as a user. If you open a browser and navigate to the browsable API at the moment, you'll find that you're no longer able to create new code snippets. In order to do so we'd need to be able to login as a user.
We can add a login view for use with the browsable API, by editing the URLconf in our project-level urls.py file. We can add a login view for use with the browsable API, by editing the URLconf in our project-level `urls.py` file.
Add the following import at the top of the file: Add the following import at the top of the file:
......
...@@ -4,7 +4,7 @@ At the moment relationships within our API are represented by using primary keys ...@@ -4,7 +4,7 @@ At the moment relationships within our API are represented by using primary keys
## Creating an endpoint for the root of our API ## Creating an endpoint for the root of our API
Right now we have endpoints for 'snippets' and 'users', but we don't have a single entry point to our API. To create one, we'll use a regular function-based view and the `@api_view` decorator we introduced earlier. Right now we have endpoints for 'snippets' and 'users', but we don't have a single entry point to our API. To create one, we'll use a regular function-based view and the `@api_view` decorator we introduced earlier. In your `snippets/views.py` add:
from rest_framework import renderers from rest_framework import renderers
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
...@@ -29,7 +29,7 @@ Unlike all our other API endpoints, we don't want to use JSON, but instead just ...@@ -29,7 +29,7 @@ Unlike all our other API endpoints, we don't want to use JSON, but instead just
The other thing we need to consider when creating the code highlight view is that there's no existing concrete generic view that we can use. We're not returning an object instance, but instead a property of an object instance. The other thing we need to consider when creating the code highlight view is that there's no existing concrete generic view that we can use. We're not returning an object instance, but instead a property of an object instance.
Instead of using a concrete generic view, we'll use the base class for representing instances, and create our own `.get()` method. In your snippets.views add: Instead of using a concrete generic view, we'll use the base class for representing instances, and create our own `.get()` method. In your `snippets/views.py` add:
from rest_framework import renderers from rest_framework import renderers
from rest_framework.response import Response from rest_framework.response import Response
...@@ -43,7 +43,7 @@ Instead of using a concrete generic view, we'll use the base class for represent ...@@ -43,7 +43,7 @@ Instead of using a concrete generic view, we'll use the base class for represent
return Response(snippet.highlighted) return Response(snippet.highlighted)
As usual we need to add the new views that we've created in to our URLconf. As usual we need to add the new views that we've created in to our URLconf.
We'll add a url pattern for our new API root: We'll add a url pattern for our new API root in `snippets/urls.py`:
url(r'^$', 'api_root'), url(r'^$', 'api_root'),
...@@ -73,7 +73,7 @@ The `HyperlinkedModelSerializer` has the following differences from `ModelSerial ...@@ -73,7 +73,7 @@ The `HyperlinkedModelSerializer` has the following differences from `ModelSerial
* Relationships use `HyperlinkedRelatedField`, * Relationships use `HyperlinkedRelatedField`,
instead of `PrimaryKeyRelatedField`. instead of `PrimaryKeyRelatedField`.
We can easily re-write our existing serializers to use hyperlinking. We can easily re-write our existing serializers to use hyperlinking. In your `snippets/serializers.py` add:
class SnippetSerializer(serializers.HyperlinkedModelSerializer): class SnippetSerializer(serializers.HyperlinkedModelSerializer):
owner = serializers.Field(source='owner.username') owner = serializers.Field(source='owner.username')
...@@ -105,7 +105,7 @@ If we're going to have a hyperlinked API, we need to make sure we name our URL p ...@@ -105,7 +105,7 @@ If we're going to have a hyperlinked API, we need to make sure we name our URL p
* Our user serializer includes a field that refers to `'snippet-detail'`. * Our user serializer includes a field that refers to `'snippet-detail'`.
* Our snippet and user serializers include `'url'` fields that by default will refer to `'{model_name}-detail'`, which in this case will be `'snippet-detail'` and `'user-detail'`. * Our snippet and user serializers include `'url'` fields that by default will refer to `'{model_name}-detail'`, which in this case will be `'snippet-detail'` and `'user-detail'`.
After adding all those names into our URLconf, our final `'urls.py'` file should look something like this: After adding all those names into our URLconf, our final `snippets/urls.py` file should look something like this:
# API endpoints # API endpoints
urlpatterns = format_suffix_patterns(patterns('snippets.views', urlpatterns = format_suffix_patterns(patterns('snippets.views',
......
...@@ -6,8 +6,8 @@ We're going to create a simple API to allow admin users to view and edit the use ...@@ -6,8 +6,8 @@ We're going to create a simple API to allow admin users to view and edit the use
Create a new Django project named `tutorial`, then start a new app called `quickstart`. Create a new Django project named `tutorial`, then start a new app called `quickstart`.
# Set up a new project # Create the project directory
django-admin.py startproject tutorial mkdir tutorial
cd tutorial cd tutorial
# Create a virtualenv to isolate our package dependencies locally # Create a virtualenv to isolate our package dependencies locally
...@@ -18,6 +18,9 @@ Create a new Django project named `tutorial`, then start a new app called `quick ...@@ -18,6 +18,9 @@ Create a new Django project named `tutorial`, then start a new app called `quick
pip install django pip install django
pip install djangorestframework pip install djangorestframework
# Set up a new project
django-admin.py startproject tutorial
# Create a new app # Create a new app
python manage.py startapp quickstart python manage.py startapp quickstart
......
...@@ -142,7 +142,7 @@ for (dirpath, dirnames, filenames) in os.walk(docs_dir): ...@@ -142,7 +142,7 @@ for (dirpath, dirnames, filenames) in os.walk(docs_dir):
toc += template + '\n' toc += template + '\n'
if filename == 'index.md': if filename == 'index.md':
main_title = 'Django REST framework - APIs made easy' main_title = 'Django REST framework - Web APIs for Django'
else: else:
main_title = main_title + ' - Django REST framework' main_title = main_title + ' - Django REST framework'
......
...@@ -310,6 +310,13 @@ class OAuth2Authentication(BaseAuthentication): ...@@ -310,6 +310,13 @@ class OAuth2Authentication(BaseAuthentication):
auth = get_authorization_header(request).split() auth = get_authorization_header(request).split()
if len(auth) == 1:
msg = 'Invalid bearer header. No credentials provided.'
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = 'Invalid bearer header. Token string should not contain spaces.'
raise exceptions.AuthenticationFailed(msg)
if auth and auth[0].lower() == b'bearer': if auth and auth[0].lower() == b'bearer':
access_token = auth[1] access_token = auth[1]
elif 'access_token' in request.POST: elif 'access_token' in request.POST:
...@@ -319,13 +326,6 @@ class OAuth2Authentication(BaseAuthentication): ...@@ -319,13 +326,6 @@ class OAuth2Authentication(BaseAuthentication):
else: else:
return None return None
if len(auth) == 1:
msg = 'Invalid bearer header. No credentials provided.'
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = 'Invalid bearer header. Token string should not contain spaces.'
raise exceptions.AuthenticationFailed(msg)
return self.authenticate_credentials(request, access_token) return self.authenticate_credentials(request, access_token)
def authenticate_credentials(self, request, access_token): def authenticate_credentials(self, request, access_token):
......
...@@ -44,12 +44,15 @@ except ImportError: ...@@ -44,12 +44,15 @@ except ImportError:
django_filters = None django_filters = None
# django-guardian is optional # Django-guardian is optional. Import only if guardian is in INSTALLED_APPS
try: # Fixes (#1712). We keep the try/except for the test suite.
guardian = None
if 'guardian' in settings.INSTALLED_APPS:
try:
import guardian import guardian
import guardian.shortcuts # Fixes #1624 import guardian.shortcuts # Fixes #1624
except ImportError: except ImportError:
guardian = None pass
# cStringIO only if it's available, otherwise StringIO # cStringIO only if it's available, otherwise StringIO
......
...@@ -116,6 +116,10 @@ class OrderingFilter(BaseFilterBackend): ...@@ -116,6 +116,10 @@ class OrderingFilter(BaseFilterBackend):
def get_ordering(self, request): def get_ordering(self, request):
""" """
Ordering is set by a comma delimited ?ordering=... query parameter. Ordering is set by a comma delimited ?ordering=... query parameter.
The `ordering` query parameter can be overridden by setting
the `ordering_param` value on the OrderingFilter or by
specifying an `ORDERING_PARAM` value in the API settings.
""" """
params = request.QUERY_PARAMS.get(self.ordering_param) params = request.QUERY_PARAMS.get(self.ordering_param)
if params: if params:
......
...@@ -43,6 +43,10 @@ class GenericAPIView(views.APIView): ...@@ -43,6 +43,10 @@ class GenericAPIView(views.APIView):
# You'll need to either set these attributes, # You'll need to either set these attributes,
# or override `get_queryset()`/`get_serializer_class()`. # or override `get_queryset()`/`get_serializer_class()`.
# If you are overriding a view method, it is important that you call
# `get_queryset()` instead of accessing the `queryset` property directly,
# as `queryset` will get evaluated only once, and those results are cached
# for all subsequent requests.
queryset = None queryset = None
serializer_class = None serializer_class = None
...@@ -185,7 +189,13 @@ class GenericAPIView(views.APIView): ...@@ -185,7 +189,13 @@ class GenericAPIView(views.APIView):
""" """
Returns the list of filter backends that this view requires. Returns the list of filter backends that this view requires.
""" """
filter_backends = self.filter_backends or [] if self.filter_backends is None:
filter_backends = []
else:
# Note that we are returning a *copy* of the class attribute,
# so that it is safe for the view to mutate it if needed.
filter_backends = list(self.filter_backends)
if not filter_backends and self.filter_backend: if not filter_backends and self.filter_backend:
warnings.warn( warnings.warn(
'The `filter_backend` attribute and `FILTER_BACKEND` setting ' 'The `filter_backend` attribute and `FILTER_BACKEND` setting '
...@@ -195,6 +205,7 @@ class GenericAPIView(views.APIView): ...@@ -195,6 +205,7 @@ class GenericAPIView(views.APIView):
DeprecationWarning, stacklevel=2 DeprecationWarning, stacklevel=2
) )
filter_backends = [self.filter_backend] filter_backends = [self.filter_backend]
return filter_backends return filter_backends
...@@ -258,6 +269,10 @@ class GenericAPIView(views.APIView): ...@@ -258,6 +269,10 @@ class GenericAPIView(views.APIView):
This must be an iterable, and may be a queryset. This must be an iterable, and may be a queryset.
Defaults to using `self.queryset`. Defaults to using `self.queryset`.
This method should always be used rather than accessing `self.queryset`
directly, as `self.queryset` gets evaluated only once, and those results
are cached for all subsequent requests.
You may want to override this if you need to provide different You may want to override this if you need to provide different
querysets depending on the incoming request. querysets depending on the incoming request.
......
...@@ -54,32 +54,37 @@ class JSONRenderer(BaseRenderer): ...@@ -54,32 +54,37 @@ class JSONRenderer(BaseRenderer):
format = 'json' format = 'json'
encoder_class = encoders.JSONEncoder encoder_class = encoders.JSONEncoder
ensure_ascii = True ensure_ascii = True
charset = None
# JSON is a binary encoding, that can be encoded as utf-8, utf-16 or utf-32. # We don't set a charset because JSON is a binary encoding,
# that can be encoded as utf-8, utf-16 or utf-32.
# See: http://www.ietf.org/rfc/rfc4627.txt # See: http://www.ietf.org/rfc/rfc4627.txt
# Also: http://lucumr.pocoo.org/2013/7/19/application-mimetypes-and-encodings/ # Also: http://lucumr.pocoo.org/2013/7/19/application-mimetypes-and-encodings/
charset = None
def get_indent(self, accepted_media_type, renderer_context):
if accepted_media_type:
# If the media type looks like 'application/json; indent=4',
# then pretty print the result.
base_media_type, params = parse_header(accepted_media_type.encode('ascii'))
try:
return max(min(int(params['indent']), 8), 0)
except (KeyError, ValueError, TypeError):
pass
# If 'indent' is provided in the context, then pretty print the result.
# E.g. If we're being called by the BrowsableAPIRenderer.
return renderer_context.get('indent', None)
def render(self, data, accepted_media_type=None, renderer_context=None): def render(self, data, accepted_media_type=None, renderer_context=None):
""" """
Render `data` into JSON. Render `data` into JSON, returning a bytestring.
""" """
if data is None: if data is None:
return bytes() return bytes()
# If 'indent' is provided in the context, then pretty print the result.
# E.g. If we're being called by the BrowsableAPIRenderer.
renderer_context = renderer_context or {} renderer_context = renderer_context or {}
indent = renderer_context.get('indent', None) indent = self.get_indent(accepted_media_type, renderer_context)
if accepted_media_type:
# If the media type looks like 'application/json; indent=4',
# then pretty print the result.
base_media_type, params = parse_header(accepted_media_type.encode('ascii'))
indent = params.get('indent', indent)
try:
indent = max(min(int(indent), 8), 0)
except (ValueError, TypeError):
indent = None
ret = json.dumps(data, cls=self.encoder_class, ret = json.dumps(data, cls=self.encoder_class,
indent=indent, ensure_ascii=self.ensure_ascii) indent=indent, ensure_ascii=self.ensure_ascii)
......
...@@ -42,13 +42,20 @@ class override_method(object): ...@@ -42,13 +42,20 @@ class override_method(object):
self.view = view self.view = view
self.request = request self.request = request
self.method = method self.method = method
self.action = getattr(view, 'action', None)
def __enter__(self): def __enter__(self):
self.view.request = clone_request(self.request, self.method) self.view.request = clone_request(self.request, self.method)
if self.action is not None:
# For viewsets we also set the `.action` attribute.
action_map = getattr(self.view, 'action_map', {})
self.view.action = action_map.get(self.method.lower())
return self.view.request return self.view.request
def __exit__(self, *args, **kwarg): def __exit__(self, *args, **kwarg):
self.view.request = self.request self.view.request = self.request
if self.action is not None:
self.view.action = self.action
class Empty(object): class Empty(object):
...@@ -280,8 +287,8 @@ class Request(object): ...@@ -280,8 +287,8 @@ class Request(object):
self._method = self._request.method self._method = self._request.method
# Allow X-HTTP-METHOD-OVERRIDE header # Allow X-HTTP-METHOD-OVERRIDE header
self._method = self.META.get('HTTP_X_HTTP_METHOD_OVERRIDE', if 'HTTP_X_HTTP_METHOD_OVERRIDE' in self.META:
self._method) self._method = self.META['HTTP_X_HTTP_METHOD_OVERRIDE'].upper()
def _load_stream(self): def _load_stream(self):
""" """
......
...@@ -5,6 +5,7 @@ it is initialized with unrendered data, instead of a pre-rendered string. ...@@ -5,6 +5,7 @@ it is initialized with unrendered data, instead of a pre-rendered string.
The appropriate renderer is called during Django's template response rendering. The appropriate renderer is called during Django's template response rendering.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
import django
from django.core.handlers.wsgi import STATUS_CODE_TEXT from django.core.handlers.wsgi import STATUS_CODE_TEXT
from django.template.response import SimpleTemplateResponse from django.template.response import SimpleTemplateResponse
from rest_framework.compat import six from rest_framework.compat import six
...@@ -15,8 +16,11 @@ class Response(SimpleTemplateResponse): ...@@ -15,8 +16,11 @@ class Response(SimpleTemplateResponse):
An HttpResponse that allows its data to be rendered into An HttpResponse that allows its data to be rendered into
arbitrary media types. arbitrary media types.
""" """
# TODO: remove that once Django 1.3 isn't supported
if django.VERSION >= (1, 4):
rendering_attrs = SimpleTemplateResponse.rendering_attrs + ['_closable_objects']
def __init__(self, data=None, status=200, def __init__(self, data=None, status=None,
template_name=None, headers=None, template_name=None, headers=None,
exception=False, content_type=None): exception=False, content_type=None):
""" """
......
...@@ -976,7 +976,7 @@ class ModelSerializer(Serializer): ...@@ -976,7 +976,7 @@ class ModelSerializer(Serializer):
try: try:
setattr(instance, key, val) setattr(instance, key, val)
except ValueError: except ValueError:
self._errors[key] = self.error_messages['required'] self._errors[key] = [self.error_messages['required']]
# Any relations that cannot be set until we've # Any relations that cannot be set until we've
# saved the model get hidden away on these # saved the model get hidden away on these
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
{% endblock %} {% endblock %}
</head> </head>
{% block body %}
<body class="{% block bodyclass %}{% endblock %} container"> <body class="{% block bodyclass %}{% endblock %} container">
<div class="wrapper"> <div class="wrapper">
...@@ -94,7 +95,7 @@ ...@@ -94,7 +95,7 @@
{% endif %} {% endif %}
{% if options_form %} {% if options_form %}
<form class="button-form" action="{{ request.get_full_path }}" method="POST" class="pull-right"> <form class="button-form" action="{{ request.get_full_path }}" method="POST">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="{{ api_settings.FORM_METHOD_OVERRIDE }}" value="OPTIONS" /> <input type="hidden" name="{{ api_settings.FORM_METHOD_OVERRIDE }}" value="OPTIONS" />
<button class="btn btn-primary js-tooltip" title="Make an OPTIONS request on the {{ name }} resource">OPTIONS</button> <button class="btn btn-primary js-tooltip" title="Make an OPTIONS request on the {{ name }} resource">OPTIONS</button>
...@@ -102,7 +103,7 @@ ...@@ -102,7 +103,7 @@
{% endif %} {% endif %}
{% if delete_form %} {% if delete_form %}
<form class="button-form" action="{{ request.get_full_path }}" method="POST" class="pull-right"> <form class="button-form" action="{{ request.get_full_path }}" method="POST">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="{{ api_settings.FORM_METHOD_OVERRIDE }}" value="DELETE" /> <input type="hidden" name="{{ api_settings.FORM_METHOD_OVERRIDE }}" value="DELETE" />
<button class="btn btn-danger js-tooltip" title="Make a DELETE request on the {{ name }} resource">DELETE</button> <button class="btn btn-danger js-tooltip" title="Make a DELETE request on the {{ name }} resource">DELETE</button>
...@@ -231,4 +232,5 @@ ...@@ -231,4 +232,5 @@
<script src="{% static "rest_framework/js/default.js" %}"></script> <script src="{% static "rest_framework/js/default.js" %}"></script>
{% endblock %} {% endblock %}
</body> </body>
{% endblock %}
</html> </html>
{% extends "rest_framework/base.html" %}
{% load url from future %} {% load url from future %}
{% load staticfiles %} {% load staticfiles %}
{% load rest_framework %} {% load rest_framework %}
<html>
<head>
{% block style %}
{% block bootstrap_theme %}
<link rel="stylesheet" type="text/css" href="{% static "rest_framework/css/bootstrap.min.css" %}"/>
<link rel="stylesheet" type="text/css" href="{% static "rest_framework/css/bootstrap-tweaks.css" %}"/>
{% endblock %}
<link rel="stylesheet" type="text/css" href="{% static "rest_framework/css/default.css" %}"/>
{% endblock %}
</head>
{% block body %}
<body class="container"> <body class="container">
<div class="container-fluid" style="margin-top: 30px"> <div class="container-fluid" style="margin-top: 30px">
...@@ -51,4 +42,4 @@ ...@@ -51,4 +42,4 @@
</div><!-- /.row-fluid --> </div><!-- /.row-fluid -->
</div><!-- /.container-fluid --> </div><!-- /.container-fluid -->
</body> </body>
</html> {% endblock %}
...@@ -154,6 +154,10 @@ class APIClient(APIRequestFactory, DjangoClient): ...@@ -154,6 +154,10 @@ class APIClient(APIRequestFactory, DjangoClient):
kwargs.update(self._credentials) kwargs.update(self._credentials)
return super(APIClient, self).request(**kwargs) return super(APIClient, self).request(**kwargs)
def logout(self):
self._credentials = {}
return super(APIClient, self).logout()
class APITransactionTestCase(testcases.TransactionTestCase): class APITransactionTestCase(testcases.TransactionTestCase):
client_class = APIClient client_class = APIClient
......
...@@ -2,15 +2,15 @@ ...@@ -2,15 +2,15 @@
Login and logout views for the browsable API. Login and logout views for the browsable API.
Add these to your root URLconf if you're using the browsable API and Add these to your root URLconf if you're using the browsable API and
your API requires authentication. your API requires authentication:
The urls must be namespaced as 'rest_framework', and you should make sure
your authentication settings include `SessionAuthentication`.
urlpatterns = patterns('', urlpatterns = patterns('',
... ...
url(r'^auth', include('rest_framework.urls', namespace='rest_framework')) url(r'^auth', include('rest_framework.urls', namespace='rest_framework'))
) )
The urls must be namespaced as 'rest_framework', and you should make sure
your authentication settings include `SessionAuthentication`.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
from django.conf.urls import patterns, url from django.conf.urls import patterns, url
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment