|
@@ -1,11 +1,12 @@
|
|
|
-from django.shortcuts import render
|
|
|
-
|
|
|
-# 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
|
|
|
|
|
|
+from django.contrib.auth import login, logout
|
|
|
+from django.contrib.auth.backends import ModelBackend
|
|
|
+
|
|
|
from workreport.models import DailyReport
|
|
|
from workreport.serializer import DailyReportSerializer
|
|
|
|
|
@@ -26,3 +27,21 @@ class ReportViews(ModelViewSet):
|
|
|
queryset = DailyReport.objects.all()
|
|
|
serializer_class = DailyReportSerializer
|
|
|
filter_fields = {'title': ['exact']}
|
|
|
+
|
|
|
+
|
|
|
+@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)
|
|
|
+ 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')
|
|
|
+ return Response(status=status.HTTP_200_OK)
|
|
|
+
|
|
|
+
|
|
|
+class LogoutView(APIView):
|
|
|
+ def post(self, request):
|
|
|
+ logout(request)
|
|
|
+ return Response(data={'msg': 'logout success!'}, status=status.HTTP_200_OK)
|