adds views for user endpoints still WIP
This commit is contained in:
parent
ec0a86bd6e
commit
efa1329432
|
@ -27,5 +27,5 @@ class UserSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ["id", "username", "snippet"]
|
fields = ["id", "username", "snippets"]
|
||||||
|
|
||||||
|
|
|
@ -3,5 +3,7 @@ from snippets import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("snippets/", views.SnippetList.as_view()),
|
path("snippets/", views.SnippetList.as_view()),
|
||||||
path("snippets/<int:pk>/", views.SnippetDetail.as_view())
|
path("snippets/<int:pk>/", views.SnippetDetail.as_view()),
|
||||||
|
path("user/", views.UserList.as_view()),
|
||||||
|
path("user/<int:pk>", views.UserDetail.as_view())
|
||||||
]
|
]
|
|
@ -2,10 +2,13 @@ from rest_framework import status
|
||||||
from rest_framework.decorators import api_view
|
from rest_framework.decorators import api_view
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
|
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
from snippets.models import Snippet
|
from snippets.models import Snippet
|
||||||
from snippets.serializers import SnippetSerializer
|
from snippets.serializers import SnippetSerializer, UserSerializer
|
||||||
|
|
||||||
# @api_view(["GET", "POST"])
|
# @api_view(["GET", "POST"])
|
||||||
# def snippet_list(request):
|
# def snippet_list(request):
|
||||||
|
@ -90,3 +93,27 @@ class SnippetDetail(APIView):
|
||||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
|
class UserList(APIView):
|
||||||
|
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||||
|
|
||||||
|
def get(self, request, format=None):
|
||||||
|
user = User.objects.all()
|
||||||
|
serializer = UserSerializer(user, many=True)
|
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
class UserDetail(APIView):
|
||||||
|
|
||||||
|
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||||
|
|
||||||
|
def get_user(self, pk):
|
||||||
|
try:
|
||||||
|
return User.objects.get(pk=pk)
|
||||||
|
except User.DoesNotExist:
|
||||||
|
raise Http404
|
||||||
|
|
||||||
|
def get(self, request, pk, format=None):
|
||||||
|
user = self.get_user(pk=pk)
|
||||||
|
serializer = UserSerializer(user)
|
||||||
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
{
|
{
|
||||||
"selected_items":
|
"selected_items":
|
||||||
[
|
[
|
||||||
|
[
|
||||||
|
"hight",
|
||||||
|
"highlighted"
|
||||||
|
],
|
||||||
[
|
[
|
||||||
"JSON",
|
"JSON",
|
||||||
"JSONParser"
|
"JSONParser"
|
||||||
|
@ -81,6 +85,7 @@
|
||||||
},
|
},
|
||||||
"file_history":
|
"file_history":
|
||||||
[
|
[
|
||||||
|
"/C/Users/eeman/learning/django-rest-tutorial/.venv/Lib/site-packages/django/contrib/auth/migrations/0005_alter_user_last_login_null.py",
|
||||||
"/C/Users/eeman/learning/django-rest-tutorial/snippets/models.py",
|
"/C/Users/eeman/learning/django-rest-tutorial/snippets/models.py",
|
||||||
"/C/Users/eeman/learning/django-rest-tutorial/snippets/serializers.py",
|
"/C/Users/eeman/learning/django-rest-tutorial/snippets/serializers.py",
|
||||||
"/C/Users/eeman/learning/django-rest-tutorial/.gitignore",
|
"/C/Users/eeman/learning/django-rest-tutorial/.gitignore",
|
||||||
|
@ -146,20 +151,20 @@
|
||||||
"selected_items":
|
"selected_items":
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
"views",
|
"seria",
|
||||||
"snippets\\views.py"
|
"snippets\\serializers.py"
|
||||||
],
|
|
||||||
[
|
|
||||||
"urls",
|
|
||||||
"tutorial\\urls.py"
|
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"model",
|
"model",
|
||||||
"snippets\\models.py"
|
"snippets\\models.py"
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"seria",
|
"urls",
|
||||||
"snippets\\serializers.py"
|
"snippets\\urls.py"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"views",
|
||||||
|
"snippets\\views.py"
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"gitig",
|
"gitig",
|
||||||
|
|
Loading…
Reference in New Issue