parent
a2d714d85f
commit
8e1a25a9c0
12 changed files with 175 additions and 14 deletions
@ -1,9 +0,0 @@ |
||||
from django.urls import path |
||||
from article import views |
||||
|
||||
app_name = 'article' |
||||
|
||||
urlpatterns = [ |
||||
path('', views.ArticleList.as_view(), name='list'), |
||||
path('<int:pk>/', views.ArticleDetail.as_view(), name='detail') |
||||
] |
@ -0,0 +1,19 @@ |
||||
# Generated by Django 4.1.1 on 2022-09-24 18:36 |
||||
|
||||
from django.db import migrations, models |
||||
import django.db.models.deletion |
||||
|
||||
|
||||
class Migration(migrations.Migration): |
||||
|
||||
dependencies = [ |
||||
('comment', '0001_initial'), |
||||
] |
||||
|
||||
operations = [ |
||||
migrations.AddField( |
||||
model_name='comment', |
||||
name='parent', |
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='comment.comment'), |
||||
), |
||||
] |
Binary file not shown.
@ -0,0 +1,10 @@ |
||||
from rest_framework.permissions import BasePermission, SAFE_METHODS |
||||
|
||||
|
||||
class IsSelfOrReadOnly(BasePermission): |
||||
|
||||
def has_object_permission(self, request, view, obj): |
||||
if request.method in SAFE_METHODS: |
||||
return True |
||||
"""确保非安全方法只能由本人操作""" |
||||
return obj == request.user |
@ -1,3 +1,44 @@ |
||||
from django.shortcuts import render |
||||
from django.contrib.auth.models import User |
||||
from rest_framework import viewsets |
||||
from rest_framework.permissions import AllowAny, IsAuthenticatedOrReadOnly |
||||
|
||||
# Create your views here. |
||||
from user_info.serializers import UserRegisterSerializer |
||||
from user_info.permissions import IsSelfOrReadOnly |
||||
|
||||
from rest_framework.decorators import action |
||||
from rest_framework.response import Response |
||||
from user_info.serializers import UserDetailSerializer |
||||
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet): |
||||
queryset = User.objects.all() |
||||
serializer_class = UserRegisterSerializer |
||||
lookup_field = 'username' # 和UserRegisterSerializer中的url中lookup_field对应 |
||||
|
||||
# http://127.0.0.1:8000/api/user/admin/info/ |
||||
@action(detail=True, methods=['get']) |
||||
def info(self, request, username=None): |
||||
queryset = User.objects.get(username=username) |
||||
serializer = UserDetailSerializer(queryset, many=False) |
||||
return Response(serializer.data) |
||||
|
||||
# http://127.0.0.1:8000/api/user/sorted/ |
||||
@action(detail=False) |
||||
def sorted(self, request): |
||||
users = User.objects.all().order_by('-username') |
||||
# 是否分页 |
||||
page = self.paginate_queryset(users) |
||||
if page is not None: |
||||
serializer = self.get_serializer(page, many=True) |
||||
return self.get_paginated_response(serializer.data) |
||||
serializer = self.get_serializer(users, many=True) |
||||
return Response(serializer.data) |
||||
|
||||
def get_permissions(self): |
||||
# 注册用户的POST请求是允许所有人都可以操作的 |
||||
if self.request.method == 'POST': |
||||
self.permission_classes = [AllowAny] |
||||
else: |
||||
self.permission_classes = [IsAuthenticatedOrReadOnly, IsSelfOrReadOnly] |
||||
|
||||
return super(UserViewSet, self).get_permissions() |
||||
|
Loading…
Reference in new issue