|  | @@ -1,7 +1,9 @@
 | 
	
		
			
				|  |  | -from django.shortcuts import render
 | 
	
		
			
				|  |  | +import kwargs as kwargs
 | 
	
		
			
				|  |  | +from django.contrib.auth import login, logout
 | 
	
		
			
				|  |  | +from django.contrib.auth.backends import ModelBackend
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -# Create your views here.
 | 
	
		
			
				|  |  | -from rest_framework import status
 | 
	
		
			
				|  |  | +from rest_framework import status, permissions
 | 
	
		
			
				|  |  | +from rest_framework.decorators import permission_classes
 | 
	
		
			
				|  |  |  from rest_framework.response import Response
 | 
	
		
			
				|  |  |  from rest_framework.views import APIView
 | 
	
		
			
				|  |  |  from rest_framework.viewsets import ModelViewSet
 | 
	
	
		
			
				|  | @@ -24,7 +26,33 @@ class ReportView(APIView):
 | 
	
		
			
				|  |  |      def delete(self, instance):
 | 
	
		
			
				|  |  |          pass
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  class DailyReportView(ModelViewSet):
 | 
	
		
			
				|  |  |      queryset = DailyReport.objects.all()
 | 
	
		
			
				|  |  |      serializer_class = DailyReportSerialiser
 | 
	
		
			
				|  |  | +    filter_fields = {'title': ['exact']}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def update(self, request, *args, **kwargs):
 | 
	
		
			
				|  |  | +        kwargs['partial'] = True
 | 
	
		
			
				|  |  | +        return super().update(request, *args, **kwargs)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# 登入,权限配置
 | 
	
		
			
				|  |  | +@permission_classes((permissions.AllowAny,))
 | 
	
		
			
				|  |  | +class LoginView(APIView):
 | 
	
		
			
				|  |  | +    def post(self, request):
 | 
	
		
			
				|  |  | +        data = request.data
 | 
	
		
			
				|  |  | +        user = ModelBackend().authenticate(username=data['username'], password=data['password'], request=request)
 | 
	
		
			
				|  |  | +        # user = authenticate(username=data['username'], password=data['password'], request=request)
 | 
	
		
			
				|  |  | +        if user is None:
 | 
	
		
			
				|  |  | +            return Response(data={'msg': '用户名或密码不正确'}, status=status.HTTP_404_NOT_FOUND)
 | 
	
		
			
				|  |  | +        else:
 | 
	
		
			
				|  |  | +            login(request, user=user, backend='django.contrib.auth.backends.ModelBackend')
 | 
	
		
			
				|  |  | +            # login(request, user=user)
 | 
	
		
			
				|  |  | +        return Response(status=status.HTTP_200_OK, data={'user_id': user.id})
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +class LogoutView(APIView):
 | 
	
		
			
				|  |  | +    def post(self, request):
 | 
	
		
			
				|  |  | +        logout(request)
 | 
	
		
			
				|  |  | +        return Response(data={'msg': 'logout success!'}, status=status.HTTP_200_OK)
 |