Skip to content

Commit c48b751

Browse files
authored
Topic/1112 (#1113)
1 parent 769c8e7 commit c48b751

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ Vinay Karanam
7070
Eduardo Oliveira
7171
Andrea Greco
7272
Dominik George
73+
David Hill

docs/tutorial/tutorial_03.rst

+32
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,35 @@ Now supposing your access token value is `123456` you can try to access your aut
7878
::
7979

8080
curl -H "Authorization: Bearer 123456" -X GET http://localhost:8000/secret
81+
82+
Working with Rest_framework generic class based views
83+
-----------------------------------------------------
84+
85+
If you have completed the `Django REST framework tutorial
86+
<https://www.django-rest-framework.org/tutorial/3-class-based-views/#using-generic-class-based-views>`_,
87+
you will be familiar with the 'Snippet' example, in particular the SnippetList and SnippetDetail classes.
88+
89+
It would be nice to reuse those views **and** support token handling. Instead of reworking
90+
those classes to be ProtectedResourceView based, the solution is much simpler than that.
91+
92+
Assume you have already modified the settings as was already shown.
93+
The key is setting a class attribute to override the default *permissions_classes* with something that will use our :term:`Access Token` properly.
94+
95+
.. code-block:: python
96+
97+
from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope
98+
99+
class SnippetList(generics.ListCreateAPIView):
100+
...
101+
permission_classes = [TokenHasReadWriteScope]
102+
103+
class SnippetDetail(generics.ListCreateAPIView):
104+
...
105+
permission_classes = [TokenHasReadWriteScope]
106+
107+
Note that this example overrides the Django default permission class setting. There are several other
108+
ways this can be solved. Overriding the class function *get_permission_classes* is another way
109+
to solve the problem.
110+
111+
A detailed dive into the `Dango REST framework permissions is here. <https://www.django-rest-framework.org/api-guide/permissions/>`_
112+

0 commit comments

Comments
 (0)