|
1 | 1 | from django.contrib.auth.models import User |
2 | | -from rest_framework import generics |
3 | 2 | from rest_framework import permissions |
4 | 3 | from rest_framework import renderers |
5 | | -from rest_framework.decorators import api_view |
| 4 | +from rest_framework import viewsets |
| 5 | +from rest_framework.decorators import link |
6 | 6 | from rest_framework.response import Response |
7 | | -from rest_framework.reverse import reverse |
8 | 7 | from snippets.models import Snippet |
9 | 8 | from snippets.permissions import IsOwnerOrReadOnly |
10 | 9 | from snippets.serializers import SnippetSerializer, UserSerializer |
11 | 10 |
|
12 | | -@api_view(('GET',)) |
13 | | -def api_root(request, format=None): |
14 | | - """ |
15 | | - This is the entry point for the API described in the |
16 | | - [REST framework tutorial][tutorial]. |
17 | | -
|
18 | | - Follow the hyperinks each resource offers to explore the API. |
19 | | -
|
20 | | - Note that you can also explore the API from the command line, for instance |
21 | | - using the `curl` command-line tool. |
22 | | -
|
23 | | - For example: `curl -X GET http://restframework.herokuapp.com/ -H "Accept: application/json; indent=4"` |
24 | 11 |
|
25 | | - [tutorial]: http://django-rest-framework.org/tutorial/1-serialization.html |
| 12 | +class SnippetViewSet(viewsets.ModelViewSet): |
26 | 13 | """ |
27 | | - return Response({ |
28 | | - 'users': reverse('user-list', request=request, format=format), |
29 | | - 'snippets': reverse('snippet-list', request=request, format=format) |
30 | | - }) |
31 | | - |
32 | | - |
33 | | -class SnippetList(generics.ListCreateAPIView): |
34 | | - """ |
35 | | - This view presents a list of code snippets, and allows new snippets to |
36 | | - be created. |
37 | | -
|
38 | | - Try it yourself by logging in as one of these four users: **amy**, **max**, |
39 | | - **jose** or **aziz** - The passwords are the same as the usernames. |
40 | | -
|
41 | | - Note that code snippets are paginated to a maximum of 10 per page, |
42 | | - and the maximum number of code snippets is capped at 100 instances. |
43 | | - """ |
44 | | - model = Snippet |
45 | | - serializer_class = SnippetSerializer |
46 | | - permission_classes = (permissions.IsAuthenticatedOrReadOnly,) |
47 | | - |
48 | | - def pre_save(self, obj): |
49 | | - obj.owner = self.request.user |
50 | | - |
51 | | - |
52 | | -class SnippetDetail(generics.RetrieveUpdateDestroyAPIView): |
53 | | - """ |
54 | | - This view presents an instance of a code snippet. |
| 14 | + This endpoint presents code snippets. |
55 | 15 |
|
56 | 16 | The `highlight` field presents a hyperlink to the hightlighted HTML |
57 | 17 | representation of the code snippet. |
58 | 18 |
|
59 | | - The **owner** of the code snippet may update or delete this instance. |
| 19 | + The **owner** of the code snippet may update or delete instances |
| 20 | + of the code snippet. |
60 | 21 |
|
61 | 22 | Try it yourself by logging in as one of these four users: **amy**, **max**, |
62 | | - **jose** or **aziz** - The passwords are the same as the usernames. |
| 23 | + **jose** or **aziz**. The passwords are the same as the usernames. |
63 | 24 | """ |
64 | | - model = Snippet |
| 25 | + queryset = Snippet.objects.all() |
65 | 26 | serializer_class = SnippetSerializer |
66 | 27 | permission_classes = (permissions.IsAuthenticatedOrReadOnly, |
67 | 28 | IsOwnerOrReadOnly,) |
68 | 29 |
|
69 | | - def pre_save(self, obj): |
70 | | - obj.owner = self.request.user |
71 | | - |
72 | | - |
73 | | -class SnippetHighlight(generics.SingleObjectAPIView): |
74 | | - model = Snippet |
75 | | - renderer_classes = (renderers.StaticHTMLRenderer,) |
76 | | - |
77 | | - def get(self, request, *args, **kwargs): |
| 30 | + @link(renderer_classes=(renderers.StaticHTMLRenderer,)) |
| 31 | + def highlight(self, request, *args, **kwargs): |
78 | 32 | snippet = self.get_object() |
79 | 33 | return Response(snippet.highlighted) |
80 | 34 |
|
| 35 | + def pre_save(self, obj): |
| 36 | + obj.owner = self.request.user |
81 | 37 |
|
82 | | -class UserList(generics.ListAPIView): |
83 | | - """ |
84 | | - This view presents a list of all the users in the system. |
85 | 38 |
|
86 | | - As you can see, related snippet instances are serialized using a |
87 | | - hyperlinked representation. |
| 39 | +class UserViewSet(viewsets.ReadOnlyModelViewSet): |
88 | 40 | """ |
89 | | - model = User |
90 | | - serializer_class = UserSerializer |
91 | | - |
| 41 | + This endpoint presents the users in the system. |
92 | 42 |
|
93 | | -class UserDetail(generics.RetrieveAPIView): |
94 | | - """ |
95 | | - This view presents a instance of one of the users in the system. |
| 43 | + As you can see, the collection of snippet instances owned by a user are |
| 44 | + serialized using a hyperlinked representation. |
96 | 45 | """ |
97 | | - model = User |
| 46 | + queryset = User.objects.all() |
98 | 47 | serializer_class = UserSerializer |
0 commit comments