Browse Source

创建工作日报模块

zhuangyp 4 years ago
parent
commit
39f9c0547b

+ 2 - 1
Pipfile

@@ -6,10 +6,11 @@ verify_ssl = true
 [dev-packages]
 
 [packages]
-django = "*"
 django-restframework = "*"
 djangorestframework-bulk = "*"
 mysqlclient = "*"
+django = "==2.2"
+django-filter = "*"
 
 [requires]
 python_version = "3.7"

+ 10 - 10
Pipfile.lock

@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "49edf963b451d99cf67dc10752e8c513f962ea5642c41019d05bd4df301cfbd1"
+            "sha256": "3ff9a01dd35966159a0417d91925888b4145fe8df884ca612b8eda8cceed2c10"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -16,21 +16,21 @@
         ]
     },
     "default": {
-        "asgiref": {
+        "django": {
             "hashes": [
-                "sha256:7e51911ee147dd685c3c8b805c0ad0cb58d360987b56953878f8c06d2d1c6f1a",
-                "sha256:9fc6fb5d39b8af147ba40765234fa822b39818b12cc80b35ad9b0cef3a476aed"
+                "sha256:7c3543e4fb070d14e10926189a7fcf42ba919263b7473dceaefce34d54e8a119",
+                "sha256:a2814bffd1f007805b19194eb0b9a331933b82bd5da1c3ba3d7b7ba16e06dc4b"
             ],
-            "markers": "python_version >= '3.5'",
-            "version": "==3.2.10"
+            "index": "pypi",
+            "version": "==2.2"
         },
-        "django": {
+        "django-filter": {
             "hashes": [
-                "sha256:31a5fbbea5fc71c99e288ec0b2f00302a0a92c44b13ede80b73a6a4d6d205582",
-                "sha256:5457fc953ec560c5521b41fad9e6734a4668b7ba205832191bbdff40ec61073c"
+                "sha256:11e63dd759835d9ba7a763926ffb2662cf8a6dcb4c7971a95064de34dbc7e5af",
+                "sha256:616848eab6fc50193a1b3730140c49b60c57a3eda1f7fc57fa8505ac156c6c75"
             ],
             "index": "pypi",
-            "version": "==3.0.8"
+            "version": "==2.3.0"
         },
         "django-restframework": {
             "hashes": [

BIN
PyCharmItems1/__pycache__/settings.cpython-37.pyc


BIN
PyCharmItems1/__pycache__/urls-api.cpython-37.pyc


BIN
PyCharmItems1/__pycache__/urls.cpython-37.pyc


+ 19 - 0
PyCharmItems1/settings.py

@@ -36,7 +36,9 @@ INSTALLED_APPS = [
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'rest_framework',
+    'django_filters',
     'workreport',
+
 ]
 
 MIDDLEWARE = [
@@ -119,3 +121,20 @@ USE_TZ = True
 # https://docs.djangoproject.com/en/3.0/howto/static-files/
 
 STATIC_URL = '/static/'
+
+REST_FRAMEWORK = {
+    'DEFAULT_AUTHENTICATION_CLASSES': (
+        'rest_framework.authentication.BasicAuthentication',
+        'rest_framework.authentication.SessionAuthentication'
+    ),
+    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
+    'PAGE_SIZE': 10,
+    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
+    'DEFAULT_PERMISSION_CLASSES': [
+        'rest_framework.permissions.IsAuthenticated',
+    ]
+}
+
+# AUTHENTICATION_BACKENDS = (
+#     'django.contrib.auth.backends.ModelBackend',
+# )

+ 22 - 0
PyCharmItems1/urls-api.py

@@ -0,0 +1,22 @@
+"""PyCharmItems1 URL Configuration
+
+The `urlpatterns` list routes URLs to views. For more information please see:
+    https://docs.djangoproject.com/en/3.0/topics/http/urls/
+Examples:
+Function views
+    1. Add an import:  from my_app import views
+    2. Add a URL to urlpatterns:  path('', views.home, name='home')
+Class-based views
+    1. Add an import:  from other_app.views import Home
+    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
+Including another URLconf
+    1. Import the include() function: from django.urls import include, path
+    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
+"""
+from django.conf.urls import url
+from django.contrib import admin
+from django.urls import path, include
+
+urlpatterns = [
+    url(r'^workreport/', include('workreport.urls'))
+]

+ 2 - 1
PyCharmItems1/urls.py

@@ -19,6 +19,7 @@ from django.urls import path, include
 
 urlpatterns = [
     path('admin/', admin.site.urls),
+    # url(r'^api-auth/', include('rest_framework.urls')),
     url(r'^api-auth/', include('rest_framework.urls')),
-    url(r'^report/', include('workreport.urls'))
+    url(r'^api/', include('PyCharmItems1.urls-api'))
 ]

BIN
workreport/__pycache__/urls.cpython-37.pyc


BIN
workreport/__pycache__/views.cpython-37.pyc


+ 4 - 3
workreport/urls.py

@@ -2,7 +2,7 @@ from django.conf.urls import url
 
 from rest_framework_bulk.routes import BulkRouter
 
-from workreport.views import ReportView, DailyReportView
+from workreport.views import ReportView, DailyReportView, LoginView, LogoutView
 
 router = BulkRouter()
 router.register(r'dailyreport', DailyReportView)
@@ -10,6 +10,7 @@ router.register(r'dailyreport', DailyReportView)
 urlpatterns = router.urls
 
 urlpatterns += [
-    url(r'^report/', ReportView.as_view())
-
+    url(r'^report/', ReportView.as_view()),
+    url(r'^login/', LoginView.as_view()),
+    url(r'^logout/', LogoutView.as_view())
 ]

+ 31 - 3
workreport/views.py

@@ -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)